Running a shell command from Home Assistant to remote linux PC

Set logger to info, restart HA, and then try the command. You’ll have log entries about any issues.

This is what I get when executing the command:

[homeassistant.components.command_line.switch] Running command: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] sudo -S poweroff

Not really helping I guess

Update:
After some seconds this shows:
[homeassistant.components.command_line.switch] Command failed: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] sudo -S poweroff

By the way. I get this error in my Synology logs every time I try to execute the command
User [pi] from [192.168.1.13] failed to log in via [SSH] due to authorization failure.

Ok, well, progress :smiley:

Are you able to open a shell inside that Docker container, running HA, and run the SSH command?

What about ownership of the different folders? I can see that the ownership of the folder “home” is root, homeassistant folder is “homeassistant” and “.homeassistant” is homeassistant. Should I change something about that?
I have a user “pi” on the Synology. The pub key is stored within the home folder of this user.

If you use ssh-copy-id to copy the public key to the remote computer, it will be placed inside the file ~/.ssh/authorized_keys.

Maybe but I am not able to use ssh-copy-id from cmd.
I have used this and guess it should be kind of the same or??
scp c:/users/jesper/.ssh/id_rsa.pub [email protected]:/var/services/homes/jesper/.ssh/authorized_keys
At least the function works when trying to connect through cmd.
ssh [email protected]
No password is requested.

I have now tried to create new keys on my Pi instead of my PC when logged in as pi.
Instead of copying the keys to default /home/pi/.ssh/id_rsa i guess I have to choose
/home/homeassistant/.homeassistant/ssh/id_rsa.pub.
When I then try to: ssh-copy-id -i home/homeassistant/.homeassistant/ssh/id_rsa [email protected] it returns with this error
failed to open ID file ‘home/homeassistant/.homeassistant/ssh/id_rsa.pub’: No such file

If HA is running in Docker, that path isn’t valid.

It is not running in Docker.
Where can I see what user HA is running under?

Then,how is it running on the Synology? I’ve only seem people use VMs and Docker there.

It isn´t. HA is installed on Raspbian.
I just want to shutdown my Synology NAS with a command from HA.
To be honest I thought that it would be as simple as copying a functioning private key to the Pi :slight_smile:

Then, how’s it installed on Raspbian :wink:

Just followed this guide a long time ago :slight_smile:
https://www.home-assistant.io/docs/installation/raspberry-pi/
Is that why I can´t the /config/ folder anywhere?

Update:
As I looked through the old guide for installation I noticed that HA is running under the user homeassistant. I then tried to rename the “ssh” user in Synology from pi to homeassistant and changed the same in the command line.
command_on: ssh -i /home/homeassistant/.homeassistant/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] ‘sudo /sbin/shutdown -h now’
Now i successfully login to the Synology :grinning: but still gets this error in the logs.
[homeassistant.components.command_line.switch] Command failed: ssh -i /home/homeassistant/.homeassistant/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] ‘sudo /sbin/shutdown -h now’

That means that the file didn’t exist on your local computer!

Is this user allowed to call sudo and to shutdown the NAS?
The name of the user of the remote computer (here the NAS) should have no relevance. I do some things on another Pi where the remote user has the name tc. As long as in this users .ssh/authorized _keys file is the public key from your local user it should work.

If the file not existed when you tried to ssh-copy-id the file, why should it be there now?

First problem is like the “smart” quotes:

ssh -i /home/homeassistant/.homeassistant/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] ‘sudo /sbin/shutdown -h now’
                                                                                                             ^                          ^

Replace those with proper quotes, then try again.

ssh -i /home/homeassistant/.homeassistant/ssh/id_rsa -o StrictHostKeyChecking=accept-new [email protected] 'sudo /sbin/shutdown -h now'

I tried to change “stricthostkeychecking” and the qoutes but no change.
I noticed that only root can perform a shutdown so maybe this error is related to that. What I am not sure what to do about it.

The file was there but afterwards I just copied the file from my pc to home/homeassistant/.homeassistant/ssh and was successfully logged in after the renaming of user to homeassistant.

When I try to shutdown from putty with:
sudo ssh [email protected] poweroff
it returns: Need to be root
So I guess shutdown is only possible with root user. When using root instead it also still wants a password.
I have tried to add user homeassistant to the root group without any difference.
“usermod -G root homeassistant”
I tried to rename the user created for this on my Synology to something different and then login is no more possible.