如何在Linux上檢查網(wǎng)卡信息
默認(rèn)情況下,在設(shè)置服務(wù)器時(shí)你會(huì)配置主網(wǎng)絡(luò)接口。這是每個(gè)人所做的構(gòu)建工作的一部分。有時(shí)出于各種原因,你可能需要配置額外的網(wǎng)絡(luò)接口。
這可以是通過(guò)網(wǎng)絡(luò)綁定/協(xié)作來(lái)提供高可用性,也可以是用于應(yīng)用需求或備份的單獨(dú)接口。
為此,你需要知道計(jì)算機(jī)有多少接口以及它們的速度來(lái)配置它們。
有許多命令可檢查可用的網(wǎng)絡(luò)接口,但是我們僅使用 ip
命令。以后,我們會(huì)另外寫(xiě)一篇文章來(lái)全部介紹這些工具。
在本教程中,我們將向你顯示可用網(wǎng)絡(luò)網(wǎng)卡(NIC)信息,例如接口名稱(chēng)、關(guān)聯(lián)的 IP 地址、MAC 地址和接口速度。
什么是 ip 命令
ip 命令 類(lèi)似于 ifconfig
, 用于分配靜態(tài) IP 地址、路由和默認(rèn)網(wǎng)關(guān)等。
# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether fa:16:3e:a0:7d:5a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.101/24 brd 192.168.1.101 scope global eth0
inet6 fe80::f816:3eff:fea0:7d5a/64 scope link
valid_lft forever preferred_lft forever
什么是 ethtool 命令
ethtool
用于查詢或控制網(wǎng)絡(luò)驅(qū)動(dòng)或硬件設(shè)置。
# ethtool eth0
1)如何在 Linux 上使用 ip 命令檢查可用的網(wǎng)絡(luò)接口
在不帶任何參數(shù)的情況下運(yùn)行 ip
命令時(shí),它會(huì)提供大量信息,但是,如果僅需要可用的網(wǎng)絡(luò)接口,請(qǐng)使用以下定制的 ip
命令。
# ip a |awk '/state UP/{print $2}'
eth0:
eth1:
2)如何在 Linux 上使用 ip 命令檢查網(wǎng)絡(luò)接口的 IP 地址
如果只想查看 IP 地址分配給了哪個(gè)接口,請(qǐng)使用以下定制的 ip
命令。
# ip -o a show | cut -d ' ' -f 2,7
或
ip a |grep -i inet | awk '{print $7, $2}'
lo 127.0.0.1/8
192.168.1.101/24
192.168.1.102/24
3)如何在 Linux 上使用 ip 命令檢查網(wǎng)卡的 MAC 地址
如果只想查看網(wǎng)絡(luò)接口名稱(chēng)和相應(yīng)的 MAC 地址,請(qǐng)使用以下格式。
檢查特定的網(wǎng)絡(luò)接口的 MAC 地址:
# ip link show dev eth0 |awk '/link/{print $2}'
00:00:00:55:43:5c
檢查所有網(wǎng)絡(luò)接口的 MAC 地址,創(chuàng)建該腳本:
# vi /opt/scripts/mac-addresses.sh
#!/bin/sh
ip a |awk '/state UP/{print $2}' | sed 's/://' | while read output;
do
echo $output:
ethtool -P $output
done
運(yùn)行該腳本獲取多個(gè)網(wǎng)絡(luò)接口的 MAC 地址:
# sh /opt/scripts/mac-addresses.sh
eth0:
Permanent address: 00:00:00:55:43:5c
eth1:
Permanent address: 00:00:00:55:43:5d
4)如何在 Linux 上使用 ethtool 命令檢查網(wǎng)絡(luò)接口速度
如果要在 Linux 上檢查網(wǎng)絡(luò)接口速度,請(qǐng)使用 ethtool
命令。
檢查特定網(wǎng)絡(luò)接口的速度:
# ethtool eth0 |grep "Speed:"
Speed: 10000Mb/s
檢查所有網(wǎng)絡(luò)接口速度,創(chuàng)建該腳本:
# vi /opt/scripts/port-speed.sh
#!/bin/sh
ip a |awk '/state UP/{print $2}' | sed 's/://' | while read output;
do
echo $output:
ethtool $output |grep "Speed:"
done
運(yùn)行該腳本獲取多個(gè)網(wǎng)絡(luò)接口速度:
# sh /opt/scripts/port-speed.sh
eth0:
Speed: 10000Mb/s
eth1:
Speed: 10000Mb/s
5)驗(yàn)證網(wǎng)卡信息的 Shell 腳本
通過(guò)此 shell 腳本你可以收集上述所有信息,例如網(wǎng)絡(luò)接口名稱(chēng)、網(wǎng)絡(luò)接口的 IP 地址,網(wǎng)絡(luò)接口的 MAC 地址以及網(wǎng)絡(luò)接口的速度。創(chuàng)建該腳本:
# vi /opt/scripts/nic-info.sh
#!/bin/sh
hostname
echo "-------------"
for iname in $(ip a |awk '/state UP/{print $2}')
do
echo "$iname"
ip a | grep -A2 $iname | awk '/inet/{print $2}'
ip a | grep -A2 $iname | awk '/link/{print $2}'
ethtool $iname |grep "Speed:"
done
運(yùn)行該腳本檢查網(wǎng)卡信息:
# sh /opt/scripts/nic-info.sh
vps.2daygeek.com
----------------
eth0:
192.168.1.101/24
00:00:00:55:43:5c
Speed: 10000Mb/s
eth1:
192.168.1.102/24
00:00:00:55:43:5d
Speed: 10000Mb/s
via: https://www.2daygeek.com/linux-unix-check-network-interfaces-names-nic-speed-ip-mac-address/