Turn on/off UnRaid Server from Hassio & Voice Assistant

I host hassio on a Intel NUC and it bothered me every time I wanted to shut down the UnRAID server I had to login to the UnRAID UI to do it. I could have paid £4 for the UnRAID app but that didn’t sit well when I just want to ask Google to turn off the server. It should be simple…

So after much Googling I’ve cobbled together this guide on how to turn on/off UnRaid from Hassio using RSA SSH and Wake on LAN. Most importantly it works from the Google Home :smiley:

Be aware this guide assumes you have WOL configured correctly in the BIOS.

Hopefully some of you will find this helpful…

Let’s begin…

Create RSA Token Home Assistant CLI

  1. Create UnRaid Directory in hassio dir

     mkdir /unraid/
     cd /unraid
     mkdir .ssh
    
  2. Generate the RSA Key

     ssh-keygen
    

    specifiy the location of the output to be /config/unraid/.ssh/rsa_key_name

  3. Once an SSH key has been created, the ssh-copy-id command can be used to install it as an authorized key on the server. Once the key has been authorized for SSH, it grants access to the server without a password.

Use a command like the following to copy SSH key:

    ssh-copy-id -i /config/unraid/.ssh/rsa_key_name user@host

enter the password when requested

  1. Once the key has been copied, it is best to test it

     ssh -i /config/unraid/.ssh/rsa_key_name user@host
    
  2. If the UnRaid server is rebooted before when next section of the guide is completed your SSH keys will get removed from UnRaid and you will need to start again from step 3.

Persistent SSH UnRaid

  1. Check authorized_keys exists

     ls /root/.ssh
    
  2. Change permission of authorized_keys to 600:

     chmod 600 /root/.ssh/authorized_keys
    
  3. Create a script in /boot/config/ssh/setup_ssh_client.sh:

     #!/bin/bash
    
     SSH_DIR=/root/.ssh
    
     mkdir ${SSH_DIR}
     chmod 755 ${SSH_DIR}
     cp /boot/config/ssh/authorized_keys ${SSH_DIR}/authorized_keys
     chmod 600 ${SSH_DIR}/authorized_keys
    
  4. make it executable:

     chmod 755 /boot/config/ssh/setup_ssh_client.sh
    
  5. Copy the previously created authorized_keys into /boot/config/ssh folder

     cp /root/.ssh/authorized_keys /boot/config/ssh/
    
  6. Edit /boot/config/go and add the following line at the end, so that our setup script can be called during reboot:

     # SSH Script
     /boot/config/ssh/setup_ssh_client.sh
    
  7. Reboot Unraid server

     powerdown -r
    
  8. Confirm the script has worked and that authorized_keys exists as we did in Step 1

     ls /root/.ssh
    
  9. Install and configure SSH Config Tool from the UnRaid community app store

Hassio Config

# Server On/Off Switch
switch:
  - platform: wake_on_lan
    mac_address: "00-01-02-03-04-05"
    name: home_server
    host: 192.168.0.2
    broadcast_address: 255.255.255.255
    turn_off:
        service: switch.turn_off
        entity_id: switch.server_off

  - platform: command_line
    switches:
      server_off:
        command_on: switch.home_server
        command_off: "ssh -i /config/unraid/.ssh/id_rsa -o StrictHostKeyChecking=no user@host 'powerdown'"

customize:
  switch.home_server:
    friendly_name: Server
    icon: mdi:server

Lovelace card

type: entities
entities:
  - entity: switch.home_server

image

7 Likes

Great write up. As a user of unRaid this good stuff. Unfortunately I cant take any benefit from it as I host my home assistant via docker on unRaid.

As a unRaid user myself I was wondering what your usecase is?
The only time I want to shutdown unRaid is when I am changing something, software or hardware on the server.
Can you share your usecase?

I use UnRAID for Plex data storage, Plex docker, downloads, and archive storage for Windows via SMB. Due to power costs and doing my part for the environment I don’t see a reason for UnRAID to be on 24/7 as it pulls considerable more power than the NUC, so it gets switched off when not in use. Eg. When everyone is asleep :slight_smile:

Thanks, I see your case now. For me it runs everything, including HA. So it stays on 24/7.

@noodlemctwoodle: thanks a lot for this great write up! I’m using it daily now.

@smoldersonline perfect, glad someone else is making use of it :smile:

1 Like

