CentOS 7 获取网络接口 IP 地址

在 CentOS 7 中,您可以通过以下方式获取网络接口的 IP 地址:

1. 使用 ifconfig 命令

ifconfig eth0

执行以上命令后,您将看到如下信息:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1400
        inet 172.16.0.10  netmask 255.255.255.0  broadcast 172.16.0.255
        inet6 fe802::200:ff:feed:1794  prefixlen 64  scopeid 0x20<link>
        ether 00:00:020:ed:17:94  txqueuelen 1000  (Ethernet)
        RX packets 1498405236  bytes 323012226907 (300.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1526483886  bytes 1462494030057 (1.3 TiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

其中,inet 后面的 172.16.0.10 即为 eth0 网络接口的 IP 地址。

2. 使用 Shell 获取 inet 的 IP 内容

您可以使用以下命令获取 inet 的 IP 地址:

ifconfig eth0 | grep 'inet ' | awk '{print $2}'

其中,ifconfig eth0 命令获取 eth0 网卡的信息,grep 'inet ' 命令过滤出 inet 开头的行,awk '{print $2}' 命令输出第二列即为 IP 地址。

注意:

  • 不同系统可能使用不同的网络接口名称,如 eth1wlan0。请根据您的系统情况修改命令中的网络接口名称。
  • ifconfig 命令在某些 Linux 发行版中已被 ip 命令取代。您可以使用 ip addr show eth0 命令代替 ifconfig eth0 命令。
CentOS 7 获取网络接口 IP 地址

原文地址: https://www.cveoy.top/t/topic/j44k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录