HOW-TO:Set up Wake-on-LAN for Ubuntu: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
>MacUsers
 
(75 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{XBMC wiki toc Inline}}
{{mininav| [[Linux]] }}
'''''This page is incomplete; writing in progress...'''''
If you are not one of the lucky ones with a built-in start-up controller (so that the remote can be used to start the computer) or if the computer is in the other room/floor, sometimes it's pretty annoying to go to the upstairs, for example, just to push a button. Wake-On-LAN (WOL) is a network standard, which provides the ability to switch on remote computers through special network packets, called [[w:Wake-on-LAN#Magic packet|Magic Packet]] from sleeping, hibernating, or powered-off state. It only works with Wake-On-LAN compliant BIOS and NIC.
== Before we start ==
First of all, the motherboard should support Wake-On-LAN and it should be enabled in the BIOS.  


Different BIOSs and motherboards do this differently; for example on the ASUS P5N7A-VM, it's under ''Power > APM Configuration'' and in there ''Resume On PCIE Wake'' and ''Resume On LAN(MAC)'' should be enabled.


==Before you start==
== Enabling WOL in the OS ==
First off, the motherboard should support the WOL (Wake On LAN) and it should be enabled in the BIOS. Different BIOS/motherboard do it differently; on ASUS (P5N7A-VM), it's under Power > APM Configuration and in there "Resume On PCIE Wake" and "Resume On LAN(MAC)" should be enabled.
Even though Wake-On-LAN works in network layer 2, OS support is still one of the important things to make it work. Even if you see the Ethernet port LED is on after the halt/shutdown, the OS shuts off the Ethernet internally. One must tell Ethernet controller to WOL when boots up and tell "halt" script not to bring down controller during shut-down.  
 
=== Install ethtool ===
==Enabling WOL in the OS==
One of the easiest applications to use to enable Wake-On-LAN is ''ethtool''. Install it with:
Even though WOL works in network layer 2, OS support is still one of the important things to make it work. Even if you see the light on the Ethernet port is on after the halt/shutdown, the OS shuts off the Ethernet internally. One must tell Ethernet controller to WOL when boots up and tell "halt" script not to bring down controller during shut-down.  
===Install ethtool===
I found "ethtool" is one of the easiest to use to enable WOL; install it first if it's not there yet.
<source lang="bash">
<source lang="bash">
sudo apt-get ethtool
sudo apt-get install ethtool
</source>
</source>
===Set Wake-on-LAN options===
=== Setup ethtool WOL options ===
Run the following command to enable it:
Run the following command to enable it on ethX (where X is the Ethernet device number).
<source lang="bash">sudo ethtool -s eth0 wol g</source>
<source lang="bash">sudo ethtool -s eth0 wol g</source>
*Note: Not all devices support this.
* ''Note: Not all devices support this.''
This can be verified running ethtool on ethX (where X is the name/number of the Ethernet device).
The settings can be verified by running:
xbmc@htpc:~$ sudo ethtool eth0
<source lang="bash">sudo ethtool eth0</source>
[sudo] password for xbmc:
<source lang="bash">
  Settings for eth0:
  Settings for eth0:
  Supported ports: [ MII ]
  Supported ports: [ MII ]
  Supported link modes:  10baseT/Half 10baseT/Full  
  Supported link modes:  10baseT/Half 10baseT/Full  
                        100baseT/Half 100baseT/Full
  ...
                        1000baseT/Full
         ...
  .....
         .....
Auto-negotiation: on
  Supports Wake-on: g
  Supports Wake-on: g
  Wake-on: g
  Wake-on: g
  Link detected: yes
  Link detected: yes
</source>
* ''Note: "g" indicates wake on Magic Packet is enabled''
=== Enable ethtool on Start-up ===
To have an application or service is running on startup, Ubuntu uses Upstart scripts which replace the old init scripts.
The following commands; create ''wakeonlan.conf'' with the upstart code to run wakeonlan (as per example above), set the file to exectuable, and start the service<ref>http://askubuntu.com/questions/47918/how-can-i-enable-wake-on-lan-permanently</ref>:
<source lang="bash">
sudo bash -c "cat > /etc/init/wakeonlan.conf" <<'EOF'
start on started network
script
    interface=eth0
    logger -t 'wakeonlan init script' enabling wake on lan for $interface
    ethtool -s $interface wol g
