Remote PC Startup/Shutdown

Startup

To startup a computer remotely, you will need Wake-on-LAN properly configured. Wake-on-LAN (WoL) is an Ethernet or Token ring computer networking standard that allows a computer to be turned on or awakened by a network message.

You will need the wakeonlan command and the MAC address of the computer that you want to wakeup. Then, you can run the command:

wakeonlan xx:xx:xx:xx:xx:xx

Shutdown

Now, we will show how to configure the remote computer using SSH.

Start by creating a user called poweroff (or other name that you like). Create a user with a temporary password, it will be removed later:

sudo adduser poweroff

Edit the file /etc/sudoers to allow the user to run the command poweroff. Add the following line at the end of the file:

poweroff ALL=NOPASSWD: /sbin/poweroff

Create a file in poweroff’s home called poweroff (sudo vim /home/poweroff/poweroff). Add the folling lines:

#!/bin/bash
sudo /sbin/poweroff -f

Save and add execute permissions to the file:

sudo chmod +x /home/poweroff/poweroff

Then, copy the SSH Key from the server and add it to authorized_keys:

sudo mkdir /home/poweroff/.ssh
sudo scp user@server:/var/www/.ssh/id_rsa.pub /home/poweroff/.ssh/authorized_keys
sudo chown -R poweroff:poweroff /home/poweroff/.ssh

Finally, remove the user’s password from shadow file (sudo vim /etc/shadow). Locate the corresponding line to the poweroff user and replace the password hash by *. Example:

poweroff:*:16816:0:99999:7:::

Then, create a SSH Key to be used to establish the connection and add it to authorized_keys:

sudo ssh-keygen -f /home/poweroff/.ssh/id_rsa
sudo cp /home/poweroff/.ssh/id_rsa.pub /home/poweroff/.ssh/authorized_keys