Guide: Start up and shut down remote linux PC using Home Assistant

Sometimes there’s a need to remote start and stop a Linux PC through Home Assistant. I’m currently having a need to start and stop my headless VDR/streaming PC when needed. The PC is only required once or twice in a month so I don’t want to keep it up 24/7 just to consume electricity.. Since it’s not ‘that simple’ I decided to write a quick guide about it.

Start up

For the startup we are going to be using Wake-On-Lan (WOL). WoL is a ethernet computer networking standard that allows a computer to be turned on or awakened by a network message.

WoL needs to be enabled from computers BIOS settings. So, to get the startup working, find a manual of your remote PC motherboard and find a way to enable WoL or just enter BIOS and explore 🙂

That’s it.. we’ll get back to this when configuring the Home Assistant at the end of this article.

Shut down

The shut down procedure is a bit more complicated than the start up. In this guide I’m using remote shell commands through SSH and password-less SSH connection is achieved by using public and private key pairs. Public and private key pairs can be though of as a key (local machine) and a lock (remote machine) that work together for security.

Prerequisites

I’m assuming you’re even a bit familiar with linux shell and have already basic SSH access to your remote machine and home assistant.

You should also know the IP and MAC addresses of your Remote PC.

Configuring the remote PC

First we need to configure the remote linux pc to support shut down in a secure way. Normally shutting down a linux machine from command line requires a root user access and of obviously we don’t want to give our Home Assistant full root access in case of e.g. security breach.

So, let’s create a new user for our Home Assistant instance by SSH into remote machine and typing

sudo adduser homeassistant

Fill out the information requested including password, you’ll need it later.

Now that we have our HA user created we need to grant it an access to reboot the machine. That can be done by editing the sudoers file.

sudo visudo

The sudoers file should be now open with visudo that verifies syntax of the file. You really don’t want to mess it up 🙂 Now lets add the access right for our newly created homeassistant user by adding line below user privilege specification

# User privilege specification
homeassistant ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown

Remember not to delete anything from the file, just add the above line in it. Now save and exit and we’re good to move to the next section. Oh, the SSH connection to your remote machine can now be closed, no need to configure that part anymore.

Creating SSH key pair

Next we’ll going to SSH into Home Assistant (you need to have SSH add-on installed in home assistant).

Start by creating the RSA public and private key pairs by typing

mkdir /config/ssh_keys
ssh-keygen -t rsa -f /config/ssh_keys/id_rsa_homeassistant

When a password is queried, leave it empty. If fulfilled with password the SSH connection will require both key and password and can’t be used for remote commands by home assistant.

In above lines we first create a directory for our ssh keys, because Home Assistant is run in a docker container and does not have visibility to default /root/.ssh folder at all. Second line starts the key generation.

Next we are going to copy the public key to our remote machine (just change the remote_hostname to proper IP address or hostname of the remote linux PC):

ssh-copy-id -i /config/ssh_keys/id_rsa_homeassistant.pub homeassistant@remote_hostname

Your homeassistant password to the remote machine will be queried at this point, so enter it.

That’s it! We now have key pairs created and transferred it to the remote machine. You can verify that it’s working by SSH’ing into remote machine from home assistant shell

ssh -i /config/ssh_keys/id_rsa_homeassistant homeassistant@remote_hostname

If everything is working as expected, no password will be queried. You can even then try to turn off the machine by typing ‘sudo poweroff -h now’. That should not query for password either and the machine should be turned off. Now lets move to configure the home assistant!

Configuring Home Assistant

Configuration is done through configuration.yaml and shell_commands.yaml files (using SSH or configuration editor add-on).

Open the shell_commands.yaml in editor, create if not existing, and add following line in it (change the remote_hostname to your remote PC IP address or hostname):

turn_off_remote_pc: "ssh -i /config/ssh_keys/id_rsa_homeassistant -o 'StrictHostKeyChecking=no' homeassistant@remote_hostname sudo shutdown -h now"

Let’s add support to shell commands in our configuration.yaml file

shell_command: !include shell_commands.yaml

Now that the shell_commands are in place, let’s create switch that turns on/off the machine (replace host and mac address of your remote machine):

