Introduction to Wake On Lan

Wake-on-lan (also known with the “W.O.L” acronym) is a standard ethernet feature which allows a machine to be woken up on the reception of a specific type of network packet (the so called MagicPacket). The main advantage of this feature is that it allows us to keep a machine in a low power consumption state, and be accessed only when needed. In this tutorial we see how to enable the WOL feature on our ethernet card under Linux, and how to send the network packet needed to wake up a machine.

In this tutorial you will learn:

  • How to check if a network card supports the Wake-on-Lan feature
  • How to enable Wake On Lan using the ethtool utility on Linux
  • How to create a udev rule to enable Wake On Lan on boot
Introduction to Wake On LAN
Introduction to Wake On Lan

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software ethtool
Other Root privileges
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Check if the Wake On Lan feature is supported

If we want to use Wake On Lan, the very first thing we have to do is to make sure the option is supported by the network interface we want to use to wake up our machine by the machine BIOS or UEFI firmware. To do the latter, we must enter the firmware settings interface when the machine starts (this is typically achieved by pressing a key such as F2 just after turning power on). Inside the firmware interface, the feature, if supported, is typically listed under the “advanced” section (search something like “PCI Device Power On”).



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


After we enabled WOL in our machine firmware, we need to make sure that our network interface card actually supports it. This is actually a really simple to task. All we have to do is to use the ethtool utility. We invoke it with administrative privileges and without any specific option, just passing the NIC name as argument. In my case the name assigned to the ethernet interface is ens5f5, so I run:

$ sudo ethtool ens5f5

The command returns the following output:

Settings for ens5f5:
    Supported ports: [ TP    MII ]
    Supported link modes:   10baseT/Half 10baseT/Full
                            100baseT/Half 100baseT/Full
                            1000baseT/Half 1000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Supported FEC modes: Not reported
    Advertised link modes:  100baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Advertised FEC modes: Not reported
    Link partner advertised link modes:  10baseT/Half 10baseT/Full
                                         100baseT/Half 100baseT/Full
    Link partner advertised pause frame use: Symmetric Receive-only
    Link partner advertised auto-negotiation: Yes
    Link partner advertised FEC modes: Not reported
    Speed: 100Mb/s
    Duplex: Full
    Auto-negotiation: on
    Port: MII
    PHYAD: 1
    Transceiver: internal
    Supports Wake-on: pg
    Wake-on: d
        Current message level: 0x000020c6 (8390)
                               probe link rx_err tx_err hw
    Link detected: yes

The relevant line of the output we want to take a look at in our case, is Supports Wake-on: pg and Wake-on: d. Here the letter resembles the feature status. Let’s focus on what those lines means. The value associated with the “Supports Wake-on” key, reports what types of Wake-on method are supported:

Letter Meaning
p Wake on phy activity
u Wake on unicast messages
m Wake on multicast messages
b Wake on broadcast messages
a Wake on ARP
g Wake on MagicPacket(tm)
s Enable SecureOn(tm) password for MagicPacket(tm)
d Disable




The current status of the Wake On Lan feature, instead, is reported under the “Wake-on” key. In this case we can see that it is currently disabled (“d”).

Enabling Wake On Lan

Once we established the Wake On Lan feature if supported by our network card, we can proceed to enable it. How can we do this? All we have to do is to use the ethtool utility and run the following command:

$ sudo ethtool -s ens5f5 wol g

You can notice in the example above we launched ethtool with the -s option. This is the short form for --change, and, as its name suggests we have to use each time we want to perform a change on the network interface we pass as its argument.

What we did in this case was to enable Wake On Lan in g mode, since we want to use a MagicPacket to wake up the system remotely.To verify the change was applied correctly, we can check the WOL status again:

$ sudo ethtool ens5f5 | grep Wake-on
Supports Wake-on: pg
Wake-on: g

Making the change persistent using a udev rule

Enabling the Wake On Lan feature the way we did it’s not enough, since the change will not persist a machine reboot. We must find a way to enable the option automatically at boot. There are many ways we can do this. For the sake of this tutorial we will create an udev rule which will run the appropriate command once the network interface is detected.

To write our udev rule, the first thing we must take note of, is the MAC ADDRESS of the network interface which will send the MagickPacket to, which in this case is ens5f5. Obtaining the address of an interface is really simple, all we need to do is to run the following command:

$ cat /sys/class/net/<interface-name>/address

Where the <interface-name> placeholder in the example above, should be replaced by the actual name of the network interface. We will base our udev rule on the MAC ADDRESS of the interface, so to be sure that it will be applied on that device only. Here is what our rule looks like:

ACTION=="add", ATTRS{address}=="XX:XX:XX:XX:XX:XX", RUN+="/usr/sbin/ethtool -s  wol g"

The rule will be applied on the “add” event, on the device which matches the given MAC ADDRESS (substitute it with your actual address in the rule). Once udev finds a match for the rule, the given command will be executed enabling the Wake-on-lan feature automatically.

Sending the MagickPacket to wake the device

We saw how to activate the Wake On Lan feature manually, and how to write a udev rule so that the appropriate command is executed automatically each time the network interface is detected. Now, all we have to do is find a way to actually send the MagickPacket to our network interface, when the machine is turned down.

First of all, the machine we are using to send the packet must be in the same subnet of the target one. It is actually possible to send a packet from a different subnet or from the internet, but the router must support the feature and must be configured to broadcast the MagicPacket.




The program we can use to send the MagicPacket varies depending on the distribution we are running. On Fedora, and more generally on the distributions which are part of the Red Hat family, all we have to do is to install the net-tools packages (it is probably installed by default) which includes the ether-wake utility:

$ sudo dnf install net-tools

If we are using Debian, or one of its many derivatives, we can install and use the etherwake utility:

$ sudo apt-get update && sudo apt-get install etherwake

If Archlinux is our favorite distribution, instead, we can install and use the wol utility to send the MagicPacket:

$ sudo pacman -Sy wol

All the mentioned applications work basically in the same way. All we have to do is to invoke them and pass the MAC address of the interface we want to send the MagicPacket to as argument, for example:

$ etherwake XX:XX:XX:XX:XX:XX

Conclusions

In this tutorial we saw what the Wake On Lan feature is, and how can we use it to remotely turn on a device by sending a MagicPacket. We saw how to verify the feature is actually supported in our machine firmware and on the network interface using the ethtool utility, how to activate it manually and how to write a udev rule to re-activate it automatically each time the interface is detected. Finally, we saw how to actually send a MagicPacket using applications as etherwake or wol.



Comments and Discussions
Linux Forum