48

Is there a clean, simple way to get an IP address for a network interface from /proc, similar to the way I can get the MAC address for a network interface?
Ideally I would just type cat /proc/<foo>/{interface_name} and get the IPv4 address.

I'd rather not run anything other than cat.

5 Answers 5

63

Under the /proc directory, you can also find the IPv4 addresses in the Forwarding Information Base table, at /proc/net/fib_trie

The table is pretty intelligible doing a mere cat, first comes the Main: and then Local:

cat /proc/net/fib_trie

or to see your network, IP addresses and netmask:

cat /proc/net/fib_trie | grep "|--"   | egrep -v "0.0.0.0| 127."
       |-- 193.136.1.0
       |-- 193.136.1.2
    |-- 193.136.1.255
       |-- 193.136.1.0
       |-- 193.136.1.2
    |-- 193.136.1.255       
10
  • 1
    Is there a way to get the interface for each IP address?
    – razeh
    May 15, 2017 at 18:18
  • They appear in a pre-determined order; probably priority, but that can be changed; other than that, no. May 15, 2017 at 18:27
  • 4
    /proc/net/fib_trie isn't available on my RHEL 6.6 system here at work
    – villapx
    May 15, 2017 at 18:47
  • 2
    @villapx fib_trie is not present on 2.6 kernels. Sep 27, 2018 at 15:07
  • There's no way to associate ip addresses with interface in fib_trie.
    – ZioByte
    Mar 10, 2019 at 21:38
7
$ awk '/32 host/ { print f } {f=$2}' <<< "$(</proc/net/fib_trie)"
6
  • 2
    Welcome on U&L! Why <<< "$(</proc/net/fib_trie)" instead of a simple </proc/net/fib_trie?
    – fra-san
    May 31, 2019 at 10:11
  • 3
    @fra-san: awk '/32 host/ { print f } {f=$2}' /proc/net/fib_trie works the same Feb 10, 2020 at 11:49
  • 3
    there is no ip command many of official docker containers, then your answer solved my problem. awk '/32 host/ { print f } {f=$2}' /proc/net/fib_trie | sort | uniq | grep -v 127.0.0.1
    – edib
    Jun 13, 2020 at 20:10
  • 1
    @God could you pl explain the awk syntax? The /32 host/ would match lines containing that 32 host. For me, these are lines like 32 host LOCAL. $2 would return the 2nd field from the line... so, what does print f do? ... and what would be its value? Why does f=$2 come after print 2. (I thought I knew awk enough.)
    – Harry
    Jul 24, 2021 at 2:32
  • 2
    @Harry It basically does this: For every line it saves $2 in f. But if the line contains "32 host" then print f that was saved from the previous line. After printing f, save $2 again
    – Wodin
    Oct 19, 2021 at 10:43
6

It might be possible to associate network addresses from /proc/net/fib_trie with interfaces from /proc/net/route:

  $ awk '/^[^I]/ {print $1 " " $2}' /proc/net/route
  br0 0001020A
  br0 FE01020A

..and to convert the hexadecimal network addresses with:

$ echo FE01020A | xxd -r -p | hexdump -e '/1 "%u."' | tac -s'.' | sed 's/\.$//'
10.2.1.254
1

I just posted this similar answer on the Ubuntu community:

If you're lucky (and hostnames are set up properly), hostname -i will provide the local IP address. If not, or if you have multiple IP addresses, it will probably return the localhost address 127.0.0.1. The command hostname -I will report all machine-local IP (v4 and v6) addresses.

0

echo $'g/32 host LOCAL/-1s/\s*|-- //p\n Q'|ed -s /proc/net/fib_trie

10.0.0.20
127.0.0.1
192.168.122.1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .