如何从“ip link”获取 ipv4 地址,就像我以前使用 ifconfig 看到的那样?

如何从“ip link”获取 ipv4 地址,就像我以前使用 ifconfig 看到的那样?

当我运行 ip 来获取 IP 地址时,我得到

$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> 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: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 6c:88:14:ba:cb:cc brd ff:ff:ff:ff:ff:ff

这些都不是 ipv4 地址,但ifconfig确实显示了它,

$ sudo /sbin/ifconfig      
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1102801  bytes 74417671 (70.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1102801  bytes 74417671 (70.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.7.7.57  netmask 255.255.252.0  broadcast 10.7.7.255
        inet6 fe80::440:3794:6794:8b1b  prefixlen 64  scopeid 0x20<link>
        inet6 2620:0:28a2:4010:2:2:8c75:f8a1  prefixlen 128  scopeid 0x0<global>
        ether 6c:88:14:ba:cb:cc  txqueuelen 1000  (Ethernet)
        RX packets 32743430  bytes 48351612590 (45.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14856403  bytes 1590947780 (1.4 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

如何获取 ipv4 信息而不陷入我不推荐使用(且值得信赖)的情况ifconfig

答案1

显然ip破坏了 MAC 地址(现在在ip link(设备)接口中)和网络ip address。该命令ip address显示网络地址,

1: lo: <LOOPBACK,UP,LOWER_UP> 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: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 6c:88:14:ba:cb:cc brd ff:ff:ff:ff:ff:ff
    inet 10.7.7.57/22 brd 10.7.7.255 scope global dynamic noprefixroute wlp3s0
       valid_lft 1203509sec preferred_lft 1203509sec
    inet6 2620:0:28a2:4010:2:2:8c75:f8a1/128 scope global dynamic noprefixroute 
       valid_lft 1203512sec preferred_lft 598712sec
    inet6 fe80::440:3794:6794:8b1b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

-br这可以以更紧凑和用户友好的 ( ief) 格式看到

$ ip -4 -br addr show
lo               UNKNOWN        127.0.0.1/8
wlp3s0           UP             10.7.7.57/22

或者,您可以看到它一行 ( -o),

$ ip -o address
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
1: lo    inet6 ::1/128 scope host \       valid_lft forever preferred_lft forever
3: wlp3s0    inet 10.7.7.57/22 brd 10.7.7.255 scope global dynamic noprefixroute wlp3s0\       valid_lft 1202464sec preferred_lft 1202464sec
3: wlp3s0    inet6 2620:0:28a2:4010:2:2:8c75:f8a1/128 scope global dynamic noprefixroute \       valid_lft 1202466sec preferred_lft 597666sec
3: wlp3s0    inet6 fe80::440:3794:6794:8b1b/64 scope link noprefixroute \       valid_lft forever preferred_lft forever

相关内容