{"id":929,"date":"2022-02-12T15:09:08","date_gmt":"2022-02-12T15:09:08","guid":{"rendered":"https:\/\/www.creatingsmarthome.com\/?p=929"},"modified":"2023-11-06T12:59:12","modified_gmt":"2023-11-06T12:59:12","slug":"guide-start-up-and-shut-down-remote-linux-pc-using-home-assistant","status":"publish","type":"post","link":"https:\/\/www.creatingsmarthome.com\/index.php\/2022\/02\/12\/guide-start-up-and-shut-down-remote-linux-pc-using-home-assistant\/","title":{"rendered":"Guide: Start up and shut down remote linux PC using Home Assistant"},"content":{"rendered":"\n<p>Sometimes there&#8217;s a need to remote start and stop a Linux PC through Home Assistant. I&#8217;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&#8217;t want to keep it up 24\/7 just to consume electricity.. Since it&#8217;s not &#8216;that simple&#8217; I decided to write a quick guide about it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"start-up\">Start up<\/h2>\n\n\n\n<p>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. <\/p>\n\n\n\n<p>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 \ud83d\ude42 <\/p>\n\n\n\n<p>That&#8217;s it.. we&#8217;ll get back to this when configuring the Home Assistant at the end of this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"shut-down\">Shut down<\/h2>\n\n\n\n<p>The shut down procedure is a bit more complicated than the start up. In this guide I&#8217;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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites<\/h3>\n\n\n\n<p>I&#8217;m assuming you&#8217;re even a bit familiar with linux shell and have already basic SSH access to your remote machine and home assistant.<\/p>\n\n\n\n<p>You should also know the IP and MAC addresses of your Remote PC.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"configuring-the-remote-pc\">Configuring the remote PC<\/h3>\n\n\n\n<p>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&#8217;t want to give our Home Assistant full root access in case of e.g. security breach.<\/p>\n\n\n\n<p>So, let&#8217;s create a new user for our Home Assistant instance by SSH into remote machine and typing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo adduser homeassistant<\/code><\/pre>\n\n\n\n<p>Fill out the information requested including password, you&#8217;ll need it later.<\/p>\n\n\n\n<p>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 <em>sudoers<\/em> file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo visudo<\/code><\/pre>\n\n\n\n<p>The <em>sudoers<\/em> file should be now open with <em>visudo<\/em> that verifies syntax of the file. You really don&#8217;t want to mess it up \ud83d\ude42 Now lets add the access right for our newly created homeassistant user by adding line below user privilege specification<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># User privilege specification\nhomeassistant ALL=(ALL) NOPASSWD: \/sbin\/poweroff, \/sbin\/reboot, \/sbin\/shutdown<\/code><\/pre>\n\n\n\n<p>Remember not to delete anything from the file, just add the above line in it. Now save and exit and we&#8217;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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-ssh-key-pair\">Creating SSH key pair<\/h3>\n\n\n\n<p>Next we&#8217;ll going to SSH into Home Assistant (you need to have SSH add-on installed in home assistant).<\/p>\n\n\n\n<p>Start by creating the RSA public and private key pairs by typing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/config\/ssh_keys\nssh-keygen -t rsa -f \/config\/ssh_keys\/id_rsa_homeassistant<\/code><\/pre>\n\n\n\n<p>When a password is queried, leave it empty. If fulfilled with password the SSH connection will require both key and password and can&#8217;t be used for remote commands by home assistant.<\/p>\n\n\n\n<p>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. <\/p>\n\n\n\n<p>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):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id -i \/config\/ssh_keys\/id_rsa_homeassistant.pub homeassistant@remote_hostname<\/code><\/pre>\n\n\n\n<p>Your homeassistant password to the remote machine will be queried at this point, so enter it.<\/p>\n\n\n\n<p>That&#8217;s it! We now have key pairs created and transferred it to the remote machine. You can verify that it&#8217;s working by SSH&#8217;ing into remote machine from home assistant shell<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -i \/config\/ssh_keys\/id_rsa_homeassistant homeassistant@remote_hostname<\/code><\/pre>\n\n\n\n<p>If everything is working as expected, no password will be queried. You can even then try to turn off the machine by typing <em>&#8216;sudo poweroff -h now&#8217;<\/em>. That should not query for password either and the machine should be turned off. Now lets move to configure the home assistant!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"configuring-home-assistant\">Configuring Home Assistant<\/h3>\n\n\n\n<p>Configuration is done through configuration.yaml and shell_commands.yaml files (using SSH or configuration editor add-on).<\/p>\n\n\n\n<p>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):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>turn_off_remote_pc: \"ssh -i \/config\/ssh_keys\/id_rsa_homeassistant -o 'StrictHostKeyChecking=no' homeassistant@remote_hostname sudo shutdown -h now\"<\/code><\/pre>\n\n\n\n<p>Let&#8217;s add support to shell commands in our configuration.yaml file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>shell_command: !include shell_commands.yaml<\/code><\/pre>\n\n\n\n<p>Now that the shell_commands are in place, let&#8217;s create switch that turns on\/off the machine (replace host and mac address of your remote machine):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch:\n  - platform: wake_on_lan\n    name: RemotePC\n    host: 192.168.1.100\n    mac: 12:34:56:78:90:ab\n    turn_off:\n      service: shell_command.turn_off_remote_pc<\/code><\/pre>\n\n\n\n<p>And it&#8217;s done! Now restart home assistant and add the RemotePC switch in Lovelace UI. The machine should start and turn off when requested.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div style=\"border: 3px dashed #abb8c3; border-radius: 0%; background-color: inherit; \" class=\"ub-styled-box ub-bordered-box wp-block-ub-styled-box\" id=\"ub-styled-box-97da4800-b620-4432-a397-172611778771\">\n<p id=\"ub-styled-box-bordered-content-\"><h5 class=\"wp-block-heading has-text-align-center\">Did you find this guide helpful? You can keep the blog going by bidding me a coffee!<\/h5>\r\n<center>\r\n<script type=\"text\/javascript\" src=\"https:\/\/cdnjs.buymeacoffee.com\/1.0.0\/button.prod.min.js\" data-name=\"bmc-button\" data-slug=\"tokorhon\" data-color=\"#FFDD00\" data-emoji=\"\"  data-font=\"Cookie\" data-text=\"Buy me a coffee\" data-outline-color=\"#000000\" data-font-color=\"#000000\" data-coffee-color=\"#ffffff\" ><\/script>\r\n<\/center><\/p>\n\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Sometimes there&#8217;s a need to remote start and stop a Linux PC through Home Assistant. I&#8217;m currently having a need to start and stop my&hellip;<\/p>\n","protected":false},"author":1,"featured_media":943,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3,100],"tags":[7,172,171,175,178,179,176,173,177,174],"class_list":["post-929","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide","category-home-assistant","tag-home-assistant","tag-linux","tag-remote-pc","tag-shut-down","tag-start","tag-stop","tag-turn-off","tag-wake-on-lan","tag-wake-up","tag-wol","has-post-thumbnail-archive"],"acf":[],"featured_image_src":"https:\/\/www.creatingsmarthome.com\/wp-content\/uploads\/2022\/02\/ha_turnonoff_remote_pc.png","author_info":{"display_name":"Toni","author_link":"https:\/\/www.creatingsmarthome.com\/index.php\/author\/topsy\/"},"_links":{"self":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/comments?post=929"}],"version-history":[{"count":16,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/929\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/posts\/929\/revisions\/2513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/media\/943"}],"wp:attachment":[{"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/media?parent=929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/categories?post=929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creatingsmarthome.com\/index.php\/wp-json\/wp\/v2\/tags?post=929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}