类型:【转载】
原文作者:【 Vivek Gite 】
日期:【July 13, 2021】
原文地址:https://www.cyberciti.biz/tips/linux-send-wake-on-lan-wol-magic-packets.html

 

Wake-on-LAN (WOL) is an Ethernet networking standard that allows a server to be turned on by a network message. You need to send ‘magic packets’ to wake-on-lan enabled ethernet adapters and motherboards to switch on the called systems. Make sure you connect the NIC (eth0 or eth1) with the motherboard and enable the BIOS’s WOL function. This is a quick guide to enable WOL under RHEL / Fedora / CentOS / Debian / Ubuntu Linux.

Client Software

It would be best if you used software to send WoL ( Wake-on-LAN ) magic packets to the target system. You will find various tools for all modern operating systems, including MS-Windows 8/10, Apple macOS & OS X, all modern Linux distros, FreeBSD, OpenBSD, NetBSD and many smartphones.

Linux Install etherwake Under Debian / Ubuntu Linux

We use the etherwake command to send a Wake-On-LAN “Magic Packet” under Linux operating systems. Type the following apt-get command/apt command to install the same under Debian / Ubuntu Linux desktop:
## apply all patches on Debian/Ubuntu ##
$ sudo aptitude install etherwake
## OR ##
$ sudo apt install etherwake

There is also Perl script to wake up computers. We can install it as follows on Debian/Ubuntu Linux:
sudo apt-get install wakeonlan

Installing a tool to send a Wake-On-LAN “Magic Packet” on RHEL/CentOS Linux

Type the following dnf command/yum command on your Fedora/RHEL/CentOS system. For instance:
$ sudo yum install net-tools

Red Hat Linux and friends users should use the net-tools package, which may be installed by default. The command name is ether-wake.

Installing wakeonlan on macOS Unix

First, install Homebrew on macOS and then run:
brew install wakeonlan
See “macOS – Wake Up Servers Using Wake-on-LAN ( WOL ) Command Utility” for more info.

How Do I Send WOL Magic Packets Under Linux?

Type the following command:
# wakeonlan MAC-Address-Here
OR
# etherwake MAC-Address-Here
# etherwake -D MAC-Address-Here

RHEL / Centos / Fedora Linux user, try:
# ether-wake MAC-Address-Here
If your MAC address were xx:yy:zz:11:22:33, you would type:
# wakeonlan xx:yy:zz:11:22:33
OR
# etherwake xx:yy:zz:11:22:33
Where,

  • xx:yy:zz:11:22:33 is remote servers mac address. You can obtained mac address using combination of the ping command and arp command:
    ping -c 4 server3 && arp -n

Examples

Here is how to use the limited broadcast address (255.255.255.255):
$ wakeonlan mac
$ wakeonlan mac-1 mac-2

Using a subnet broadcast address:
$ wakeonlan -i 192.168.1.255 mac
We can use another destination port too
$ wakeonlan -i 192.168.1.255 -p PORT mac
$ wakeonlan -i 192.168.1.255 -p 4242 mac

We can use a file as source of hardware and IP addresses:
$ wakeonlan -f homelab.wol
$ wakeonlan -f homelab.wol mac

The following is an example of a text file containing hardware addresses (macs). Let us display it using the more command or cat command/less command:
more homelab.wol

How Do I Verify That Remote Linux Server Supports Wake-on-LAN (WOL)?

First, reboot the remote server and go to BIOS > Power Management > “Wake On LAN”. Please turn WoL on. Next, save and close the bios settings. After activating Wake On LAN (WoL) in your BIOS hardware option for the network interface card, it is also necessary to activate it using ethtool. The ethtool command will configure eth0 to respond to the magic packet:
# ethtool -s eth0 wol g
Where,

  1. -s eth0 : Your NIC. Feel free to replace eth0 with your actual network interface device name.
  2. wol g : Sets Wake-on-LAN options using MagicPacket.

Type the following command to see current status of wol for eth0:
# ethtool eth0
Output from my NAS server:

Settings for eth0:
	Supported ports: [ ]
	Supported link modes:
	Supports auto-negotiation: No
	Advertised link modes:  Not reported
	Advertised auto-negotiation: No
	Speed: 100Mb/s
	Duplex: Full
	Port: MII
	PHYAD: 1
	Transceiver: internal
	Auto-negotiation: off
        Supports Wake-on: g
       Wake-on: g
	Link detected: yes

If you are using RHEL / SL / Fedora / CentOS Linux, edit /etc/sysconfig/network-scripts/ifcfg-eth0:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Add / modify the following line:

ETHTOOL_OPTS="wol g"

OR

ETHTOOL_OPTS="wol g autoneg off speed 100 duplex full "

Save and close the file. If you are using Debian / Ubuntu Linux, edit /etc/network/interfaces:
# vi /etc/network/interfaces
Append the following to eth0:

auto eth0
iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0
gateway 192.168.1.254
        post-up /sbin/ethtool -s eth0 wol g
        post-down /sbin/ethtool -s eth0 wol g

How to use the wakeonlan in your backup scripts

Here is a sample shell script that will wake up my laptop (IP 192.168.2.25 and mac 48:2a:e3:5c:16:bc) from my rsnapshot Linux backup server:

#!/bin/bash
# load ssh keys from keychain 
. /home/backups/.keychain/backup-ssh-key
 
# Try to wake up sleeping laptop at night and odd time as per cronjob
/usr/bin/wakeonlan 48:2a:e3:5c:16:bc
 
# Sleep for 30 seconds to that laptop comes online
/bin/sleep 30
 
# Verify and start backup
/sbin/ping -q -c 30 192.168.2.25 >/dev/null
if [ "$1" != "" ]
then
     # start backup
     /usr/local/bin/rsnapshot "$1"
     # push everything offsite to aws-s3 buckets and exit this session due to slow upload links
     echo '/home/backups/push-mirror-to-aws-s3' | /usr/bin/at now + 5 minute
else
     echo "Usage: $0 [hourly|daily|montly|weekly|yearly]"
     exit 1
fi

Summing up

I explained how to set ‘magic packets’ to wake-on-lan (WoL) enabled ethernet adapters and motherboards to switch on the PC or server. The magic packets also work when your system is in suspend or deep sleep mode. For example, my backup FreeBSD and Linux server can wake up my laptop at midnight and backup files. Of course, your BIOS must support the WoL option. Otherwise, it will not work. See the following man pages using the man command:
man wakeonlan
man ethtool

Also see WikiPedia page about WoL.

发表评论