Once your server is installed and set up, it is ready to be packed away somewhere out of the way, i.e. in an attic, a basement or a press. From now on, all of your interactions with it will be across the network, either through webpages, SSH sessions or maybe viewing movies on other devices. But these are all done while it is running. What if you want to turn it on or off to begin with? Luckily this can all be done remotely also.
The Wizardry of Magic Packets
Wake-on-LAN (abbreviated as WOL) is an industry standard protocol for waking computers up from a very low power state remotely.
Wake on LAN is a feature whereby you are able to send a special packet, or message, to your server while it is turned off that will tell it to turn itself on. It requires both your motherboard and Network Interface Card (NIC) support it. When a server is turned off, it is actually put into a low power state, but there is enough power being drawn for your NIC to monitor the network, and what it is looking for specifically, are Magic Packets.
A Magic Packet is a specially designed network data packets which will prompt a server to awaken from its shutdown. Each NIC has a unique address coded into it, called a Media Access Control address, or MAC address, and no other NIC in the world contains that MAC address. Magic Packets contain a servers MAC address so you are able to specify which server you want to wake from its sleep.
Waking a Sleeping Server
To check the WOL settings on your server NIC, you need to use a tool called ethtool. This is most likely already installed as part of Ubuntu Server. If not, you can install it via, sudo apt-get install ethtool. Ethtool allows you to query and control network device driver and hardware settings. The first thing we are interested in is whether Wake On LAN is already enabled on our NIC. You will need the device name of your NIC. To get this run ifconfig:
On this device I have one network interface, enp3s0, and one loopback interface, lo.
To check the settings on my NIC then, I run
sudo ethtool enp3s0
There is a lot of information there about the configuration of the network settings, but what we are interested in is the Wake-on setting down at the bottom. What you want to see after it is the ‘g’ setting, which means it is configured to respond to Magic Packets. If there is anything other than ‘g’ there, you will need to set this by typing
sudo ethtool –s <your_NIC> wol g
Check again to ensure that your NIC is now showing a ‘g’. Reboot the server (sudo reboot) to ensure that it is still set to ‘g’ when it comes up again.
Making it Sticky
When I was running Ubuntu 14.04, that was all it took to set Wake On LAN. I set it in ethtool, the Wake-on setting changed to ‘g’ and that was that. And it worked for years. I found though that when I upgraded to Ubuntu 18.04, I wasn’t able to wake up the server via the network. After manually powering it on, I checked the ethtool settings and found that, instead of ‘g’, the Wake-on setting was now ‘d’, which disabled all WOL settings. Obviously not what you want.
I re-enabled it, rebooted (sudo reboot), and checked again, only to find that it was again set to ‘d’. Why? Same motherboard, same NIC, only difference was the operating system. I understand from reading forum entries that it has something to do with Ubuntu moving to netplan for the networking.
In order to get this to stay enabled, I added a line to my rc.local script, to run the ethtool command each time the server boots (Use the which command to get the correct and full location for the ethtool command).
#!/bin/sh –e /sbin/ethtool –s enp3s0 wol g exit 0
On Ubuntu 18.04, you will also need to edit your networking config file, at /etc/netplan/xxx.yaml (replace xxx with the name of your .yaml file), to add your MAC address details. We covered this file before when assigning a static IP address to your server. To get your MAC address, run the ifconfig command again. It is the 12 digit alpha-numeric string of text after ‘ether’, i.e. ether a0:b1:c2:d3:e4:f5.
cd /etc/netplan sudo cp xxx.yaml xxx.yaml.BACKUP sudo nano xxx.yaml
then add the ‘match: macaddress’ and ‘wakeonlan’ lines:
network: version: 2 ethernets: enp3s0: addresses: [192.168.178.160/24] gateway4: 192.168.178.1 nameservers: addresses: [192.168.178.1, 188.8.131.52, 184.108.40.206] match: macaddress: aa:bb:cc:dd:ee:ff wakeonlan: true
Take note of the indenting and spelling as this is very important to netplan. Once complete, test your netplan script before powering down your server. If it is acceptable, there will be no message. If something in the script is wrong, netplan will print an error message telling you the line it has a problem with. Some quick Google searching should help fix any issues.
sudo netplan apply
Booting Your Server from Your Phone
Once your server is configured to boot remotely, the next step is actually doing so. From a laptop, there are a number of programs which will be able to do this. My own favourite way, is to boot the server from my phone. Search the Apple App Store or Google Play Store for Wake On LAN apps to find one you like. Personally I use one called Mocha WOL on iPhone.
When setting up the app, the most important thing you will need is the server MAC address. Some apps require the server IP address also, but this is not really relevant as Magic Packets only use the MAC address, and because the server is shut down at the time, it is not using its network address. Port number is often set to 7 or 9, and you can leave this at its default settings.
Once you have it all set up, and MAC address is correct, test it by shutting down the server (sudo shutdown –Ph now) and try start it up again.