end script
EOF
</source><br />
<source lang="bash">
sudo chmod +x /etc/init/wakeonlan.conf
</source><br />
<source lang="bash">
sudo service wakeonlan start
</source>
== Waking with Magic Packet ==
Wake-on-LAN is platform-independent; any application that sends "magic packets" can wake up computers from shutdown state (as long mains power is not off) regardless of the OS it boots into afterwards.
The following steps are for setting up a Wake-on-LAN application on Ubuntu but there are many applications available, including for Windows and Mac OS X, that accomplish the same task.
Install our Wake-on-LAN application, in this case ''powerwake'', as it has IP and hostname options.
<source lang="bash">
sudo apt-get install powerwake
</source>
The simplest usage of powerwake is to just supply the IP address or hostname of the machine you want to wake:
<source lang="bash">
powerwake 192.168.1.1
</source><br />
<source lang="bash">
powerwake xbmc-hostname.local
</source>
* ''Note: To enable powerwake to cache the remote machine MAC address you may need to run it once with the remote machine awake.''
If everything goes well, as described above, you should now be seeing the machine in the other room booting up.
== Finishing Touches ==
<div id="Finishing Touches"></div>
=== Aliases ===
You can make it even easier to run the above application from the command line or other scripts. One way is to include aliases to it in your .bashrc file, found in your user's home directory. Open the file in your preferred text editor and scroll to the bottom of the file, then add one or more of the lines below, depending on what you need to do.
<source lang="bash">nano ~/.bashrc</source><br />


==Get it enabled at system start-up==
Debian/Ubuntu uses Sys-V like init system for executing commands during the system bootup and shutdown time. If you wish to add a new service to start when the machine boots the script should be added to the directory /etc/init.d/ and then the "/etc/rc{runlevel}.d/" symlinks cause the script to be executed.
===The init.d script===
Create a file, say "walk-on-lan" in the "/etc/init.d/" (just copy & paste the entire code in the terminal)
<source lang="bash">
<source lang="bash">
sudo cat << EOF >> /etc/init.d/walk-on-lan
# User Defined Aliases
#!/bin/bash
alias htpcwol="powerwake xbmc-hostname.local"
#
alias htpcssh="powerwake xbmc-hostname.local; ssh user@xbmc-hostname.local"
### BEGIN INIT INFO
</source>
# Provides:            walk-on-lan
Save and exit, then run the following to reload it (normally loaded automatically at startup):
# Required-Start:      \$local_fs
<source lang="bash">source ~/.bashrc</source>
# Required-Stop:        \$local_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 6
# Short-Description:    Enable Wake-On-Lan
### END INIT INFO
#
. /lib/lsb/init-functions


do_start() {
You will now have two new commands (aliases) available for use from the command line; the first alias ''htpcwol'' simple wakes the host up remotely and the second one ''htpcssh'' also wakes it up but then creates an SSH session to the remote machine.
    ethtool -s eth0 wol g
    exit
}


do_stop() {
Aliases are a huge time saver if you use the command line for remote access often and there are many examples on the internet.<ref>http://www.howtogeek.com/73768/how-to-use-aliases-to-customize-ubuntu-commands/</ref>
    ethtool -s eth0 wol d
    exit
}


case "$1" in
=== Suspend or Hibernate from command line ===
  start)
To suspend the remote machine from within an SSH session set the suspend command to run several minutes in the future:<ref>http://askubuntu.com/questions/35719/how-do-i-suspend-over-ssh</ref>
        do_start
<source lang="bash">
        ;;
echo 'pm-suspend' | sudo at now + 3 minutes
  restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
  stop)
        do_stop
        ;;
  *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
EOF
sudo chmod 755 /etc/init.d/walk-on-lan
</source>
</source>


===Automatic start-up at boot===
You can then disconnect safely from the SSH session and the remote machine will suspend at the set time.
"sysv-rc-conf" is run-level configuration for Sys-V like init script links. From the man page: sysv-rc-conf gives an easy to use interface for managing "/etc/rc{runlevel}.d/" symlinks. sysv-rc-conf is not installed by default and needs to be installed first.
 
<source lang="bash">sudo apt-get install sysv-rc-conf</source>
=== Using the Kodi remote control iOS app ===
and when done, we enable the walk-on-lan service for runlevel 2,3,4 & 5, using --level option
Commands to ''shutdown'' systems often work best when issuing them against MAC addresses. If you are using the Kodi Remote Control app on the iPad or iPhone, you must supply the MAC address (or: HWaddr) of your NIC in the setup. I can now shutdown and startup Kodi from the app.
<source lang="bash">sudo sysv-rc-conf --level 2345 walk-on-lan on</source>
 
If everything goes well, you should see the WOL is enabled for the above run-levels.
To find out the HWaddr on Ubuntu use this command:
xbmc@htpc:~$ sysv-rc-conf --list | grep walk
<source lang="bash">
walk-on-lan  2:on 3:on 4:on 5:on
ifconfig | grep HWaddr
That's all, the machine is ready to accept WOL magic packet.
</source>


