Networking

使用 ip 命令僅顯示具有 ip 地址的網路介面

  • September 10, 2018

我使用ipa 來獲取我的機器的 IP 地址。有時,當我有 20 多個網路介面時,會變得難以辨識。

有沒有辦法只顯示具有 IP 的網路介面?沒有 IP 地址的網路介面應該被忽略並且不應該顯示。

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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 4c:cc:6a:32:42:0b brd ff:ff:ff:ff:ff:ff
inet 10.76.99.232/23 brd 10.76.99.255 scope global noprefixroute dynamic eno1
  valid_lft 81438sec preferred_lft 81438sec
inet6 fe80::4cd3:b92f:e2:54c8/64 scope link noprefixroute 
  valid_lft forever preferred_lft forever
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:67:f0:89 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
  valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:67:f0:89 brd ff:ff:ff:ff:ff:ff   
5: virbr1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:8e:77:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24 brd 192.168.100.255 scope global virbr1
  valid_lft forever preferred_lft forever
6: virbr1-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr1 state DOWN group default qlen 1000
link/ether 52:54:00:8e:77:61 brd ff:ff:ff:ff:ff:ff
7: br-251ee4bbddbc: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
link/ether 02:42:59:14:5d:96 brd ff:ff:ff:ff:ff:ff
inet 172.20.0.1/16 brd 172.20.255.255 scope global br-251ee4bbddbc
  valid_lft forever preferred_lft forever
inet6 fe80::42:59ff:fe14:5d96/64 scope link 
  valid_lft forever preferred_lft forever
8: br-7d1010889446: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
link/ether 02:42:e9:35:47:a7 brd ff:ff:ff:ff:ff:ff
inet 172.19.0.1/16 brd 172.19.255.255 scope global br-7d1010889446
  valid_lft forever preferred_lft forever
9: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
link/ether 02:42:a2:bf:15:a8 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  valid_lft forever preferred_lft forever
inet6 fe80::42:a2ff:febf:15a8/64 scope link 
  valid_lft forever preferred_lft forever
11: veth42032d5@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-251ee4bbddbc state UP group default 
link/ether 56:1f:89:31:03:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::541f:89ff:fe31:302/64 scope link 
  valid_lft forever preferred_lft forever 
13: veth6b578c7@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
link/ether da:12:96:51:48:48 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::d812:96ff:fe51:4848/64 scope link 
  valid_lft forever preferred_lft forever
15: veth36b9b3b@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-251ee4bbddbc state UP group default 
link/ether 9e:89:cb:57:36:67 brd ff:ff:ff:ff:ff:ff link-netnsid 2
inet6 fe80::9c89:cbff:fe57:3667/64 scope link 
  valid_lft forever preferred_lft forever
34: br-22ddff6ae6ef: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
link/ether 02:42:d7:77:d5:2d brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global br-22ddff6ae6ef
  valid_lft forever preferred_lft forever

預期輸出(大約):

1. eno1: 10.76.99.232 
3. virbr0: 192.168.122.1
5. virbr1: 192.168.100.1 

等等

我正在尋找使用ip工具進行過濾的任何本機方式。使用其他工具來獲得預期的輸出也值得讚賞。

ip -4 -brief address show

將顯示最少的地址資訊,順便只顯示帶有 IPv4 地址的介面,因為被告知只顯示 IPv4(即:不顯示乙太網或 IPv6 地址)。

這個選項最近(2015 年)被添加到 iproute2 實用程序中,並且需要時間來轉移到發行版。例如,它是:

  • 在 Debian 9 中可用,但在 Debian 8 中不可用
  • 在 RHEL 7.5 中可用,但在某些以前的 RHEL 7 版本中不可用。

上游送出:

添加對連結和地址的簡短輸出的支持

輸出可能開始於:

$ ip -4 -brief address show
lo               UNKNOWN        127.0.0.1/8 
eno1             UP             10.76.99.232/23
virbr0           UP             192.168.122.1/24 
virbr1           UP             192.168.100.1/24 

引用自:https://serverfault.com/questions/930142