Using Systemd To Enable Wake-On-Lan

What is this about?

I made a previous post (Enabling Wake-On-LAN (In Ubuntu 20.10)) about enabling Wake-On-LAN on my server, but it didn't seem to work after a re-boot. I looked in the logs using journalctl and noticed that the wol.service that I set up didn't get enabled after the re-boot. I thought about it and I remembered that when I was working with bluetooth and set up a service for it I had to make it wait for the bluetooth service to start first or it wouldn't work and realized that there was nothing in the wol.service file to tell it to wait so I went poking around and found what I hope to be a fix.

So, how do you do it?

We need to update the /etc/systemd/system/wol.service that I created in that earlier post. Previously it looked like this:

[Unit]
Description=Enable Wake On Lan

[Service]
Type=oneshot
ExecStart = /sbin/ethtool --change enp4s0 wol g

[Install]
WantedBy=basic.target

We need to update the [Unit] section by adding

Requires=network.target
After=network.target

To let systemd know that we don't want this to run until after the network service has run. I also changed the WantedBy to multi-user.target. So the updated file looks like this:

[Unit]
Description=Enable Wake On Lan
Requires=network.target
After=network.target

[Service]
Type=oneshot
ExecStart = /sbin/ethtool --change enp4s0 wol g

[Install]
WantedBy=multi-user.target

Now reload the daemon and re-enable it.

sudo systemctl daemon-reload
sudo systemctl enable wol.service

To make sure I didn't introduce any errors I ran it once.

sudo systemctl start wol

Then I rebooted it.

sudo reboot now

This time in journalctl I saw this:

Dec 07 00:02:17 erebus systemd[1]: Finished Enable Wake On Lan.
Dec 07 00:02:17 erebus systemd[1]: wol.service: Succeeded.

And running ethtool showed that it was enabled.

Wake-on: g

So, That Fixed It?

Well, insofar as it's working now, yes. But since I change more than one thing I don't really know exactly what did it (was it one of the three lines I changed or all of them?). Anyway, since it works I'll let it lie for now.

Where did you find out about this?

  • The Arch wiki has a page on Wake-On-LAN that tells you many different ways to set it up – I copied the systemd file almost exactly. It has quite a bit of useful information (too much maybe), but not all the commands will work out of the box on Ubuntu (because it's Arch).