@noodlemctwoodle Thank you so much for sharing this guide. But I have a problem. I do all the descripted things with the Addon SSH & Web Terminal. So the key creation, copy etc. worked pretty well. When I isssue the ssh connection and commands from this SSH Terminal all is working like a charme. But when I trigger the Switch or an Automation, the exact same command which works in the SSH Terminal, wont work.

Command failed: ssh -i /unraid/.ssh/rsa- -o StrictHostKeyChecking=no root@unraidip ‘powerdown -r’

So my assumtion is, that I have to create the key on the hass.io OS layer, but the ssh terminal is a docker container. So any hints or tipps, how I can access the hass.io OS layer to create the keys there? Or I am completely wrong?

Best regards

@ph0b0s101 - Edit: Probably should have said Welcome to the Community :smiley:

To be honest a lot has changed since I originally posted this guide.

I have rebuilt my UnRAID server on the AMD 3700X platform and consolidated all of my individual network appliances onto this server, therefore I am now unable to turn off UnRAID. It is now running Home Assistant Core and all of the containers associated with Home Assistant, my Firewall and, other home resources such as cloud backup and Plex :slight_smile:

You could try and install SSH & Terminal add-on for hass.io this would give you shell access into the container.

Thank you for answering so quickly. That is what I already did. But it seems that the key is only available in the ssh addon, but when I issue a shell_command via a Switch or Automation, this will be issued on the hassio core system, and there are no ssh keys because they are in the docker container. So I think I have find a way to generate them directly on the hassio layer. Did you get what I mean?

How do you have hass.io installed and on what platform?

Hass.io was installed with Balena Etcher on my USB 3.0 SSD for the Raspberry PI 4 with 4GB.

@noodlemctwoodle

image

Thank you so much for sharing this configuration. I am able to power on/off my UnRaid server with Siri, but it only recognize “Server” instead of “UnRaid-3700x”. Do you know it is possible to voice control by the server name?

1 Like

@koffee - I really couldn’t tell you with Siri, as I have never owned or will own an Apple product in my life :green_apple:

I have a thing about :green_apple: products, it’s like a trigger word that sets off a small rage inside my head :stuck_out_tongue:

I’m not sure if Siri has this method for Nabu Casa
https://www.nabucasa.com/config/google_assistant/

Here you can specify the device and the Name for Google Assistant, which makes naming the devices very easy. Once you name the device in Yaml, it shows up on Google Home with the same name.

Sorry, I cant be more help.

nice write-up! I’m going to use this method to turn on and off docker containers via alexa

1 Like

Hi,
thanks for this little guide. I use it to ssh to UnRaid and then execute some script there.
The Persistent SSH steps are no longer required from UnRaid 6.9

Home Assistant command line code doesn’t work but if i use terminal it works.

Unraid logs:

Connection from 192.168.1.36 port 35846 on 192.168.1.10 port 22 rdomain ""
Dec 8 17:28:49 unRAID sshd[1753]: Accepted key RSA SHA256: found at /root/.ssh/authorized_keys:2
Dec 8 17:28:49 unRAID sshd[1753]: Postponed publickey for root from 192.168.1.36 port 35846 ssh2 [preauth]
Dec 8 17:28:49 unRAID sshd[1753]: Postponed keyboard-interactive for root from 192.168.1.36 port 35846 ssh2 [preauth]
Dec 8 17:28:49 unRAID sshd[1755]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.36 user=root
Dec 8 17:28:51 unRAID sshd[1753]: error: PAM: Authentication failure for root from 192.168.1.36
Dec 8 17:28:51 unRAID sshd[1753]: Failed keyboard-interactive/pam for root from 192.168.1.36 port 35846 ssh2
Dec 8 17:28:51 unRAID sshd[1753]: Postponed keyboard-interactive for root from 192.168.1.36 port 35846 ssh2 [preauth]
Dec 8 17:28:51 unRAID sshd[1789]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.36 user=root

EDIT:
Regenerated the keys without password and set permit root login yes and now it works.

I’d like to set this up myself and am running Unraid 6.9, which steps from the OP could you skip?

from memory you can skip the whole Persistent SSH UnRAID section of the original post.

UnRAID has made some changes so that the SSH keys are stored on the flash drive by default.

https://wiki.unraid.net/Manual/Release_Notes/Unraid_OS_6.9.0#SSH_Improvements

1 Like

Cool, thanks a lot, will give this a shot :slight_smile: