How to extract eth0 interface name from eth0@if10 in a kubernetes container using ip addr

5/8/2018

running ip addr:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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
3: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc noqueue state UP group default
    link/ether 0a:58:0a:00:00:07 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.0.0.7/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::e837:feff:fe5c:c912/64 scope link
       valid_lft forever preferred_lft forever

Is there some way that ip addr will return eth0 instead of eth0@if10 ? I saw ifconfig does list it as eth0 and not eth0@if10

-- Mika R
containers
ip
kubernetes
linux

1 Answer

5/8/2018

If you just withs to get the entries left of the @, then use the following.

ip a | cut -sf1 -d@

You can add more filters before if you want to target specific interfaces.

-- xen-dara
Source: StackOverflow