Raspberry Pi as a Wake-On-LAN server

‘Wake-On-LAN’ is a mechanism by which a sleeping computer can be switched on on receipt of a magic packet sent to its MAC address.

This is great for wired networks, but my PC connects to the router wirelessly. This post explains how I used a Raspberry Pi as an intermediary allowing me to issue Wake-On-LAN commands to my PC from anywhere in the world. The Raspberry Pi lends itself well to this task as it is so cheap to run that it isn’t a big deal to leave it switched on permanently.

I want to SSH to the Pi from anywhere on the Internet and send a magic packet to wake the PC it is wired to. This useful thread summarizes it perfectly:

Me => Internet => My router => WiFi => Pi => Wired Ethernet => PC

What you need

  • WiFi capable desktop PC with a motherboard that supports Wake-On-LAN and an Ethernet port
  • Raspberry Pi (any model with an Ethernet port)
  • Raspberry Pi compatible WiFi dongle
  • Ethernet cable

Steps

These steps assume your Raspberry Pi is already connected wirelessly to your router and is accessible via SSH.

  1. Connect the Raspberry Pi directly to the desktop PC using the Ethernet cable
  2. SSH into your Raspberry Pi and edit your interfaces file:
    sudo nano /etc/network/interfaces
    
  3. Modify it as follows. Note that this assumes your router’s address is 192.168.0.1:
    auto lo
    
    iface lo inet loopback
    
    # Configure WiFi LAN
    allow-hotplug wlan0
    iface wlan0 inet static
    address 192.168.0.100
    netmask 255.255.255.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
    wpa-ssid "<wpa-ssid>"
    wpa-psk "<wpa-password>"
    wpa-group TKIP CCMP
    wpa-key-mgmt WPA-PSK
    
    # Configure ethernet LAN
    auto eth0
    iface eth0 inet static
    address 10.0.0.1
    netmask 255.255.255.0
    gateway 10.0.0.1
    up route del default gw 10.0.01 eth0
    up route add default gw 192.168.0.1 wlan0
    
    # Required
    iface default inet dhcp
    up route del default gw 10.0.0.1 eth0
    up route add default gw 192.168.0.1 wlan0
    

    This effectively sets up the Raspberry Pi on two networks – the home network via the WiFi dongle, and a separate network consisting of only the Pi and the desktop PC via the Ethernet cable.

    In doing this we have given it two static IP addresses: 192.168.0.100 for the home network, and 10.0.0.1 for the wired network. These addresses can be changed to anything you like.

  4. Now we need to configure the desktop PC to be able to talk to the Pi.
    • In Windows, open Network Connections (on Windows 8.1, right click the Start icon -> Network Connections) and right click your Ethernet connection and choose Properties.
    • Scroll down the list and select Internet Protocol Version 4, then click Properties.
    • Select ‘Use the following IP address’ and enter an IP address on the same subnet as the Pi’s wired IP address. I used 10.0.0.2.
    • Set ‘Subnet mask’ to 255.255.255.0
    • Set ‘Default gateway’ to the Pi’s wired IP address (10.0.0.1 in my case)
    • Leave the DNS server entries empty
  5. The Pi and the PC should be able to see each other, and you should be able to ping one from the other. If not, try rebooting both the Pi and the PC. At this point I found that I could now not access the Internet from the Pi. In my case, the problem was that the DNS settings were wrong. I managed to fix this as follows:
    • Edit the resolv.conf file:
      sudo nano /etc/resolve.conf
      
    • Ensure the nameserver entry points to your router’s IP address:
      nameserver 192.168.0.1
      
  6. You may need to reboot the Pi again, and you should now be able to access the Internet and the PC from the Pi. Almost there, now we can install what we need to issue the Wake-On-LAN command.
    • On the Pi, install the WakeOnLAN package:
      sudo apt-get install wakeonlan
      
  7. Now you can test it out:
    • Put the PC to sleep
    • Access the Pi via SSH from another device
    • Issue the wakeonlan command passing in the IP and MAC address of the PC’s wired Ethernet connection, e.g:
      wakeonlan -i 10.0.0.2 "11:AA:22:BB:33:CC"
      

      …and the PC should come whirring to life!
      It would be a good idea to set up a script to execute this command to avoid having to remember IP and MAC addresses.

  8. We can now wake up the PC from any device on the network. The final piece of the puzzle is to enable access to the Pi via the Internet. There are many guides on setting up port forwarding to enable this, however I couldn’t be bothered with all that and instead used it as an opportunity to try out Weaved – a service that allows secure access to devices via their website.

And that’s it! No more having to leave my PC on wasting energy and money just on the off chance I might need to access it.