手机版 收藏 导航

如何在 Linux 中查看 IP 和物理地址

原创   www.link114.cn   2024-01-25 18:33:28

如何在 Linux 中查看 IP 和物理地址

使用 ifconfig 命令

ifconfig 命令是 Linux 系统中最常用的网络配置工具之一。它可以用来查看网卡的 IP 地址、子网掩码、网关等信息。只需在终端输入 ifconfig 命令即可。

$ ifconfig
eth0: flags=4163  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::215:5dff:fe26:4011  prefixlen 64  scopeid 0x20
        ether 00:15:5d:26:40:11  txqueuelen 1000  (Ethernet)
        RX packets 1234567  bytes 123456789 (123.4 MiB)
        TX packets 987654  bytes 98765432 (94.1 MiB)

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 12345  bytes 1234567 (1.2 MiB)
        TX packets 12345  bytes 1234567 (1.2 MiB)

从输出结果中可以看到,这台 Linux 机器有两个网卡:一个是物理网卡 eth0,IP 地址为 192.168.1.100;另一个是虚拟网卡 lo(loopback),IP 地址为 127.0.0.1。

使用 ip 命令

除 ifconfig,Linux 系统还提供另一个常用的网络配置工具 ip 命令。它可以更精细地管理网络接口、路由、防火墙等。使用 ip 命令查看 IP 地址的方法如下:

$ ip addr show
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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:15:5d:26:40:11 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
       valid_lft 86387sec preferred_lft 86387sec
    inet6 fe80::215:5dff:fe26:4011/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

从输出结果中可以看到,eth0 网卡的 IP 地址是 192.168.1.100,lo 网卡的 IP 地址是 127.0.0.1。

使用 ifconfig 命令

上面我们已经看到,在 ifconfig 命令的输出中,可以找到网卡的物理地址(MAC 地址)。对于 eth0 网卡,它的物理地址是 00:15:5d:26:40:11。

使用 ip 命令

同样,我们也可以使用 ip 命令来查看网卡的物理地址:

$ ip link show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:15:5d:26:40:11 brd ff:ff:ff:ff:ff:ff

从输出结果中可以看到,eth0 网卡的物理地址(MAC 地址)是 00:15:5d:26:40:11。

使用 arp 命令

除 ifconfig 和 ip 命令,我们还可以使用 arp 命令来查看 IP 地址和物理地址的对应关系。arp 命令可以显示系统 ARP 缓存中保存的 IP 地址和对应的物理地址。

$ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.1.1              ether   00:00:00:00:00:00   C                     eth0
192.168.1.100            ether   00:15:5d:26:40:11   C                     eth0

从输出结果中可以看到,IP 地址 192.168.1.100 对应的物理地址是 00:15:5d:26:40:11。

在 Linux 系统中查看 IP 地址和物理地址有多种方法。ifconfig 和 ip 命令是最常用的两种方法,它们可以快速地获取网络接口的详细信息,包括 IP 地址和物理地址。arp 命令则可以查看系统 ARP 缓存中保存的 IP 地址和对应的物理地址。无论使用哪种方法,掌握这些基本的网络命令对于排查网络问题和管理 Linux 系统都很有帮助。