如何使用 Linux 命令 [ LAN 唤醒( WOL ) ] 通过发送魔术包唤醒计算机
局域网唤醒(WOL) 是一种以太网网络标准,允许通过网络消息打开服务器。您需要向启用局域网唤醒的以太网适配器和主板发送“魔术包”以打开被叫系统。确保将 NIC(eth0 或 eth1)与主板连接并启用 BIOS 的 WOL 功能。这是在 RHEL / Fedora / CentOS / Debian / Ubuntu Linux 下启用 WOL 的快速指南。
客户端软件
最好使用软件向目标系统发送 WoL(局域网唤醒)魔术包。您会发现适用于所有现代操作系统的各种工具,包括 MS-Windows 8/10、Apple macOS 和 OS X、所有现代 Linux 发行版、FreeBSD、OpenBSD、NetBSD 和许多智能手机。
Linux 在 Debian / Ubuntu Linux 下安装 etherwake
我们使用 etherwake 命令在 Linux 操作系统下发送网络唤醒“魔术包”。在 Debian / Ubuntu Linux 桌面下输入以下apt-get 命令/ apt 命令安装该命令:
会话:
## apply all patches on Debian/Ubuntu ##
$ sudo aptitude install etherwake
## OR ##
$ sudo apt install etherwake
[sudo] password for vivek: Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package etherwak [vivek@example-wks01 backups]$ sudo apt install etherwake Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: wakeonlan The following NEW packages will be installed: etherwake wakeonlan 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 18.1 kB of archives. After this operation, 56.3 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 etherwake amd64 1.09-4build1 [8,900 B] Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 wakeonlan all 0.41-12 [9,232 B] Fetched 18.1 kB in 1s (16.9 kB/s) Selecting previously unselected package etherwake. (Reading database ... 338929 files and directories currently installed.) Preparing to unpack .../etherwake_1.09-4build1_amd64.deb ... Unpacking etherwake (1.09-4build1) ... Selecting previously unselected package wakeonlan. Preparing to unpack .../wakeonlan_0.41-12_all.deb ... Unpacking wakeonlan (0.41-12) ... Setting up wakeonlan (0.41-12) ... Setting up etherwake (1.09-4build1) ... Processing triggers for man-db (2.9.1-1) ...
在 RHEL/CentOS Linux 上安装发送网络唤醒“魔包”的工具
还有用于唤醒计算机的 Perl 脚本。我们可以在 Debian/Ubuntu Linux 上按如下方式安装它:
sudo apt-get install wakeonlan
在您的 Fedora/RHEL/CentOS/Rocky 和 AlmaLinux 系统上输入以下 dnf 命令/ yum 命令。例如:
$ sudo yum install net-tools
来自我的 RHEL 8 框的输出:
Last metadata expiration check: 3:23:23 ago on Mon Feb 22 05:56:16 2021. Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Installing: net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k Transaction Summary ================================================================================ Install 1 Package Total download size: 322 k Installed size: 942 k Is this ok [y/N]: y Downloading Packages: net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 689 kB/s | 322 kB 00:00 -------------------------------------------------------------------------------- Total 271 kB/s | 322 kB 00:01 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1 Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1 Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1 Installed: net-tools-2.0-0.52.20160912git.el8.x86_64 Complete!
Red Hat Linux 及其相关用户应使用 net-tools 包,该包可能已默认安装。命令名称为 ether-wake。
在 macOS Unix 上安装 wakeonlan
首先,在 macOS 上安装 Homebrew,然后运行:有关更多信息,
$ brew install wakeonlan
请参阅“ macOS – 使用 LAN 唤醒 (WOL) 命令实用程序唤醒服务器”。
如何在 Linux 下发送 WOL 魔包?
输入以下命令:
$ wakeonlan MAC-Address-Here
或
RHEL / Centos / Fedora Linux 用户,请尝试:
如果您的 MAC 地址是 xx:yy:zz:11:22:33,则输入:
或
其中,
$ etherwake MAC-Address-Here
$ etherwake -D MAC-Address-Here
$ ether-wake MAC-Address-Here
$ wakeonlan xx:yy:zz:11:22:33
$ etherwake xx:yy:zz:11:22:33
- xx:yy:zz:11:22:33是远程服务器的 mac 地址。您可以使用ping 命令和 arp 命令的组合来获取 mac 地址:
ping -c 4 server3 && arp -n
示例
以下是如何使用有限广播地址 (255.255.255.255):
使用子网广播地址:
我们也可以使用另一个目标端口
我们可以使用文件作为硬件和 IP 地址的来源:
以下是包含硬件地址 (macs) 的文本文件示例。让我们使用 more 命令或cat 命令/less 命令显示它:
示例配置文件:
$ wakeonlan mac
$ wakeonlan mac-1 mac-2
$ wakeonlan -i 192.168.1.255 mac
$ wakeonlan -i 192.168.1.255 -p PORT mac
$ wakeonlan -i 192.168.1.255 -p 4242 mac
$ wakeonlan -f homelab.wol
$ wakeonlan -f homelab.wol mac
more homelab.wol
# File structure # -------------- # - blank lines are ignored # - comment lines are ignored (lines starting with a hash mark '#') # - other lines are considered valid records and can have 3 columns: # # Hardware address, IP address, destination port # # the last two are optional, in which case the following defaults # are used: # # IP address: 255.255.255.255 (the limited broadcast address) # port: 9 (the discard port) # 00:16:3e:a3:9d:a8 192.168.1.255 9 00:16:3e:08:ed:c6 255.255.255.255 f0:1f:af:1f:2c:60
如何验证远程 Linux 服务器是否支持局域网唤醒 (WOL)?
首先,重新启动远程服务器,然后转到 BIOS > 电源管理 > “局域网唤醒”。请打开 WoL。接下来,保存并关闭 BIOS 设置。在网络接口卡的 BIOS 硬件选项中激活局域网唤醒 (WoL) 后,还需要使用 ethtool 激活它。ethtool 命令将配置 eth0 以响应魔术包:
# ethtool -s eth0 wol g
其中,
- -s eth0:您的 NIC。请随意将 eth0 替换为您的实际网络接口设备名称。
- wol g:使用 MagicPacket 设置 LAN 唤醒选项。
键入以下命令来查看 eth0 的 wol 当前状态:
# ethtool eth0
来自我的 NAS 服务器的输出:
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
如果您使用的是 RHEL / SL / Fedora / CentOS Linux,请编辑 /etc/sysconfig/network-scripts/ifcfg-eth0:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
添加/修改以下行:
ETHTOOL_OPTS="wol g"
或者
ETHTOOL_OPTS="wol g autoneg off speed 100 duplex full "
保存并关闭文件。如果您使用的是 Debian / Ubuntu Linux,请编辑 /etc/network/interfaces:
# vi /etc/network/interfaces
将以下内容附加到 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
如何在备份脚本中使用 wakeonlan
下面是一个示例 shell 脚本,它将从我的rsnapshot Linux 备份服务器唤醒我的笔记本电脑(IP 192.168.2.25 和 mac 48:2a:e3:5c:16:bc):
#!/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
总结
我解释了如何将“魔术包”设置为启用了局域网唤醒 (WoL) 的以太网适配器和主板,以打开 PC 或服务器。魔术包在系统处于挂起或深度睡眠模式时也能工作。例如,我的备份 FreeBSD 和 Linux 服务器可以在午夜唤醒我的笔记本电脑并备份文件。当然,您的 BIOS 必须支持 WoL 选项。否则,它将无法工作。使用 man 命令查看以下手册页:
另请参阅有关WoL 的 WikiPedia 页面。
$ man wakeonlan
$ man ethtool