I am running HA on a dedicated RPi5.
I use ssh to log in and send commands from my laptop.
The following works:
# ssh homeassistant
$ ha core status
The following all fail:
# ssh homeassistant "ha core status"
# ssh homeassistant "sudo ha core status"
# ssh homeassistant "sudo /usr/bin/ha core status"
# ssh homeassistant "sudo -E /usr/bin/ha core status"
In all the above cases, I get the error message:
Unexpected server response. Status code: 401
time="2024-07-18T12:33:56-04:00" level=error msg="Unexpected server response. Status code: 401"
Why am I not able to send ‘ha core status’ over ssh but can only send the command when logged in interactively (via ssh)?
Status?
There does not appear to be a command ha core status.
➜ /config git:(master) ✗ ha core
This command provides a set of subcommands to control the Home Assistant Core
instance running on this installation.
It provides commands to control Home Assistant Core (start, stop, restart),
but also allows you to check your Home Assistant Core configuration.
Furthermore, some options can be set and allows for upgrading/downgrading
Home Assistant Core.
Usage:
ha core [command]
Aliases:
core, homeassistant, home-assistant, ha
Examples:
ha core check
ha core restart
ha core update
ha core update --version 2021.11.5
Available Commands:
check Validates your Home Assistant Core configuration
info Provides information about Home Assistant Core
logs View the log output of Home Assistant Core
options Allow to set options on Home Assistant Core instance
rebuild Rebuild the Home Assistant Core instance
restart Restarts the Home Assistant Core
start Manually start Home Assistant Core
stats Provides system usage stats of Home Assistant Core
stop Manually stop Home Assistant Core
update Updates the Home Assistant Core
Well ha core info (and all the other flavors) have the same problem.
And I can’t understand what would allow execution from an interactive session but not from a non-interactive one…
Also maybe its undocumented, but ‘status’ works:
# ssh homeassistant
$ ha core status
blk_read: 0
blk_write: 0
cpu_percent: 0.11
memory_limit: 8328765440
memory_percent: 4.46
memory_usage: 371695616
network_rx: 0
network_tx: 0
Interestingly, the following “partial” commands work as in they return the usage message.
# ssh homeassistant "ha"
# ssh homeassistant "ha core"
However ssh homeassistant "ha core [command]" always fails with:
Unexpected server response. Status code: 401
time="2024-07-18T12:52:29-04:00" level=error msg="Unexpected server response. Status code: 401"
Is there some type of ‘token’ missing?
Keep in mind you are not the same user, and are in a container coming in thru ssh.
How am I different when logging in via an interactive ssh session vs. passing a command via ssh?
One thing I noticed is that in the interactive session, there is an environment variable, SUPERVISOR_TOKEN that is set but that is not present when sending a command over ssh.
Perhaps that is the problem?
That does seem to be the problem as the following 2 methods work:
# ssh homeassistant "SUPERVISOR_TOKEN=<SUPERVISOR-TOKEN> ha core status
# ssh homeassistant "ha --api-token <SUPERVISOR-TOKEN> ha core status"
So now my question reduces to how can I get a non-interactive ssh session to inherit the SUPERVISOR_TOKEN environment variable
NOTE that /etc/sshd_config contains the line:
PermitUserEnvironment SUPERVISOR_TOKEN
Note the following kludge will also work:
ssh homeassistant "SUPERVISOR_TOKEN=\$(sudo cut -d= -f2 /data/.ssh/environment) ha core status"
YUK!
Is there a better way?