3

How do I get all interface and associate IP address like following

[root@centso ]# ifconfig
enp3s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet addr:10.5.2.10  Bcast:10.5.7.255  Mask:255.255.248.0
        inet6 fe80::e611:5bff:feea:5e50  prefixlen 64  scopeid 0x20<link>
        ether e4:11:5b:ea:5e:50  txqueuelen 1000  (Ethernet)
        RX packets 638000416  bytes 763371981799 (710.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16607280  bytes 9787019600 (9.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp3s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether e4:11:5b:ea:5e:52  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp4s0f0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether e4:11:5b:ea:5e:44  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp4s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether e4:11:5b:ea:5e:46  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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 1  (Local Loopback)
        RX packets 45015  bytes 4371658 (4.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45015  bytes 4371658 (4.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I want output like following, also I believe there is a command ip link or something does that but didn't recall, also some machine has nic name different like enoX or ethX

enp3s0f0: 10.5.2.10
enp3s0f1:
enp4s0f0:
enp4s0f1:
  • 1
    Don't use ifconfig, use ip addr instead, it will show you additional IPv4 addresses that may have been associated to an interface. ifconfig only gives you the primary IPv4 address. – xhienne Aug 2 '17 at 19:12
4

I think i found something, i am sure there must be more smart way but for now i am all set.

[root@server1 ~]# ip -o -4 addr show | awk '{print $1" " $2": "$4}'
1: lo: 127.0.0.1/8
2: eno1: 192.168.100.190/24
3: eno2: 10.5.8.33/21
| improve this answer | |
1

Try this:

ip addr show | awk -F '[: ]+' '
    /^[^ ]/      { iface=$2 }               # Start of section, we memorize the interface name
    $2 == "inet" { print iface ": " $3 }    # IPv4 address => print the line
'
| improve this answer | |
1

There is an example:

ip address show | awk '/inet / {split($2,var,"/*"); print $7,":",var[1]}'

the output should be somthing like:

 : 127.0.0.1
enp3s0f0: 10.5.2.10
enp3s0f1: .......
enp4s0f0: .......
enp4s0f1: .......
| improve this answer | |
1

This gives a report in the format you requested:

# ip -o -4 addr show | awk '{print $2": "$4}'
| improve this answer | |
  • Hi, what else your answer is adding to this previous answer? – αғsнιη Aug 3 '17 at 17:30
  • It's simpler and conforms to the requested report format. I've edited a previous answer but it seems it hasn't passed the review. – niglesias Aug 3 '17 at 17:32
1

This command displays the all the interfaces and their respective ip adress separated by a space in a line

ip r|grep " src "|cut -d " " -f 3,12 | xargs

reference: https://stackoverflow.com/questions/38041163/display-only-network-interface-and-ip-address-using-ifconfig-and-awk

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.