Add systemd service to Start Wake On LAN

In a previous tutorial we looked at how you can setup Wake on LAN (wol) on RHEL7 or CentOS7. In this tutorial we take another look at wol by adding a UDEV rule and systemd service, because the ETHTOOL_OPTS is deprecated.

Deprecated ETHTOOL_OPTS

If we take a look at /usr/share/doc/initscripts*/sysconfig.txt and search for ETHTOOL_OPTS we will find the following:


Deprecated, but supported:
    ETHTOOL_OPTS=...
      Any device-specific options supported by ethtool. For example,
      if you wanted to force 100Mb full duplex:
        ETHTOOL_OPTS="speed 100 duplex full autoneg off"
      Note that changing speed or duplex settings almost always
      requires disabling autonegotiation with 'autoneg off'.

      Multiple options can also be set like so :
      ETHTOOL_OPTS="-K ${DEVICE} tso on; -G ${DEVICE} rx 256 tx 256"

      Long term, this should be done by sysadmin-written udev rules.

As you can see the option ETHTOOL_OPTS=”-s ${DEVICE} wol g” in the NIC configuration file (/etc/sysconfig/network-scripts/ifcfg-eth0) as we have explained in the previous tutorial is deprecated, but it is still supported. They say it’s still supported, but on many RHEL7 or CentOS7 systems it looks like the option is not picked up at boot time, resulting in a not configured Wake on LAN NIC. If you look with ethtool the NIC option for wake-on is d after reboot. So we need a new method to enable it.

UDEV Rule

We can add a UDEV rule for the network device so it will be picked up when the NIC comes online. First find out the name of you NIC:


cat /proc/net/dev
enp0s25: 23399505   25169    0  0  0  0  0 2  2716146 20636  0 0 0 0 0  0
    lo:   12772     144    0  0  0  0 0  0 12772 144 0 0 0 0 0  0

So in my case the NIC is called: enp0s25
We now add a udev rule for this device, if the device is online we want to start our systemd wakeonlan.service:


# cd /etc/udev/rules.d
# vi 99-wakeonlan
KERNEL="enp0s25", ACTION=="online", PROGRAM="/bin/systemctl start wakeonlan.service"

Creating a systemd service wakeonlan

Create a systemd script called systemd-wakeonlan that is called by our service to stop and start the service:


# vi /usr/lib/systemd/systemd-wakeonlan
#!/bin/sh

# only usable for root
[ $EUID = 0 ] || exit 4

start() {
        ethtool -s enp0s25 wol g
}

stop() {
        ethtool -s enp0s25 wol d
}

case "$1" in
        start|stop) "$1" ;;
esac

Make the script executable:


# chmod +x /usr/lib/systemd/systemd-wakeonlan

Now we need to add the service file:


# vi /usr/lib/systemd/system/wakeonlan.service
[Unit]
Description=Configure Wake-up on LAN

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/lib/systemd/systemd-wakeonlan start
ExecStop=/usr/lib/systemd/systemd-wakeonlan stop

[Install]
WantedBy=basic.target

We need to enable our service so it will also work after reboot (chkconfig was the command on RHEL6):


# systemctl enable wakeonlan.service
ln -s '/usr/lib/systemd/system/wakeonlan.service'\
'/etc/systemd/system/basic.target.wants/wakeonlan.service'

Now that the service is enabled it is possible to stop and start the Wake on LAN service using systemctl.


# systemctl stop wakeonlan.service
or
# systemctl start wakeonlan.service
or
# systemctl status wakeonlan.service

If you’ve done everything right the service will start without any warning or error. Now reboot your system and check after reboot using the ethtool if the service is automatically started at boot time.


# ethtool enp0s25

The Wake-on option should in our case be g (from wake on magicpacket).

That’s all for this tutorial on Wake on LAN using a UDEV rule and systemd service.

This entry was posted in Linux Administration. You can follow any responses to this entry through the RSS 2.0 feed. You can trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “Add systemd service to Start Wake On LAN”

Why not let us know what you think by adding your own comment!

  1. Bill Maidment on May 24th, 2015:

    There is a problem with this solution. When you do “poweroff” there is a call to “systemd-wakeonlan stop” which sets the NIC to “Wake on d” before powering off.
    Commenting out the ExecStop line in wakeonlan.service gives persistent “Wake on g”.
    There is probably a better solution, but I am not yet knowledgeable enough in the ways of the systemd force to work that one out.

  2. Marc Schindler on November 8th, 2020:

    In RHEL8 it’s easy to configure with one command:

    https://access.redhat.com/solutions/2127401

    # nmcli con mod enp2s0 802-3-ethernet.wake-on-lan 8 nmcli con show enp2s0 | grep wake
    802-3-ethernet.wake-on-lan: multicast
    802-3-ethernet.wake-on-lan-password: —

Leave a Reply: