大网数据广告活动
服务器运维问答

Using `ip address show type` to display physical network interface

· 52CE 我爱测

Using Ubuntu 20.04. I have learnt in How to get only the name of the physical ethernet interface? that to display only the physical network interfaces, we could do ls -l /sys/class/net/ | grep -v virtual I tried (unsuccessfully) …

问题原文

原帖链接 · Polymerase · CC BY-SA 4.0

Using Ubuntu 20.04. I have learnt in How to get only the name of the physical ethernet interface? that to display only the physical network interfaces, we could do

ls -l /sys/class/net/ | grep -v virtual

I tried (unsuccessfully) to obtain the same info using ip address show type XXX. According to ip address help the possible types are:

TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |
          bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |
          gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |
          nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |
          hsr | macsec | netdevsim }

Q1. Is it possible to use ip address to show only the physical interfaces? For example, to see the local IP addresses of the machine.

Q2. Maybe there is a good reason for not having a type corresponding to physical interface. What is the rationale?

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

The interface type is more a link property than an address property even if this information can be available (but not always) in ip address's output. So the command to use to answer the title of OP's question would be ip link show...

...but an hardware interface has no type associated. Only the virtual interfaces get this simply because it's an additional property that doesn't have a real interface (this answers Q2, but see note at the end). So there's no type allowing to display the physical ones, and as there's no negation available in the filter options either (nor an expression to display any type to negate upon), there's no filter available in the command syntax to display only such interfaces.

To find those interfaces, you must display all of them, and filter out those without type with an external filter. So what can't be done with the ip link command alone, can be done by filtering its output, with an adequate tool: jq using ip -details -json link's JSON output, for reliable parsing. There's this jq documentation to learn how to use it, the man page is based on its contents.

From the full list of interface this has to be done:

  • exclude any type found (in the JSON output that's the info_kind property of the linkinfo, which can be all of the type from the help (vlan, veth, ...), not to be confused with link_type which can be for example loopback, ether, none ...).
  • exclude the loopback link_type.

which translates into:

ip -details -json link show | jq -r '
.[] | 
      if .linkinfo.info_kind // .link_type == "loopback" then
          empty
      else
          .ifname
      end
'

For example, on this system with currently a total of 12 interfaces, only those two, the only two real, are displayed with the command above:

eth0
wlan0

Fell free to improve this jq filter.

To address question Q1, for this case, replacing ip ... link with ip ... address will give the same output. But to display the addresses in addition to the interfaces, of course the filter has to change (and requires a recent enough jq command to allow custom handling of linefeeds):

ip -details -json address show | jq --join-output '
.[] | 
      if .linkinfo.info_kind // .link_type == "loopback" then
          empty
      else
          .ifname ,
          ( ."addr_info"[] |
              if .family == "inet" or .family == "inet6" then
                  " " + .local
              else
                  empty
              end
          ),
          "\n"
      end
'

If you want to check the whole JSON output directly, try ip -details -pretty -json address or ip -d -j address | jq .. The -json option is available for several iproute2 commands, but not all of them. Its addition for the ip ... commands is quite recent (mid 2017).


Note: actually the type keyword can translate to multiple different types of properties when used in JSON: .linkinfo.info_kind is the actual type of interface as described before, but there is also .linkinfo.info_slave_kind for an interface (including a real interface) that has a bridge or a bond as master (resp. bridge_slave and bond_slave). This won't change JSON results since it won't check this property.

Anyway such interfaces should never have an IP address set to them then: the address should be set to the master, which despite being virtual, should be the interface that matters when interacting with the physical environment around.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。