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

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

Matheus Lozano
Bash Tips and Tricks
2 min readNov 30, 2020

--

How to get Linux IP without ipconfig or ip address

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.

--

--

Matheus Lozano
Bash Tips and Tricks

A SysAdmin who love to automate everything — DevSecOps, SRE and Chaos Engineer, let's share our skills.