Wake On Lan with a Raspberry Pi

Juliana Mei
8 min readMay 15, 2022

--

This weekend, I worked on a project to remotely trigger a desktop to wake up!

It uses a magic packet called Wake On LAN. It “allows people to remotely power on a computer or to wake it up from sleep mode”

Magic packet

The magic packet is a frame that is most often sent as a broadcast and that contains anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal), followed by sixteen repetitions of the target computer’s 48-bit MAC address, for a total of 102 bytes.

Since the magic packet is only scanned for the string above, and not actually parsed by a full protocol stack, it could be sent as payload of any network- and transport-layer protocol, although it is typically sent as a UDP datagram to port 0 (reserved port number),[6] 7 (Echo Protocol) or 9 (Discard Protocol),[7] or directly over Ethernet as EtherType 0x0842.[8] A connection-oriented transport-layer protocol like TCP is less suited for this task as it requires establishing an active connection before sending user data.

A standard magic packet has the following basic limitations:

  • Requires destination computer MAC address
  • Does not provide a delivery confirmation
  • May not work outside of the local network
  • Requires hardware support of Wake-on-LAN on destination computer
  • Most 802.11 wireless interfaces do not maintain a link in low power states and cannot receive a magic packet

The Wake-on-LAN implementation is designed to be very simple and to be quickly processed by the circuitry present on the network interface card with minimal power requirement. Because Wake-on-LAN operates below the IP protocol layer, IP addresses and DNS names are meaningless and so the MAC address is required.

TLDR: A magic packet is a string of numbers and letters in a pattern that computers recognize to mean “wake up.”

I went through several tutorials online and the writeup awfully assumed you were an experienced network professional! So I’m writing this to hopefully fill in the gaps I needed to get the job done the easy way. And the raspberry pi is $200 right now 👀 — the supply chain is bonkers.

Without further ado, here’s the tutorial!

The network map we are going to achieve

Set up Raspberry Pi

Let’s begin by setting up the raspberry pi. Why are we using a pi? The raspberry pi will be like a computer that we can ssh into from anywhere. The pi will have a library downloaded called Etherwake that can send magic packets.

Flash the Raspberry Pi

The raspberry pi runs off a microSD card with the operating system on it. We will download the operating system image and then use a software to flash the image onto the card.

  1. The image I used is called Raspbian Jessie lite. Click to download the zip. When it has downloaded, unzip the file. It should be several gigabytes large.
  2. The software I used to flash is called Balena Etcher. Click the link to download. (I’m using Balena over the official Raspberry Pi Imager because it corrupted my SD card and I ended up using Balena anyway to re-flash. Maybe it’ll work for you! 🤷‍♀)
Balena Etcher screenshot

The SD card is loaded with a copy of Jessie OS.

Before you pop the loaded SD into the Pi, we will need to enable SSH on it. Plug the SD card into your computer and make two files. Plug the SD card back into your computer until you see it pop up. (If it doesn’t pop up, it might be corrupted and you will need to re-flash.) Let’s make two files. Make a file named ssh all lowercase and no extensions. The second file is called wpa_supplicant.conf Copy and paste the stuff below, fill it with your wifi login.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid="your wifi name here"
psk="your passwork here"
key_mgmt=WPA-PSK
}

If you have trouble, reference my write up on how to setup ssh on your raspberry pi here.

Pop the SD card into the pi and turn the pi on! The pi will connect to your wifi.

Assuming you’re on a mac, open the terminal app. (if you’re on Windows, you can use something like Putty. I will not cover the scope of Windows users.)

In the terminal, type in ssh pi@raspberrypi.local the default password is raspberry If you cannot successfully ssh into the raspberry pi, check the wifi name and password, make sure the wifi is 2.4gHz, check the power supply has 2.5A output, log into the router and check for connected devices, use nmap to scan the network to look for connected devices. If none of that works, use google :)

Once ssh’d into the pi, type in sudo apt-get etherwake to install etherwake onto the pi.

Then create a file called "wol.sh” with touch wol.sh Inside this file, we will write a small script to use etherwake. Run nano wol.sh to edit the file, copy and paste the snippet below into the file. Replace “AA:BB:CC:DD:EE:FF” with the MAC address of your computer!

#!/bin/bash# Replace "AA:BB:CC:DD:EE:FF" with the MAC address

echo "Going to wake up your PC..... <3"
sudo etherwake -i wlan0 AA:BB:CC:DD:EE:FF

