手机版 收藏 导航

如何在Unix/Linux上通过命令行查看网络接口的详细信息

原创   www.link114.cn   2025-01-08 21:37:40

如何在Unix/Linux上通过命令行查看网络接口的详细信息

ifconfig是最常见的网络接口查看命令。它可以显示所有的网络接口信息,包括接口名称、IP地址、子网掩码、MAC地址、收发包统计等。使用方法如下:

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:d3:85:90  
          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12345 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6789 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:987654 (963.9 KiB)  TX bytes:123456 (120.6 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

ip命令是ifconfig的替代品,功能更加强大。它可以用于查看、配置网络接口信息。使用方法如下:

$ ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d3:85:90 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever

ethtool命令可以显示网卡的详细信息,如链路状态、协商速率、双工模式、收发包统计等。使用方法如下:

$ ethtool eth0
Settings for eth0:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: on (auto)
    Supports Wake-on: pumbg
    Wake-on: g
    Current message level: 0x00000007 (7)
    Link detected: yes

lshw命令可以列出系统的硬件信息,包括网卡的一些基本信息。使用方法如下:

$ lshw -C network
*-network
   description: Ethernet interface
   product: 82540EM Gigabit Ethernet Controller
   vendor: Intel Corporation
   physical id: 3
   bus info: pci@0000:00:03.0
   logical name: eth0
   version: 02
   serial: 00:0c:29:d3:85:90
   size: 100MB/s
   capacity: 1GB/s
   width: 32 bits
   clock: 66MHz
   capabilities: pm bus_master cap_list ethernet physical
   configuration: autonegotiation=on broadcast=yes driver=e1000 driverversion=7.3.21-k8-NAPI duplex=full ip=192.168.1.100 latency=64 link=yes multicast=yes port=twisted pair speed=100MB/s
   resources: irq:16 memory:c0200000-c0200fff ioport:d820(size=8)

nmcli是NetworkManager提供的命令行工具,可以用于查看和配置网络设置。它提供丰富的功能,如查看网络连接状态、查看网络接口信息等。使用方法如下:

$ nmcli device status
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  Wired connection 1
lo      loopback  unmanaged  --

$ nmcli device show eth0
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:D3:85:90
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     Wired connection 1
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.1.100/24
IP4.GATEWAY:                            192.168.1.1
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 192.168.1.1, mt = 100
IP6.ADDRESS[1]:                         fe80::20c:29ff:fed3:8590/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100

以上就是几种常用的查看Unix/Linux网络接口信息的命令行工具。通过这些工具,我们可以获取网卡的各种详细信息,如IP地址、MAC地址、网速、流量统计等。这对网络管理和故障排查都非常有帮助。