• Matheus Lozano

Getting the Linux IP address without any package (ifconfig, ip address, etc)

Updated: Jan 21

How to get Linux IP address, netmask and gateway without ifconfig or ip commands

How to get Linux IP without ipconfig or ip address

Replica: https://medium.com/bash-tips-and-tricks/getting-the-linux-ip-address-without-any-package-ifconfig-ip-address-etc-7b1363077964

bash: ip: command not found bash: ifconfig: command not found

Explaining how it works

How many times you tried to debug/troubleshoot a Linux with an unprivileged user and you didn’t have net tools or iproute installed? Well, the good news is: You can do it with Bash (ok ok, this not a bash feature :’), without having to install any package.

Many information displayed by those tools are available by the Kernel and most of them doesn’t require a privileged user. Cool, right?

In order to understand how this is possible, we’re going to explore~very shortly~ the IPv4 Routing Subsystem, in specifically the Forwarding Information Base trie (Aka FIB trie). The FIB trie is the main data structure used by the IPv4, it defines the routing trie and can be used by us to collect our IP addresses, gateway IP, netmask, etc.


fibtrie example - Showing the IP address, netmaks and gateway
Routing trie till 192.0.2.48/32 — Credits vincent.bernat.ch

As I said, very sort :D. Now let’s get our IPs!

The file that contains the routing trie is the /proc/net/fib_trie and you can use the commands below to filter and get only the desired IPs.

It’s important to notice that the first command is removing the *.0 and *.255, if you have a network bigger than /24 (netmask 255.255.255.0) you may not get your desired IPs.

Read More IPv4 route lookup on Linux by Vincent Bernat The IPv4 Routing Subsystem FIB trie Competitive FIB Aggregation for Independent Constant IP Lookup With FIB Explosion Trie - Wikipedia

0 comments

Recent Posts

See All