One last thing, run chmod +x wol.sh to make the script executable. To confirm, run ls -la, you should see something like this:

-rwxr-xr-x 1 pi pi 364 May 15 02:36 wol.sh

Network Setup

Great! The pi is all set up. The next step is to do a couple simple things in your home network. You’ll need admin access to the router. To get to the admin panel, it’s usually something like ‘http://192.168.1.1/’ You can google the router brand to find the right URL.

Put the raspberry pi on a static ip

Every time a new device joins your wifi, your router has to be the DHCP which is like an “address-giver” The DHCP gives your device a temporary address. That is your device’s IP and is effectively a pointer. What we have to do is make sure that the pointer doesn’t change for the raspberry pi. We must make the IP static, the router should recognize the device and always assign it the same IP.

In the network admin panel, go into the advanced setting. This was my setting in Netgear. I “reserved” the IP 192.168.1.9 so that every when the pi turns off and reconnects to the router at another time, it will always be at the same predictable address.

Port Forwarding

Now that your pi is on a static IP, we need to set your pi up for port forwarding. What that means is that we are going to be able to log into the raspberry pi from anywhere in the world — like going to a website.

In the admin panel, set up port forwarding. This was my setting in Netgear. We are matching port “22” from two IPs: your external IP and the internal IP of the raspberry pi. So that when I try to log into your external IP on port 22 — from anywhere in the world — it will be the same as if I am logging into the raspberry pi 😁

Get a Domain to your network address

What does that mean? We’re going to get you a website URL that will point to your home wifi. Yes, that’s right. Sign up for noip. It’s a domain name service similar to buying your own website address — except it points to your wifi’s network IP. Your network IP is your address in the world wide web of the internet — it is external facing. To find your external facing IP, go to google and type in “What is my IP.” Your router will change it every two weeks. Your router will sent a message to your ISP (i.e Google fiber or spectrum) and your ISP will send back a new IP for you.

This is a problem because, in order to run a script on a raspberry pi in your network, you’ll need to know your external IP. That’s why we are going to sign up for noip which will point a domain name url that is easy to remember (mine is sudobangbang.ddns.net) to your external IP.

By the way, if your router is set up so that it doesn’t rotate the external IP, then you may skip this step. You can find out by going to the admin panel. The screenshot below has the setting to “Use Static IP” and “Get Dynamically from ISP.”

You can also set your router to static and skip this step entirely. Assuming you still get your internet visa’s dynamically, how does noip map your ever-rotating IP to a domain name? Well, noip has a software that sits on the network and checks if the IP is changed, then it will notify noip so that your domain name is always up to date. We will install it onto the raspberry pi. While SSH’d into the pi, follow this url: https://www.noip.com/support/knowledgebase/install-ip-duc-onto-raspberry-pi/

We are near the end.

Set Computer to Allow Wake on Lan

Back at your chunky desktop, there’s a setting you’ll have to flip on. It’s called Wake on Lan. Both Apple and Windows machines usually have this feature. It’s the ability for a special packet to trigger the computer to wake up from sleep mode. The packet is called the Magic Packet.

Pull the Trigger, Dora.

Finally, we’re at the final stage.

With all the above set up in place like a lined up wall of dominoes, we are going to get one final tool. On your mobile device, whether android or apple, download an SSH client. I like to use JuiceSSH or Termius. We are going to SSH into the pi (from anywhere in the world) and call the script we wrote “wol.sh” to send a magic wake-up packet to your desktop. 🔥 One and done, baby.

Below is a screenshot from my juicessh. The address is your noip url, the port is 22 (which is the normal SSH port), and since you’re logging into the raspberry pi, the username is “pi” and the default password is “raspberry.” Since this is open to the public world, change your raspberry pi password 🛑Because a chaotic crawler bot from the dark webs will find your external ip and gain access to your network through your weak raspberry pi login. Change your pi password by ssh-ing your your pi and typing in passwd you will be prompted to type in your current password, then the new one twice. Let’s hope you heard me 😆

You should see a tiny window of your raspberry pi terminal! 😍

In the terminal, type in ./wol.sh to run the wol script. Your desktop will be awake ☕️ 👀 ready for the work day ahead of it. You’re free to work from anywhere, whether it is the beaches of Hawaii or the couch next to your special one, don’t compromise the quality of your work for limited computing power. Use your desktop — from anywhere.

— Juliana 🌻

--

--

Juliana Mei

Software Engineer — Blockchain, Cybersecurity, and Commercial Space