
有時出于各種原因,你可能需要配置額外的網絡接口。
- 來源:https://linux.cn/article-12165-1.html
- 作者:Magesh Maruthamuthu
- 譯者:geekpi
預設情況下,在設定伺服器時你會配置主網絡接口。這是每個人所做的建構工作的一部分。有時出于各種原因,你可能需要配置額外的網絡接口。
這可以是通過網絡 綁定(bonding) / 協作(teaming)來提供高可用性,也可以是用于應用需求或備份的單獨接口。
為此,你需要知道計算機有多少接口以及它們的速度來配置它們。
有許多指令可檢查可用的網絡接口,但是我們僅使用
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