Running a shell command from Home Assistant to remote linux PC

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.

@Tinkerer I have try to lunch form HA Raspberry a script into my MacMini with this code:

  - platform: command_line
    switches:
      telecamere_switch:
        command_on: "ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo -u leo osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'"
        command_off: "ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo -u leo osascript /Users/leo/Desktop/Scripts/DisarmaTelecamere.scpt'"
        friendly_name: Telecamere

But HA log say:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:23
Integration: command_line (documentation, issues)
First occurred: 15:20:13 (1 occurrences)
Last logged: 15:20:13

Command failed: ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo -u leo osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

See the discussion on Discord :wink:

  1. You’re connecting as leo - why are you using sudo to become leo?
  2. Have you confirmed that the SSH key works?

I tried removing “sudo” but it always asks for the password. i think at this point it is an ssh key problem. Is there a guide to follow to configure correctly?

Quoting from a response to somebody else the other day:

When using keys with SSH the host you’re connecting to needs to be told to accept a key
Typically that’s done by adding the public key to the .ssh/authorized_keys file to the specified account on the remote host
There should be quite a few good guides on this findable via any search engine:
https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/
How To Set Up SSH Keys on Ubuntu 12.04 | DigitalOcean
What is an SSH Key? An Overview of SSH Keys

Thanks now if I type in HA terminal:

ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

Now work from terminal… But if I use in configuration.yaml:

- platform: command_line
    switches:
      telecamere_switch:
        command_on: "ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'"
        command_off: "ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/DisarmaTelecamere.scpt'"
        friendly_name: Telecamere

Don’t Work:

2020-10-23 22:48:42 ERROR (SyncWorker_11) [homeassistant.components.command_line.switch] Command failed: ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

Why

The

Heck

Did you remove all the arguments to ssh? I didn’t say to do that… I said to remove the sudo -u leo part

1 Like

Thanks Tinker, I have try:

ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

Is the same…

You have the ssh-key in the correct file of your Mac?

If you replace the command with just date does it work?

If not, the problem is with the key

Thanks for your answers I think I have installed the keys correctly, in fact if I try from the HA terminal to access SSH to the MacMini it works and does not ask me for a password. I needed to access via SSH to control the cameras via SecuritySpy software. I found a specific plugin and solved it. Thanks to everyone anyway!

Hi guys, totally new here.

I’m running HA as supervised (hassio) install on Docker on Raspbian (RPI 4A). As I use the RPI for gaming (steamlink) as well as media center (kodi) I need a way to switch between those when needed. I figured out that ssh’ing to console to run commands will be the best option here. I have ssh setup with personal keys already from my win10 PC. I have generated public key via hassio terminal as well. Even though I could not localize root folder somehow, I used cp to copy the id_rsa.pub to config/ssh. I have added hassio public key to RPI ssh known_hosts. I can ssh from hassio terminal without any problems (no password needed as I disabled it). When running shell_command from automation it does not work though.

I have tried different commands like:

All of above result with Error 255. When running from hassio terminal simple ssh [email protected] kodi works straight away.

I suspect it has something to do with user that is running those commands. I presume that running those from hassio terminal makes me acting as root (I think it is confirmed in ssh public key that I generated from terminal as it has root@core-ssh as acting user at the end of key. Automation will not run as root as I think :smiley: . The thing is I have no idea what is the user that runs shell_command in automation. I have tried find something useful in Portainer data for both hassio_core and homeassistant containers to no avail.

Any hints to where should I place my next steps? Also sorry for this terribly long post.

Please see point 16 in here :wink:

I clearly didn’t follow the rules as I should, sorry for that. Edited previous post :slight_smile:

Odds are high that the problem is your use of the public key in those commands :wink: The public key goes to the authorized_keys file on the remote host, the private key is the one you use when connecting.

1 Like