Setting up Wake-on-lan on Ubuntu Server 22.04 LTS
Published 2 years ago on May 31, 2023

Update:

I originally wrote this post around 2018 for an old blog of the same name under a different domain. It was written for Ubuntu Server 18.04 LTS, however, the content also applies to Ubuntu Server 20.04 LTS and Ubuntu Server 22.04 LTS.




When I work from home, which is almost always, I often find myself working on various devices (MacBook, Windows laptop, Windows desktop) and lately I’ve been working on a heavily database oriented project. Local dev with databases involved isn’t very friendly across multiple machines. You can add your code to VCS but including a database full of test data isn’t my idea of a fun time.

So, long story short, I re-purposed my linux desktop pc from its semi-neglected state to something more useful. A LAN MySql server. Of course, there’s the whole LAMP stack on there just for shigs, though. That, and ease of data management with PMA.

So what is the problem?

Well, I’m lazy. I’m a developer. I’m not going to go to where my server is located and turn it on every time I need to access it, no sir! Nor am I going to leave it on permanently, leeching my hard earned electricity from under my nose. That coupled with the fact that it’s next to my bed and when night sets in, it sounds like an engine next to my face. So, I need to be able to turn it on from my laptops. Easy stuff, wake-on-lan at your service.

The motherboard supports it, awesome, why not. So, I configure the bios to listen for those magic packets and locate a tutorial to enable Ubuntu to cooperate. Enabling wake-on-lan varies from motherboard to motherboard, depending on the manufacturer and the bios firmware installed, so if you need help enabling wake-on-lan for your particular motherboard, you should search “enable wake on lan for <motherboard here>”.

If you’re unaware, generally speaking, when an Operating System shuts down it also powers down the network adapter. This is bad for Wake-on-lan. If it’s not on, it’s not listening!

The solution

The steps for preparing Ubuntu for WoL are as follows:

  1. Enable Wake-on-Lan in BIOS, as above
  2. Install ethtool
  3. Get network interface name with ifconfig
  4. Use ethtool to tell network interface to listen for magic packets
  5. Create a service to re-run this command each on each boot, because it does not persist


Install ethtool

sudo apt-get install ethtool
Ubuntu Server 18.04 LTS already has this installed.

Using ethtool to enable Wake On Lan

Run the command to get the name of your network interface:


ifconfig
Its worth noting here that all the guides I found say that “your network interface is most likely eth0”. The thing is, that’s no longer the case. Ubuntu has been transitioning to systemd since version 15.04 and part of that transition is the implementation of Predictable Network Interface Naming and so you might just find that your network interface name is something along the lines of enp0s15

Now we want to run ethtool, replacing INTERFACE with the interface name you retrieved from the previous command:

sudo ethtool -s enp0s15 wol g
This tells ethtool to tell the network card to listen out for magic packets. The g denotes that it should listen for magic packets.

You may also see this command written like so:
sudo ethtool --change enp0s15 wol g
which does exactly the same thing.

The problem is, this solution isn’t persistent between shutdowns so once the machine is powered down and then booted up again, the network interface configuration is lost.

Making it permanent

On Ubuntu 18.04, you need to create a systemd service as opposed to enabling, creating and/or modifying rc.local as you would’ve done on previous versions.

So, navigate to: /etc/systemd/system and create a new file here called wol.service – you could be more descriptive but I prefer short filenames, I know what wol means here. I use vim for all my terminal based editing so I run this command: sudo vim wol.service to create and begin editing my service file.

Now you need to populate your wol.service file with all it needs to run as a service:
[Unit] Description=Configure Wake-up on LAN [Service] Type=oneshot ExecStart=/sbin/ethtool -s enp35s0 wol g [Install] WantedBy=basic.target


ExecStart provides the command to run. It’s important to note that this must be the absolute path to ethtool because services don’t do relative paths. Check the documentation if you want to understand the file structure more thoroughly, and to find the full path of ethtool just type which ethtool into terminal.

Once you’ve created your file, you need to add it to the systemd services so you should run systemctl daemon-reload to tell the system to update and/or locate new service files, like the one we just created.

Run systemctl enable wol.service to enable the service to run on start up and, finally, systemctl start wol.service to fire up the service.

Conclusion

And there we have it, if you’ve gone through all of that and enabled Wake-on-lan in BIOS, you should be able to power off your machine and then wake it up with a magic packet.

The Magic Packet

I opted to use a python script to send my magic packet, provided by San Bergmans, thank you! I modified it ever so slightly to include my server MAC address, and a name for it and now all I have to type in terminal is:
wol the-lab
and the magic packet is sent, the server fires up and I’m good to go! (I have the script in a directory added to path, some security-oriented people advise against such a practice but convenience is what I thirst for!)