修改interfaces后刷新网卡ip_如何在 Linux 上检查网卡信息 | Linux 中国

  9a1a1bd4e12a74b719056e942ad6fab2.png有时出于各种原因,你可能需要配置额外的网络接口。https://linux.cn/article-12165-1.html 作者:Magesh Maruthamuthu 译者:geekpi

默认情况下,在设置服务器时你会配置主网络接口。这是每个人所做的构建工作的一部分。有时出于各种原因,你可能需要配置额外的网络接口。

这可以是通过网络绑定(bonding)/协作(teaming)来提供高可用性,也可以是用于应用需求或备份的单独接口。

为此,你需要知道计算机有多少接口以及它们的速度来配置它们。

有许多命令可检查可用的网络接口,但是我们仅使用 ip 命令。以后,我们会另外写一篇文章来全部介绍这些工具。

在本教程中,我们将向你显示可用网络网卡(NIC)信息,例如接口名称、关联的 IP 地址、MAC 地址和接口速度。

62a9064b-4735-eb11-8da9-e4434bdf6706.svg

什么是 ip 命令

ip 命令 类似于 ifconfig, 用于分配静态 IP 地址、路由和默认网关等。

# ip a1: 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 forever2: 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 62a9064b-4735-eb11-8da9-e4434bdf6706.svg

什么是 ethtool 命令

ethtool 用于查询或控制网络驱动或硬件设置。

# ethtool eth0 62a9064b-4735-eb11-8da9-e4434bdf6706.svg

1)如何在 Linux 上使用 ip 命令检查可用的网络接口

在不带任何参数的情况下运行 ip 命令时,它会提供大量信息,但是,如果仅需要可用的网络接口,请使用以下定制的 ip 命令。

# ip a |awk '/state UP/{print $2}'eth0:eth1: 62a9064b-4735-eb11-8da9-e4434bdf6706.svg

2)如何在 Linux 上使用 ip 命令检查网络接口的 IP 地址

如果只想查看 IP 地址分配给了哪个接口,请使用以下定制的 ip 命令。

# ip -o a show | cut -d ' ' -f 2,7ip a |grep -i inet | awk '{print $7, $2}'lo 127.0.0.1/8192.168.1.101/24192.168.1.102/24 62a9064b-4735-eb11-8da9-e4434bdf6706.svg

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/ship a |awk '/state UP/{print $2}' | sed 's/://' | while read output;do echo $output: ethtool -P $outputdone

运行该脚本获取多个网络接口的 MAC 地址:

# sh /opt/scripts/mac-addresses.sheth0:Permanent address: 00:00:00:55:43:5ceth1:Permanent address: 00:00:00:55:43:5d 62a9064b-4735-eb11-8da9-e4434bdf6706.svg

4)如何在 Linux 上使用 ethtool 命令检查网络接口速度

如果要在 Linux 上检查网络接口速度,请使用 ethtool 命令。

检查特定网络接口的速度:

# ethtool eth0 |grep "Speed:"Speed: 10000Mb/s

检查所有网络接口速度,创建该脚本:

# vi /opt/scripts/port-speed.sh#!/bin/ship a |awk '/state UP/{print $2}' | sed 's/://' | while read output;do echo $output: ethtool $output |grep "Speed:"done

运行该脚本获取多个网络接口速度:

# sh /opt/scripts/port-speed.sheth0:Speed: 10000Mb/seth1:Speed: 10000Mb/s 62a9064b-4735-eb11-8da9-e4434bdf6706.svg

5)验证网卡信息的 Shell 脚本

通过此 shell 脚本你可以收集上述所有信息,例如网络接口名称、网络接口的 IP 地址,网络接口的 MAC 地址以及网络接口的速度。创建该脚本:

# vi /opt/scripts/nic-info.sh#!/bin/shhostnameecho "-------------"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.shvps.2daygeek.com----------------eth0:192.168.1.101/2400:00:00:55:43:5cSpeed: 10000Mb/seth1:192.168.1.102/2400:00:00:55:43:5dSpeed: 10000Mb/s

via: https://www.2daygeek.com/linux-unix-check-network-interfaces-names-nic-speed-ip-mac-address/

作者:Magesh Maruthamuthu 选题:lujun9972 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

6ba9064b-4735-eb11-8da9-e4434bdf6706.svg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值