如何使用ethtool命令管理以太網(wǎng)卡
ethtool
用于查看和修改網(wǎng)絡設(shè)備(尤其是有線以太網(wǎng)設(shè)備)的驅(qū)動參數(shù)和硬件設(shè)置。你可以根據(jù)需要更改以太網(wǎng)卡的參數(shù),包括自動協(xié)商、速度、雙工和局域網(wǎng)喚醒等參數(shù)。通過對以太網(wǎng)卡的配置,你的計算機可以通過網(wǎng)絡有效地進行通信。該工具提供了許多關(guān)于接駁到你的 Linux 系統(tǒng)的以太網(wǎng)設(shè)備的信息。
在這篇文章中,我們將告訴你如何更改以下的參數(shù)以及如何查看這些參數(shù)。這篇文章將幫助你在 Linux 系統(tǒng)中排除與以太網(wǎng)卡相關(guān)的問題。
下面的信息將幫助你了解以太網(wǎng)卡的工作原理。
- 半雙工:半雙工模式允許設(shè)備一次只能發(fā)送或接收數(shù)據(jù)包。
- 全雙工:全雙工模式允許設(shè)備可以同時發(fā)送和接收數(shù)據(jù)包。
- 自動協(xié)商:自動協(xié)商是一種機制,允許設(shè)備自動選擇最佳網(wǎng)速和工作模式(全雙工或半雙工模式)。
- 速度:默認情況下,它會使用最大速度,你可以根據(jù)自己的需要改變它。
- 鏈接檢測:鏈接檢測可以顯示網(wǎng)卡的狀態(tài)。如果顯示為
no
,請嘗試重啟網(wǎng)卡。如果鏈路檢測仍顯示no
,則檢查交換機與系統(tǒng)之間連接的線纜是否有問題。
如何在 Linux 上安裝 ethtool
默認情況下,大多數(shù)系統(tǒng)上應該已經(jīng)安裝了 ethtool
。如果沒有,你可以從發(fā)行版的官方版本庫中安裝。
對于 RHEL/CentOS 6/7 系統(tǒng),請使用 s-systems/" class="ext" rel="external nofollow" target="_blank">yum 命令 安裝 ethtool
:
$ sudo yum install -y ethtool
對于 RHEL/CentOS 8 和 Fedora 系統(tǒng),請使用 dnf 命令 安裝 ethtool
:
$ sudo yum install -y ethtool
對于基于 Debian 的系統(tǒng),請使用 apt 命令 或 apt-get 命令 安裝 ethtool
:
$ sudo apt-get install ethtool
對于 openSUSE 系統(tǒng),使用 zypper 命令 安裝 ethtool
:
$ sudo zypper install -y ethtool
對于 Arch Linux 系統(tǒng),使用 pacman 命令 安裝 ethtool
:
$ sudo pacman -S ethtool
如何檢查 Linux 上的可用網(wǎng)絡接口
你可以使用 ip 命令或 ifconfig
命令(在現(xiàn)代發(fā)行版中已被淘汰)來驗證可用的、活動的網(wǎng)卡的名稱和其他細節(jié):
# ip a
或
# ifconfig
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:10:22:35:23:sf brd ff:ff:ff:ff:ff:ff
inet 192.164.23.100/24 brd 192.164.23.255 scope global eth0
valid_lft forever preferred_lft forever
如何檢查 Linux 上的網(wǎng)卡(NIC)信息
掌握了以太網(wǎng)卡名稱后,就可以使用 ethtool
命令輕松查看其詳細信息,如下所示。
在 Linux 系統(tǒng)中,每個網(wǎng)卡(NIC)都被分配了唯一的名稱,如 ethX、enpXXX 等。
- 舊的 Linux 發(fā)行版使用的是
eth[X]
格式。例如,RHEL 6 和它們的舊版本。 - 現(xiàn)代的 Linux 發(fā)行版使用
enp[XXX]
或ens[XXX]
格式。例如,大多數(shù)現(xiàn)代 Linux 發(fā)行版都使用這種格式,包括 RHEL 7、Debian 10、Ubuntu 16.04 LTS。
# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 1000baseT/Full
10000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: No
Supported FEC modes: Not reported
Advertised link modes: Not reported
Advertised pause frame use: No
Advertised auto-negotiation: No
Advertised FEC modes: Not reported
Speed: 10000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: off
MDI-X: Unknown
Supports Wake-on: uag
Wake-on: d
Link detected: yes
如何檢查以太網(wǎng)卡的驅(qū)動程序和固件版本
你可以使用 ethtool
命令的 -i
選項檢查驅(qū)動程序版本、固件版本和總線的詳細信息,如下所示:
# ethtool -i eth0
driver: vmxnet3
version: 1.4.16.0-k-NAPI
firmware-version:
expansion-rom-version:
bus-info: 0000:0b:00.0
supports-statistics: yes
supports-test: no
supports-eeprom-access: no
supports-register-dump: yes
supports-priv-flags: no
如何檢查網(wǎng)絡使用情況統(tǒng)計
你可以使用 ethtool
命令中的 -S
選項來查看網(wǎng)絡使用情況統(tǒng)計。它可以顯示傳輸?shù)淖止?jié)數(shù)、接收的字節(jié)數(shù)、錯誤數(shù)等。
# ethtool -S eth0
NIC statistics:
Tx Queue#: 0
TSO pkts tx: 2053
TSO bytes tx: 7167572
ucast pkts tx: 4028081
ucast bytes tx: 399093197
mcast pkts tx: 0
mcast bytes tx: 0
bcast pkts tx: 0
bcast bytes tx: 0
pkts tx err: 0
pkts tx discard: 0
drv dropped tx total: 0
too many frags: 0
giant hdr: 0
hdr err: 0
tso: 0
ring full: 0
pkts linearized: 0
hdr cloned: 0
giant hdr: 0
Tx Queue#: 1
TSO pkts tx: 1955
TSO bytes tx: 6536945
ucast pkts tx: 3711838
ucast bytes tx: 346309662
mcast pkts tx: 0
mcast bytes tx: 0
bcast pkts tx: 1186
bcast bytes tx: 49812
pkts tx err: 0
pkts tx discard: 0
drv dropped tx total: 0
too many frags: 0
giant hdr: 0
hdr err: 0
tso: 0
ring full: 0
pkts linearized: 0
hdr cloned: 0
giant hdr: 0
Rx Queue#: 0
LRO pkts rx: 0
LRO byte rx: 0
ucast pkts rx: 5084776
ucast bytes rx: 4673133395
mcast pkts rx: 0
mcast bytes rx: 0
bcast pkts rx: 154143
bcast bytes rx: 45415676
pkts rx OOB: 0
pkts rx err: 0
drv dropped rx total: 0
err: 0
fcs: 0
rx buf alloc fail: 0
Rx Queue#: 1
LRO pkts rx: 0
LRO byte rx: 0
ucast pkts rx: 6346769
ucast bytes rx: 4835534292
mcast pkts rx: 0
mcast bytes rx: 0
bcast pkts rx: 3464
bcast bytes rx: 714646
pkts rx OOB: 0
pkts rx err: 0
drv dropped rx total: 0
err: 0
fcs: 0
rx buf alloc fail: 0
tx timeout count: 0
如何改變以太網(wǎng)設(shè)備的速度
你可以根據(jù)需要改變以太網(wǎng)的速度。當你進行此更改時,網(wǎng)卡將自動掉線,你需要使用 ifup 命令 或 ip
命令或 nmcli
命令將其重新上。
# ethtool -s eth0 speed 100
# ip link set eth0 up
如何在 Linux 上啟用/禁用以太網(wǎng)卡的自動協(xié)商?
你可以使用 ethtool
命令中的 autoneg
選項啟用或禁用自動協(xié)商,如下圖所示:
# ethtool -s eth0 autoneg off
# ethtool -s eth0 autoneg on
如何同時更改多個參數(shù)
如果你想使用 ethtool
命令同時更改以太網(wǎng)卡的多個參數(shù),請使用下面的格式:
Syntax:
ethtool –s [device_name] speed [10/100/1000] duplex [half/full] autoneg [on/off]
# ethtool –s eth0 speed 1000 duplex full autoneg off
如何檢查特定網(wǎng)卡的自動協(xié)商、RX 和 TX
要查看關(guān)于特定以太網(wǎng)設(shè)備的自動協(xié)商等詳細信息,請使用以下格式:
# ethtool -a eth0
如何從多個設(shè)備中識別出特定的網(wǎng)卡(閃爍網(wǎng)卡上的 LED)
如果你想識別一個特定的物理接口,這個選項非常有用。下面的 ethtool
命令會使 eth0
端口的 LED 燈閃爍:
# ethtool -p eth0
如何在 Linux 中永久設(shè)置這些參數(shù)
在系統(tǒng)重啟后,你使用 ethtool
所做的更改將被默認恢復。
要使自定義設(shè)置永久化,你需要更新網(wǎng)絡配置文件中的值。根據(jù)你的 Linux 發(fā)行版,你可能需要將此值更新到正確的文件中。
對于基于 RHEL 的系統(tǒng)。你必須使用 ETHTOOL_OPTS
變量:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
ETHTOOL_OPTS="speed 1000 duplex full autoneg off"
對于基于 Debian 的系統(tǒng):
# vi /etc/network/interfaces
post-up ethtool -s eth0 speed 1000 duplex full autoneg off