Save Energy! – Power Up Your Home Linux Box From Behind Your Work Proxy Using Wake On LAN (WOL)

pwrbutton1These days with the price of electricity being so high and with computer power supplies being so big, not to mention the global warming / climate change factor, leaving your home-server running all day while you’re out at work on the off-chance that you might need to access it during the day is a luxury most people can ill-afford.

In an ideal world you would be able to switch on your home server remotely from your work laptop when you’re away from home. Maybe you need to log in via SSH or FTP (if these ports are not blocked by the proxy) or maybe you have it set up as a web server for development use (as I do) and would like to use some of your office downtime to work on something on your dev server. Once you’re done you can then switch it off again with a shell command.

All of this is easy enough to do so long as your home server’s connection to the internet is via an ethernet card that supports Wake On LAN (WOL). You can send a “magic packet“, a string of characters specifically formed to wake your server up either directly from your windows laptop or, if you are behind your company’s proxy server that blocks traffic on port 9, from any internet webserver.

Pre-requisites

This article will describe the steps that you need to take in order to configure this energy-saving functionality. I am assuming the following:

  1. your home server is running Ubuntu/Debian Linux (although it should be possible to use any flavour of Unix/Linux).
  2. the server is connected to the internet with a cable via an ethernet card that supports WOL
  3. your server has a static IP address at least within your home network. (If your ISP changes your IP address periodically like mine does then you will probably need to either pay them extra for a fixed IP address or use a dynamic DNS service)
  4. you have FTP write access to a webserver connected to the internet that supports PHP scripting
  5. you have about an hour of spare time.

The Steps

The procedure (describe in full below) can be divided into these three steps:

Step 1 – Verify that your computer and ethernet card support WOL and enable it

Step 2 – Ensure that your firewall forwards traffic arriving on port 9 to your home server

Step 3 – Install the “Magic Packet” PHP script on your internet web server

Once you have performed each of these steps and tested them to ensure that there are no problems then you will be able to switch on your home Linux box by simply loading a special web page from any internet browser and entering the username and password that you set to stop unauthorised access.

Step 1 – Verify that your computer and ethernet card support WOL and enable it

First we need to make sure that your hardware is actually configured to respond to magic packets.

  1. Open your home server’s BIOS and ensure that the Wake On LAN option (if it exists) is enabled..
  2. Open a command prompt and find the name of your network card by opening your interfaces file. Type…
    vi /etc/network/interfaces

    (If you are not using Ubuntu or Debian then the location of the interfaces file may be different for your distro)

    You should then see something like this…

    # The primary network interface
    auto eth0
    iface eth0 inet static
    
    address 192.168.1.101
    netmask 255.255.255.0
    network 192.168.1.0
    gateway 192.168.1.1
    

    Here we can see that the ethernet card is called ‘eth0′. If you have more than one ethernet card connected to your server then each will be named differently ie ‘eth0′, ‘eth1′, ‘eth2′…etc. (Incidentally, the interfaces file is also where the static IP address of the server is set, in this case being 192.168.1.101).

  3. Armed with the name of your ethernet device you can now find out whether its driver supports magicpacket and if it does then you will have to place a script to enable WOL in your init.d folder that is run each time the server powers down. This procedure is described very well in the Enabling WOL in Linux section of this article on the tjansson.dk blog.
  4. Once you have enabled WOL you must test that it works properly. You will need another computer in order to do this.
    • If your other computer is running Linux then follow the instructions in the Sending the magical package from Linux section at the end of the tjansson.dk blog article used in the step 3.
    • If your other computer runs windows then you can download and install “WOL Magic Packet Sender” and use that.

If you have successfully managed to switch on your home server from a remote computer then congratulations! You are already well on the way to saving the planet! … however, it is unlikely that this technique will work from outside of the LAN that you’re connected to because most likely your router’s firewall blocks port 9 traffic.

Step 2 – Ensure that your firewall forwards traffic arriving on port 9 to your home server

If your home server is not protected by a firewall then you don’t need to perform this step. However if this is the case then you should seriously consider configuring a firewall as soon as possible to protect your home network. These days almost all domestic routers feature a firewall – enable it now!….and then forward port 9 as described in this section.

The specific port-forwarding method you need to use will depend upon which make and model of router you are using but generally it will require you to log in through the router’s web interface and find the page where you can set traffic arriving from outside to be re-directed to the local (static) IP address of your home server (as displayed in the interfaces file, described above). You can find out everything you need to know about port forwarding here.

Once you have set up port forwarding then you need to test whether you can now switch the server on from outside your LAN. The easiest way to achieve this is to connect to the internet through a different connection and send the magic packet. This could be done by logging into a friend’s VPN and sending it (as I was able to do) or maybe using a neighbours wifi connection or even using your mobile phone WAP connection. If finding an alternative internet connection proves impossible then don’t worry, you will have the chance to test your port forwarding properly after carrying out step 3.

(Incidentally, if you only ever need to switch on the home server from remote locations where port 9 traffic is not blocked by your company’s proxy server a then you do not need to continue with this tutorial as you have already finished!)

Step 3 – Install the “Magic Packet” PHP script on your internet web server