switch:
  - platform: wake_on_lan
    name: RemotePC
    host: 192.168.1.100
    mac: 12:34:56:78:90:ab
    turn_off:
      service: shell_command.turn_off_remote_pc

And it’s done! Now restart home assistant and add the RemotePC switch in Lovelace UI. The machine should start and turn off when requested.

14 Replies to “Guide: Start up and shut down remote linux PC using Home Assistant”

  1. A few questions:
    1. an user ‘home-assistant’ is added, but later on in the document the hyphen is omitted, which one should I use?
    2. sudo visudo edits sudoers.tmp is this correct? the tmp extension i mean.
    3. ssh-copy-id -I results in “illegal option” and fails. should i use a lower case i ?
    4. ssh-copy-id (with a lower case i) to my remote host fails on CONNECTION REFUSED.
    and now i’m stuck. 🙁

    1. Hi and thanks for the feedback. Seems like I’ve made few typos in the instructions.

      1. homeassistant as user is correct, my bad in the instructions. Will fix it immediately
      2. should be, visudo will edit tmp file first and make the export to proper place itself
      3. ssh-copy-id -i is the correct one, somehow I’ve managed to typo (again) it in the instructions. Will fix it right away.
      4. Maybe the fixes above will fix the issue. Can you please try again and if still not working report back, and I’ll look further

      Br, Toni

    2. Addendum:

      5. I kept naming to home-assistant (with the hyphen) in all further command lines.
      6. I installed the ssh suite on the remote machine and now the ssh-copy-id worked. the ssh server component needed on the remote machine isn’t installed by default linux mint installation it seems.
      7. I followed your instructions on the ssh-creation strictly, I left all the password fields blank. Alas ssh-ing into the remote machine stubbornly keeps asking for a password.
      stuck again 🙁

      1. Hi,

        In remote machine under home-assistant user there should be the key copied in file ~/.ssh/authoized_keys

        Please ensure that the file exists in that location. If not, there has gone something wrong with the key copy.

        1. Hi Tony. I’m all set.
          In your instructions, the line:
          ssh -i /config/ssh_keys/id_rsa homeassistant@remote_hostname
          should read:
          ssh -i /config/ssh_keys/id_rsa_homeassistant homeassistant@remote_hostname

          Thanks for your prompt replies and help.

          1. Hi!

            Great you got it working and thanks for pointing this issue out as well.
            Sometimes it’s difficult to write 100% accurate instructions after proof of concept level implementation.

            I will try to improve with future guides 🙂

        2. Darn. The WOL feature doesn’t work, it is enabled in the target PC BIOS
          Should the MAC address in configuration.yaml be quoted maybe, like:
          mac: “24:4b:fe:8d:ca:fb”

          1. Quotes doesn’t matter in HA mac addr configurations. I have two PCs with remote on/off, one seems to have quotes and another doesn’t, both still work.

            You could try to use wake-on-lan using external wol application just to see if the problem is at remote machine or in HA config.

  2. Hi, this is tina from dusuniot.
    I saw your website, noted you are focus on the RPI and smart devices, which use home assistant opensource platform.
    we are IoT hardware provider, and have edge gateway which can install home assistant/ node-red/jeedom, ect.
    for details, pls see our website: https://www.dusuniot.com/iot-app-gateway/home-assistant-gateway
    it is easy for customer to use the home assistant and add the device.
    hope we have the opportunity to cooperate.

    Best regards
    Tina

    1. Hi,

      It’s great to hear that more hardware providers are coming to the market that are supporting Home Assistant straight out of the box!

      I’m not actually using RPI, but running Home Assistant on virtual machine running on a mini pc.

      I’d be interested to test some of your devices and write a review of those if you want to.

      You can contact me through email creatingsmarthome (at) gmail.com and we can try to find a way to do some cooperation 🙂

  3. Hi. Thank you for the guide and the explanation – makes things clearer for us newbies. A couple of questions, though. I am running my HA instance on a Docker container in a raspberry pi. How will I be able to achieve this using my setup? Thanks again.

    1. Hi!

      You should be able to use the HA container to achieve this, but most probably first steps won’t work as in this guide suggests.
      I don’t have experience of running HA in a Docker container so unfortunately can’t help you with this 🙁

Leave a Reply to Christian Karl Cancel reply

Your email address will not be published.