Networking

如何像以前在 ifconfig 中看到的那樣從 ip link 獲取 ipv4 地址?

  • April 30, 2019

當我執行 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

顯然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

這可以在更緊湊和使用者友好的 ( -brief) 格式中看到

$ 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

引用自:https://unix.stackexchange.com/questions/516387