The problem

My server can be interrupted by a blackout or even a single power switch, which is not very often, but quite annoying. It is not easy from me go to the machine and press the button.

Solutions

  • UPS

    Highly recommended if you have $$$, see here (Amazon) for some suggestions.

  • Turn on with AC power in BIOS

    This depends on the motherboard in your computer, some may not support that. If so, please read option 3.

  • ==WakeOnLan== in Linux

    This is what we are going to do here. More references can be found at here

    • Hardware support

      Also need to make sure your network card and motherboard support some type of Wake On LAN (WOL) feature. How? Reboot your computer and go to the UEFI/BIOS, find an option labelled “Wake On LAN”, or something like that under either boot, power management or network. If you do not have that option, there may be another power management or network option that enables WOL. Even you can not find anything, it is still possible that your network card supports that. Let’s have a look in next step – software activating part.

    • Software activation

      First check if WOL functionality is already activated, using ethtool (install it, if command not find) like this: sudo ethtool eth0. Note, your network card may not named as eth0, use ifconfig to check its name. The output of sudo ethtool eth0 will print out something like Wake-on: g. g means it is enabled. If not, let’s activate it in different ways: ifupdown, ethtool, systemd networkd or NetworkManager.

      • ifupdown

        Add an interface config file /etc/network/interfaces.d/eth0 (or modify the global interface config file /etc/network/interfaces):

        auto eth0
            iface eth0 inet dhcp
            ethernet-wol g
        

        Then, reboot the system to activate it.

      • ethtool (recommended)

        Just one simple line: sudo ethtool -s eth0 wol g. Again, Replace eth0 with your network interface device name. Note this works fine for CentOS stream 9, but not for Ubuntu! I have tried multiple ways to make Ubuntu to work but failed… Here are some modifications that I have done:

        • adding this line wakeonlan: true to the netplan configure file at /etc/netplan/00-installer-config.yaml:
            # This is the network config written by 'subiquity'
            network:
          renderer: NetworkManager
          ethernets:
            enp2s0:
              addresses:
              - x.x.x.x/y
              nameservers:
                addresses: [x.x.x.x, x.x.x.x]
              routes:
              - to: default
                via: x.x.x.x
              wakeonlan: true
          version: 2
          
        • follow the standard instruction from Ubuntu to start the WOL service for systemd. Here is the file in “/etc/systemd/system/wol.service”:
          [Unit]
          Description=Enable Wake On Lan
        
          [Service]
          Type=oneshot
          ExecStart = /usr/sbin/ethtool --change enp2s0 wol g
        
          [Install]
          WantedBy=basic.target
        
      • systemd-networkd

        You can check if your network is managed by systemd-networkd by networkctl list command. I haven’t try this in CentOS, but failed to configure this in Ubuntu for it changed to netplan.

        Set the WakeOnLan field to one of the available options in the [Link] section of the .link file for the network interfaces you want to use WOL on:

        [Link]
        WakeOnLan=magic
        

        Beware that only the first .link file is applied and that there is a 99-default.link. The name needs to be lexicographically smaller. Then, activate it:

        sudo networkctl reload
        sudo networkctl reconfigure
        
      • NetworkManager

        You can check if your network devices are managed with nmcli d command.

        NetworkManager supports WOL since version 1.0.6 and you can enable it from either your desktop network configuration GUI, or the nm-connection-editor GUI from nm-connection-editor, or from the nmcli command-line tool using this command:

        sudo nmcli c modify "wired1" 802-3-ethernet.wake-on-lan magic

  • Use WOL

    It is very easy, simply do this in another computer’s terminal: sudo etherwake <mac address> or wakeonlan <mac address>. Note the mac address is the one of your network card that you want to wake up. I found the second software wakeonlan works very well. Note that I have to use wakeonlan -i ip_address <mac address> in Ubuntu to get the signal sent to my host machine. Though it reported as received the magic packet by tcpdump, but it just won’t wake the machine when it is done. I think it is because the netcard doesn’t get power on, its light is not on, even the machine is in hibernate mode…

  • Trouble shooting

    please refere to the debain’s wiki page for WakeOnLan or Google it. Note that much of these content is also covered on the ArchLinux wiki.