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.
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. π
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
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 π
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.
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.
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 π
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”
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.
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
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 π
Excellent Work!
I run straight through. And: Works!!
Thank you to make me happy!
Hi! Great to hear the guide was helpful π
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.
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 π
Thank you for this article, it’s working great.
Just one thing : the switch in home assistant doesn’t really show the state of the computer in real time. When I turn on the computer, the switch goes immediatly back to the off position. Although the computer has been effectively turned on
Thanks for the feedback!
Device takes some time to start up before it’s shown as ‘on’ (responds to network ICMP requests).
So far I’ve not seen this as a problem, but maybe using templated sensors this could be bypassed.
E.g. switch on the input_boolean right away and start following the real state after 30 seconds.
Hi.
Im a HA rookie and are just getting into the game.
The instructions are perfect, but i do have a question (And probably a noob one :D)
***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):***
How do you create that Switch? Where do you insert the Yaml lines?
Depending on your installation, but normally adding a new entity goes into the configuration.yaml π
Try to search previous ‘switch:’ block and add the yaml code below that. If you do not have an existing switch in there, just insert the whole block of code at the end of the file.
Hello, I’m new to HA and I’m still testing a lot. I have to control 3 printers or the Raspberrys of the printers. When I take your instructions it works perfectly the first time. when I set up the 2nd printer I can no longer control the first and when I enter the 3rd printer the other two cannot.
shell_commands.yaml
turn_off_remote_pc: “ssh -i /config/ssh_keys/id_rsa_homeassistant -o ‘StrictHostKeyChecking=no’ homeassistant@192.168.10.52 sudo shutdown”
turn_off_voron01: “ssh -i /config/ssh_keys/id_rsa_voron01 -o ‘StrictHostKeyChecking=no’ voron01@192.168.10.51 sudo shutdown”
turn_off_voron24: “ssh -i /config/ssh_keys/id_rsa_voron24 -o ‘StrictHostKeyChecking=no’ voron24@192.168.10.50 sudo shutdown”
configuration.yaml
switch:
– platform: wake_on_lan
name: RemotePC
host: 192.168.10.52
mac: +++++++from my raspberry remote
turn_off:
service: shell_command.turn_off_remote_pc
switch:
– platform: wake_on_lan
name: voron01
host: 192.168.10.51
mac: ++++++from my raspberry voron01
turn_off:
service: shell_command.turn_off_voron01
switch:
– platform: wake_on_lan
name: voron24
host: 192.168.10.50
mac: ++++from my raspberry voron24
turn_off:
service: shell_command.turn_off_voron24
I think the problem is definitely when putting on the switch, as if he only takes one and always the last one.Thanx a lot….
sorry i forget this to write. Also in my configuration.yaml is:
shell_command: !include shell_commands.yaml
Hi!
There seems to be a small issue with your configuration: you should only add one ‘switch:’ header in your configuration.yaml and all the switches below that one π
switch:
β platform: wake_on_lan
name: RemotePC
host: 192.168.10.52
mac: +++++++from my raspberry remote
turn_off:
service: shell_command.turn_off_remote_pc
β platform: wake_on_lan
name: voron01
host: 192.168.10.51
mac: ++++++from my raspberry voron01
turn_off:
service: shell_command.turn_off_voron01
β platform: wake_on_lan
name: voron24
host: 192.168.10.50
mac: ++++from my raspberry voron24
turn_off:
service: shell_command.turn_off_voron24
Hopefully that will solve your issue!
my Pc with remote Linux crashed recently, had to re-install entirely, and this helped me a lot to make it work again, because I was forgetting to add the nopasswd to the sudoers file. Thanks for sharing!
Great it was helpful and thanks for your positive feedback!
where to put the code?
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
configuration.yaml is the place to put this code into.
Thank you!! One tip… Some cases is necessary put the homeassistant user in SSH group, like openmediavault case.
Hello,
I am using Linux Mint and when I test the ssh command from Home Assistant it fails. I am sending the following command:
ssh -1 /config/ssh_keys/id_rsa_homeassistant -o ‘StrictHostKeyChecking=no’ homeassistant@192.xxx.xxx.xxx sudo reboot -h now
and this is what it returns:
sudo: a terminal is required to read the password; either use -S option to read from standard input or configure an askpass helper
sudo: a password is required
Any suggestions on how to fix this would be appreciated
Ignore my above comment. When I edited sudo I mistakenly did not add the homeassistant account entry at the bottom of the file as I should have. You might want to add this to the instructions so others don’t repeat my mistake.
I think I’ll be setting this up for my OMV server so I can automate a graceful shutdown upon loss of power. Thanks for the guide,
Hello! First of all, let me thank you for your help, thank you! This is exactly the information I was looking for.
I did it for mac, not Linux, and it worked perfectly.
My mac is finicky and won’t play music when he is in a bad mood :<
So I use this to restart it. It helped me very, very much.
Thanks for your guidance, really appreciated.
I have implemented the above and the remote shutdown from Home Assistant is working correctly, I can however not get WOL to work on a Raspberry PI 4b.
I have tried another WOL utility but without success.
I see mixed posts on PI4 support for WOL. Can you confirm it should work, and if so, is there anything I have to configure in the OS?
Unfortunately I don’t have experience of PI4 wol support so can’t really confirm it.
I don’t see any issues why it wouldn’t work, but you never know…