==Extra info==
== References ==
Wake-on-LAN is platform-independent; any application that sends "magic packets" can wake up computers from shutdown state (as long as it's not off from mains) regardless of OS it boots into afterward.
<references/>


[[category:How To|Linux]]
[[Category:How-to|Wake]]
[[category:Inner Workings]]
[[Category:Linux]]
[[category:Development]]
[[category:Linux]]
[[category:XBMC Live]]
[[category:To-Do]]

Latest revision as of 23:12, 20 June 2017

Home icon grey.png   ▶ Linux ▶ HOW-TO:Set up Wake-on-LAN for Ubuntu

If you are not one of the lucky ones with a built-in start-up controller (so that the remote can be used to start the computer) or if the computer is in the other room/floor, sometimes it's pretty annoying to go to the upstairs, for example, just to push a button. Wake-On-LAN (WOL) is a network standard, which provides the ability to switch on remote computers through special network packets, called Magic Packet from sleeping, hibernating, or powered-off state. It only works with Wake-On-LAN compliant BIOS and NIC.

Before we start

First of all, the motherboard should support Wake-On-LAN and it should be enabled in the BIOS.

Different BIOSs and motherboards do this differently; for example on the ASUS P5N7A-VM, it's under Power > APM Configuration and in there Resume On PCIE Wake and Resume On LAN(MAC) should be enabled.

Enabling WOL in the OS

Even though Wake-On-LAN works in network layer 2, OS support is still one of the important things to make it work. Even if you see the Ethernet port LED is on after the halt/shutdown, the OS shuts off the Ethernet internally. One must tell Ethernet controller to WOL when boots up and tell "halt" script not to bring down controller during shut-down.

Install ethtool

One of the easiest applications to use to enable Wake-On-LAN is ethtool. Install it with:

sudo apt-get install ethtool

Setup ethtool WOL options

Run the following command to enable it on ethX (where X is the Ethernet device number).

sudo ethtool -s eth0 wol g
  • Note: Not all devices support this.

The settings can be verified by running:

sudo ethtool eth0
 Settings for eth0:
 	Supported ports: [ MII ]
 	Supported link modes:   10baseT/Half 10baseT/Full 
 	...
         ...
 	Supports Wake-on: g
 	Wake-on: g
 	Link detected: yes
  • Note: "g" indicates wake on Magic Packet is enabled

Enable ethtool on Start-up

To have an application or service is running on startup, Ubuntu uses Upstart scripts which replace the old init scripts.

The following commands; create wakeonlan.conf with the upstart code to run wakeonlan (as per example above), set the file to exectuable, and start the service[1]:

sudo bash -c "cat > /etc/init/wakeonlan.conf" <<'EOF'
start on started network

script
    interface=eth0
    logger -t 'wakeonlan init script' enabling wake on lan for $interface
    ethtool -s $interface wol g
end script
EOF


sudo chmod +x /etc/init/wakeonlan.conf


sudo service wakeonlan start

Waking with Magic Packet

Wake-on-LAN is platform-independent; any application that sends "magic packets" can wake up computers from shutdown state (as long mains power is not off) regardless of the OS it boots into afterwards.

The following steps are for setting up a Wake-on-LAN application on Ubuntu but there are many applications available, including for Windows and Mac OS X, that accomplish the same task.

Install our Wake-on-LAN application, in this case powerwake, as it has IP and hostname options.

sudo apt-get install powerwake

The simplest usage of powerwake is to just supply the IP address or hostname of the machine you want to wake:

powerwake 192.168.1.1


powerwake xbmc-hostname.local
  • Note: To enable powerwake to cache the remote machine MAC address you may need to run it once with the remote machine awake.

If everything goes well, as described above, you should now be seeing the machine in the other room booting up.

Finishing Touches

Aliases

You can make it even easier to run the above application from the command line or other scripts. One way is to include aliases to it in your .bashrc file, found in your user's home directory. Open the file in your preferred text editor and scroll to the bottom of the file, then add one or more of the lines below, depending on what you need to do.

nano ~/.bashrc


# User Defined Aliases
alias htpcwol="powerwake xbmc-hostname.local"
alias htpcssh="powerwake xbmc-hostname.local; ssh [email protected]"

Save and exit, then run the following to reload it (normally loaded automatically at startup):

source ~/.bashrc

You will now have two new commands (aliases) available for use from the command line; the first alias htpcwol simple wakes the host up remotely and the second one htpcssh also wakes it up but then creates an SSH session to the remote machine.

Aliases are a huge time saver if you use the command line for remote access often and there are many examples on the internet.[2]

Suspend or Hibernate from command line

To suspend the remote machine from within an SSH session set the suspend command to run several minutes in the future:[3]

echo 'pm-suspend' | sudo at now + 3 minutes

You can then disconnect safely from the SSH session and the remote machine will suspend at the set time.

Using the Kodi remote control iOS app

Commands to shutdown systems often work best when issuing them against MAC addresses. If you are using the Kodi Remote Control app on the iPad or iPhone, you must supply the MAC address (or: HWaddr) of your NIC in the setup. I can now shutdown and startup Kodi from the app.

To find out the HWaddr on Ubuntu use this command:

ifconfig | grep HWaddr

References