If, like me, you work in a company where a proxy server blocks outgoing traffic on all but the most necessary ports, then it will more than likely also block your magic packet on port 9. However, if you have access to a live internet webserver that supports PHP scripting then a simple solution is to configure this magic packet PHP script (courtesy of hackernotcracker.com) to switch on your server.

  1. Find the MAC address of your home Linux box’s ethernet card by typing…
    ifconfig

    you should now see something like this…

    eth0	Link encap:Ethernet  HWaddr 00:13:d1:f9:53:a6
    	inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
    	inet6 addr: fe80::219:d1ff:fef9:53a6/64 Scope:Link
    	UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    	RX packets:5617 errors:0 dropped:0 overruns:0 frame:0
    	TX packets:1008 errors:0 dropped:0 overruns:0 carrier:0
    	collisions:0 txqueuelen:1000
    	RX bytes:1479172 (1.4 MB)  TX bytes:127750 (124.7 KB)
    	Interrupt:222
    
    lo	Link encap:Local Loopback
    	inet addr:127.0.0.1  Mask:255.0.0.0
    	inet6 addr: ::1/128 Scope:Host
    	UP LOOPBACK RUNNING  MTU:16436  Metric:1
    	RX packets:198 errors:0 dropped:0 overruns:0 frame:0
    	TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
    	collisions:0 txqueuelen:0
    	RX bytes:19931 (19.4 KB)  TX bytes:19931 (19.4 KB)
    

    The MAC address is the long hex number separated by colons that appears immediately after “HWaddr” in the section corresponding to your ethernet card. In this case it is 00:13:d1:f9:53:a6.

  2. Write down your IP address. The easiest way to do this is to use a web browser to visit this site. Your IP address will be displayed to you by the web page that you opened.
  3. Open your favourite text editor and create a new file called wol.php and save it somewhere handy, like your desktop.
  4. Paste this code (borrowed from hackernotcracker.com) into the editor.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    
    	<?php
    	# Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
    	# Modified by Allan Barizo http://www.hackernotcracker.com
    	flush();
    	function WakeOnLan($addr, $mac,$socket_number) {
    	  $addr_byte = explode(':', $mac);
    	  $hw_addr = '';
    	  for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
    	  $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
    	  for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
    	  // send it to the broadcast address using UDP
    	  // SQL_BROADCAST option isn't help!!
    	  $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    	  if ($s == false) {
    	    echo "Error creating socket!\n";
    	    echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
    	    return FALSE;
    	    }
    	  else {
    	    // setting a broadcast option to socket:
    	    $opt_ret = socket_set_option($s, 1, 6, TRUE);
    	    if($opt_ret <0) {
    	      echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
    	      return FALSE;
    	      }
    	    if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) { 
    	      echo "Magic Packet sent successfully!";
    	      socket_close($s);
    	      return TRUE;
    	      }
    	    else { 
    	      echo "Magic packet failed!";
    	      return FALSE;
    	      }     
    	    }
    	  }
    	// Port number where the computer is listening. Usually, any number 
    	// between 1-50000 will do. Normally people choose 7 or 9.
    	$socket_number = "9";
    	// MAC Address of the listening computer's network device
    	$mac_addy = "mac address here";
    	// IP address of the listening computer. Input the domain name if you 
    	// are using a hostname (like when under Dynamic DNS/IP)
    	$ip_addy = gethostbyname("domain here");
    	// $ip_addy = 'IP address here'; 
     
    	WakeOnLan($ip_addy, $mac_addy,$socket_number)
    	?>

  5. Insert your MAC address into line 42
  6. Do one of the following:
    • insert your domain name (you can find this by typing hostname at the linux command prompt)into line 44
    • OR

    • insert your IP address into line 45 and remove the // comment at the begining of the line
  7. Re-save wol.php
  8. Log into your internet webserver and create a new directory called wol in your site’s document root.
  9. upload the script into the new folder via FTP or some equivalent method.
  10. Now browse this file in any web browser by typing:
    http://[your domain]/wol/wol.php

    if all goes well then you should see a browser page that outputs the words “Magic Packet sent successfully!”. If you don’t see this then you have done something wrong.

  11. Now you can test your script properly. Switch off you linux server by powering it down properly. This can be done by typing the command..
    shutdown -P now

    Note that typing ‘halt’ is not advisable as the server does not necessarily start back up again when the magic packet arrives.

  12. Browse your magic packet script again. This time your server should immediately spring to life. If it does then congratulations (again) !! If it works then that means that you should be able to switch it on from anywhere where you have internet access just by browsing the trigger page.

    If your server still fails to start up even after you have carefully checked that you followed all of the steps correctly then you may wish to install a packet sniffer such as Wireshark on your linux box so that you can monitor your ethernet port to see whether the magic packet arrives when you trigger it through its URL. If the script is sending its packet then you should see it arrive in the wireshark window. If it still fails to power up the box then you know that there is something wrong with the WOL configuration. Try installing the script on a webserver running on the local machine and triggering it from there. If it works then you know that you need to be looking at your router or webserver configuration. If it doesn’t then there is a problem in the script.

    As a final security measure you may want to password protect the folder containing your magic packet script on your webserver.

3 thoughts on “Save Energy! – Power Up Your Home Linux Box From Behind Your Work Proxy Using Wake On LAN (WOL)

  1. Pingback: Acdrtux » Blog Archive » SSH, o «la comodidad de trabajar en casa desde cualquier parte»

  2. This WOL php script does not make very far. It yields: Fatal error: Call to undefined function socket_create() in C:\MyWebSite\WOL.php on line 13

    I wish it worked, because it could be very useful !

    • @strato – it sounds like there’s something wrong with your PHP configuration. If you have access to the php.ini file on your webserver then you should make sure that sockets are enabled by uncommenting “extension=php_sockets.dll”. If you’re on shared hosting then this might not be possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>