System Tech
Wake on LAN in Ubuntu
by Benjamin on Aug.24, 2009, under System Tech
I have a small server lab racked up in my basement. It’s very useful for testing out new applications and configurations, but I don’t need it to run all of the time. They generate a lot of heat, a lot of noise, and most annoyingly a lot of power usage. This means that I only turn these servers on when I need them. That’s great, but I don’t want to have to visit the basement every time I’m looking to bring a few extra servers online. This is the future! And what better way is there to be futury than by using technology to help us be lazier?
There are a few solutions to this problem. I could go drop some money on a remotely accessible PDU. While they offer some significant advantages such as built in fuses and circuit breakers, per circuit power monitoring, and remote on/off toggle capabilities, they are also expensive. And this is my basement we’re talking about. Maybe I would spend that kind of money if I was a millionaire. Remember Tony Stark’s basement-garage-museum-shop-laboratory-office? That was totally awesome. I would spend some extra cash to put some PDUs in there.
Instead, I’ve implemented a technology called Wake on LAN or “WOL”. I’m going to use the acronym now, because it’s hip and because it’s lazy. WOL is implemented in the network card and usually also enabled in computer’s BIOS. The network card monitors traffic on the network while the machine is powered off. In particular, it listens for a specific command in what is known as a Magic Packet. The Magic Packet is a broadcast frame with the mac address of the server inside of it. The broadcast frame is sent to all of the machines on the network. When the network card sees the packet, it sends a signal to the motherboard to wake up. Wake up little server, Wake up!
To enable Wake on LAN, first make sure that your computer supports the feature. Computers with network cards built in to their motherboard should support WOL by default, but it may not be enabled in the BIOS. Newer computers built this century with PCI network cards will probably support it as well. Read your manual. It misses you.
Once the BIOS is ready for WOL, you will need to install ‘ethtool’. This tool is what we will use to enable WOL on the network card itself.
benjamin@srv:~$ sudo apt-get install ethtool
Once ethtool is installed, we can test WOL functionality. First, however, we need to record the machine’s mac address. We will need this later to wake the machine up again. The mac address is outlined in red below. Ethtool offers a number of WOL options, but in this case we will just tell the network card to look for regular magic packets. Check out ‘man 8 ethtool‘ for more options if you insist on being different like everyone else.
benjamin@srv:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 100
link/ether 00:06:5b:f1:1d:56 brd ff:ff:ff:ff:ff:ff
benjamin@srv:~$ sudo ethtool -s eth0 wol g
If you don’t encouter any errors, the WOL functionality of the network card should be ready to go. The only thing left to do is shut down the computer. One caveat to remember is that WOL functionality only works when the computer is properly shut down. Don’t just pull the plug. Would you want someone to pull the plug on you? In 20 years, they might have the technology to wake you up again! In this case, since the whole point is to avoid walking to the basement, I’ll power down the machine remotely.
benjamin@srv:~$ sudo shutdown -P now
Now you have a silent, power sipping computer. This is nice, but it’s also off. Can’t do much with a computer like that. So let’s turn it back on. Firstly, you need to a method to send the magic packet to your server. To do this, we will install ‘wakeonlan’ on an Ubuntu client computer. If you aren’t using an Ubuntu client computer, you are forgiven. I kid! Everyone knows that all operating systems are created of raw angel flatus and sculpted with the finest care by blind Peruvian monks who exist soley on bread and the tears of fallen SysAdmins. If you want to know how to send magic packets from your Angelic Host, ask Google. It misses you.
benjamin@clnt:~$ sudo apt-get install wakeonlan
That’s it. Now we can use wakeonlan to send a magic packet to the mac address of the remote computer we want to wake up.
benjamin@clnt:~$ wakeonlan 00:06:5b:f1:1d:56
At this point, your computers should begin booting. Don’t you wish you could have someone send YOU a magic packet in the morning? They could whisper a magic packet into your ear, and on command you would dutifully jump out of bed and begin shaving your teeth. Wouldn’t that be fun?
Now this is great, but there is a catch. The network card will not maintain WOL state across reboots. Unless we do something to address this, you will need to run ethtool by hand every time the server boots and that defeats the entire purpose of being lazy. Some people might say “Ah Ha!”. I know that kind of “Ah Ha!” and it generally means more work for me. And in this case, I’m right. Some people would say, “I know how to do something when the computer boots. I will write an init script!”. And in response, I would shed a small tear and add it to my Peruvian monk collection. Writing a script? How lazy is that? Not very lazy at all, I say! There’s all that typing, and testing, and checking things into source control. Pah!
Fortunately, Ubuntu gives us a way to be even lazier. “Lazier?”, you exclaim. Yes, I say! Why write an entire script to say “Please wake up next time I whisper sweet nothings to you” when you can tell another script to do it for you? Wake on LAN is a network function, so let’s ask the networking init script to take care of the whole thing. We aren’t going to edit /etc/init.d/networking, of course. That would be sinful and, as Alton Brown says, that’s another show. Instead, we are going to edit /etc/network/interfaces. Go ahead. Start your vims and your emacses and your joes and such. Inside, you’ll see something similar to this.
auto eth0
iface eth0 inet static
address 10.0.0.13
netmask 255.255.255.0
broadcast 10.0.0.255
gateway 10.0.0.1
Or perhaps this.
auto eth0
iface eth0 inet dhcp
Either way, it doesn’t matter. What does matter is that you add this
post-up ethtool -s eth0 wol g
underneath so that it looks something like this.
auto eth0
iface eth0 inet static
address 10.0.0.13
netmask 255.255.255.0
broadcast 10.0.0.255
gateway 10.0.0.1
post-up ethtool -s eth0 wol g
And that’s it! After your network interface is successfully brought up, the script will run ethtool and your computer will wake up to know another sunrise.
Fin.