[Ubuntu] Enable Ubuntu WOL function

Ubuntu 18.04

$ sudo apt install net-tools
$ sudo atp-get install ethtool

使用 ipconfig 查看 LAN 卡名稱:

$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:c8:22:33:44 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::28db:9357:aa77:f13 prefixlen 64 scopeid 0x20<link>
ether 33:9c:52:07:66:88 txqueuelen 1000 (Ethernet)
RX packets 25517 bytes 2338698 (2.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 46529 bytes 14797516 (14.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 328 bytes 23053 (23.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 328 bytes 23053 (23.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

docker0:給 Docker 用的虛擬網卡
lo: 為 Loopback
enp1s0: 真正的網卡名稱

有了網卡名稱再使用 ethtool <網卡名稱> 察看網卡狀態:

$ sudo ethtool enp1s0
Settings for enp1s0:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full 
100baseT/Half 100baseT/Full 
1000baseT/Half 1000baseT/Full 
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full 
100baseT/Half 100baseT/Full 
1000baseT/Half 1000baseT/Full 
Advertised pause frame use: Symmetric Receive-only
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Link partner advertised link modes: 10baseT/Half 10baseT/Full 
100baseT/Half 100baseT/Full 
1000baseT/Full 
Link partner advertised pause frame use: Symmetric
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Port: MII
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: pumbg
Wake-on: d
Current message level: 0x00000033 (51)
drv probe ifdown ifup
Link detected: yes

其中 Wake-on 就是網卡WOL狀態, d = Disable, g = Enable。

啟用網卡 WOL 功能:
$ sudo ethtool -s enp1s0 wol g

啟用後實測確時可以喚醒電腦,但要注意是當系統重開機 Wake-on 狀態就會又跳回 Disable 了。

 

如果要讓系統每次開機都能自動啟用 WOL,可以這樣做:

  1. 建立一 wol service 檔案 /etc/systemd/system/wol@.service ,內容如下:
[Unit]
Description=Wake-on-LAN for %i
Requires=network.target
After=network.target

[Service]
ExecStart=/sbin/ethtool -s %i wol g
Type=oneshot

[Install]
WantedBy=multi-user.target

2. 啟用 wol service (將 enp1s0 改為實際網卡名稱)

$ systemctl enable wol@enp1s0
Created symlink /etc/systemd/system/multi-user.target.wants/wol@enp1s0.service → /etc/systemd/system/wol@.service.

3. 檢查 wol service 是否啟用

$ systemctl is-enabled wol@enp1s0
enabled

4. 重開機然後再檢查一次 walk-on 狀態

$ sudo ethtool enp1s0 

可以看到 WOL 在重開機後已正確啟用

walk-on: g

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *