I always find setting up control of devices with Hassio, using SSH a little difficult to remember and I thought it might be useful to someone (and me the next time I need to do it) if I posted a complete guide.
Prerequisites: SSH add on
Here goes:
Generate SSH Key
from hassio (terminal), change to the config directory, Generate a passphraseless SSH key and push it to entity.
$ cd /config
$ mkdir .ssh
$ ssh-keygen -t rsa -b 2048
Response:
Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: ...... cut ......
Push Key to Entity
Next copy the files to ââ/config/.sshââ
$ cp /root/.ssh/* .ssh/
This is what we end up with:
$ ls .ssh
authorized_keys id_rsa id_rsa.pub known_hosts8
Next Copy your keys to the target server(s); ie for 192.168.1.185:
$ ssh-copy-id [email protected]
[email protected]âs password:
This command establishes trust between the server (192.168.1.185) and any SSH Client connecting to the server, using these keys to authenticate.
Note: You may wish to use a different user (not root); in my case I wish root access to the device
Now try logging into the machine, with ssh like this
$ ssh [email protected]
If everything worked then you should get an ssh connection to 192.168.1.185
Example Automation
In this case, as we are connecting as root we can issue any command we like from home assistant. In the following example we setup a shell_command which actually issues the command to the entity, using ssh; a Switch, which invokes the 'shell_command; and an Automation that uses the SwitchâŚ
shell_command
shell_command:
turn_off_defiant: 'ssh -o UserKnownHostsFile=/config/.ssh/knownhosts -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] sudo shutdown -Ph now'
Switch
switch:
- platform: wake_on_lan
name: defiant_wol
mac_address: "AA:BB:CC:DD:EE:FF"
host: "192.168.1.185"
turn_off:
service: shell_command.turn_off_defiant
Automation
alias: NAS Drive OFF
initial_state: false
trigger:
platform: state
entity_id: group.computers
from: "on"
to: "off"
for: "00:05:00"
action:
- service: switch.turn_off
entity_id: switch.defiant_wol
Thatâs about it: Hope you find it useful!
You can find my homeAssistant setup right here on GitHub.