如何在Linux上檢查網卡信息
默認情況下,在設置服務器時你會配置主網絡接口。這是每個人所做的構建工作的一部分。有時出于各種原因,你可能需要配置額外的網絡接口。
這可以是通過網絡綁定/協作來提供高可用性,也可以是用于應用需求或備份的單獨接口。
為此,你需要知道計算機有多少接口以及它們的速度來配置它們。
有許多命令可檢查可用的網絡接口,但是我們僅使用 ip
命令。以后,我們會另外寫一篇文章來全部介紹這些工具。
在本教程中,我們將向你顯示可用網絡網卡(NIC)信息,例如接口名稱、關聯的 IP 地址、MAC 地址和接口速度。
什么是 ip 命令
ip 命令 類似于 ifconfig
, 用于分配靜態 IP 地址、路由和默認網關等。
# 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
用于查詢或控制網絡驅動或硬件設置。
# ethtool eth0
1)如何在 Linux 上使用 ip 命令檢查可用的網絡接口
在不帶任何參數的情況下運行 ip
命令時,它會提供大量信息,但是,如果僅需要可用的網絡接口,請使用以下定制的 ip
命令。
# ip a |awk '/state UP/{print $2}'
eth0:
eth1:
2)如何在 Linux 上使用 ip 命令檢查網絡接口的 IP 地址
如果只想查看 IP 地址分配給了哪個接口,請使用以下定制的 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 命令檢查網卡的 MAC 地址
如果只想查看網絡接口名稱和相應的 MAC 地址,請使用以下格式。
檢查特定的網絡接口的 MAC 地址:
# ip link show dev eth0 |awk '/link/{print $2}'
00:00:00:55:43:5c
檢查所有網絡接口的 MAC 地址,創建該腳本:
# 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
運行該腳本獲取多個網絡接口的 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 命令檢查網絡接口速度
如果要在 Linux 上檢查網絡接口速度,請使用 ethtool
命令。
檢查特定網絡接口的速度:
# ethtool eth0 |grep "Speed:"
Speed: 10000Mb/s
檢查所有網絡接口速度,創建該腳本:
# 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
運行該腳本獲取多個網絡接口速度:
# sh /opt/scripts/port-speed.sh
eth0:
Speed: 10000Mb/s
eth1:
Speed: 10000Mb/s
5)驗證網卡信息的 Shell 腳本
通過此 shell 腳本你可以收集上述所有信息,例如網絡接口名稱、網絡接口的 IP 地址,網絡接口的 MAC 地址以及網絡接口的速度。創建該腳本:
# 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
運行該腳本檢查網卡信息:
# 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/