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