I'm writing a program that needs to know the system's current IPv4 address (received via DHCP), if it's connected and has one. For the sake of argument, the language is irrelevant and I must read from a file on the disk. Is there such a file that always stores the current IPv4 address?

  • 1
    What language are you using? Usually there is a language specific method. Jan 8, 2015 at 19:37
  • If you tell us what language it is, we could suggest to use that language API instead of trying to read it from a file.
    – Braiam
    Jan 8, 2015 at 20:42
  • 2
    I happen to be using Dart, though golang would also be of interest. I wanted to keep this question somewhat generic and language agnostic, though, if there was a method that only relied on the filesystem.
    – Crunchy
    Jan 8, 2015 at 22:32
  • See unix.stackexchange.com/a/48315 Jan 9, 2015 at 0:08

4 Answers 4


There is a file /proc/net/tcp which stores the IP address in the little-endian four-byte hexadecimal number format. However, this has the assumption that a session is open to find the IP address. Other than that, you could use ip addr show command as well.

You need to reverse the string to get the IP address. Refer to this answer on how to get the output from the file /proc/net/tcp.

  • Is it little-endian or machine-endian?
    – Random832
    Jan 8, 2015 at 22:21
  • @Random832, from the link that I have referred to, it says little endian.
    – Ramesh
    Jan 8, 2015 at 22:25
  • Looks like it's actually a big-endian value interpreted as the machine endianness, which means it ends up reversed on little-endian machines. lxr.free-electrons.com/source/net/ipv4/tcp_ipv4.c
    – Random832
    Jan 8, 2015 at 22:29
  • @Random832, thanks for the info. If you feel, the answer needs to be modified, you can go ahead and correct it :)
    – Ramesh
    Jan 8, 2015 at 22:30
  • 2
    /proc/net/tcp will only show you TCP connections and listening TCP sockets. If you have an IP address which no socket is directly bound to and which is not currently used by a TCP connection, it will not show up in /proc/net/tcp.
    – kasperd
    Jan 9, 2015 at 10:57

There's no file on disk that's guaranteed to contain the current IP address. If you obtained your IP address through DHCP, the DHCP probably wrote the address somewhere, but there is no standard location.

The normal, portable way to obtain the current IP address would be to parse the output of ifconfig or (Linux-only) ip addr show. Note that in addition to the address of the primary Internet connection, there is also the loopback address, and often there are more (for internal networks, virtual machines, etc.). A good hint is to retrieve the address of the interface that provides the first default route.

default_interface=$(route -n | awk '$1 == "" {print $8; exit}')
ip_address=$(ifconfig "$default_interface" | awk 'sub(/.* inet addr:/, "") {print $1}')
  • You could do an ip route get on a fairly well-known IP address which will give you the local source addr. e.g. Google's DNS server is In one short one-liner: ip route get | sed -nr 's/.*src ([0-9.]+).*/\1/p' Jan 9, 2015 at 1:37
  • 1
    ifconfig not always there, e.g. not on CentOS 7.0
    – Dirk
    Jan 9, 2015 at 5:11

I had a look at Linux Mint's /var/lib/dhcp/ (based on Ubuntu 14.04) folder and the only file there was empty.

But searching every file, I did find:

  • /var/lib/NetworkManager/dhclient-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx-eth0.lease
    (with the x's nearly random letters & numbers) with a fixed-address xxx.xxx... line that looks like the pasted file from linuxfan's answer

  • entries in /var/log/syslog with the IP address (for example here, you could pick one of these (perhaps the last one in case it changes frequently)

    Jan 8 17:49:45 mint dhclient: DHCPREQUEST of on eth0 to port 67 (xid=0xXXXXXXXX)
    Jan 8 17:49:45 mint dhclient: DHCPOFFER of from
    Jan 8 17:49:45 mint dhclient: DHCPACK of from
    Jan 8 17:49:45 mint dhclient: bound to -- renewal in 38149 seconds.
    Jan 8 17:49:45 mint NetworkManager[1363]: address
    Jan 8 17:49:45 mint avahi-daemon[1117]: Joining mDNS multicast group on interface eth0.IPv4 with address
    Jan 8 17:49:45 mint avahi-daemon[1117]: Registering new address record for on eth0.IPv4.

Or, if you're using a bash or other shell script or something shell-friendly, here's a pipe-friendly grep & cut way to get the ip from ifconfig, change eth0 to whichever you prefer, or even -a for all.

ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d ' ' -f 1

It doesn't take much more time than grep-ing a single file, the above takes real 0m0.002s, user 0m0.000s, sys 0m0.000s while grep-ing one file takes real 0m0.001s, user 0m0.000s, sys 0m0.000s


All DHCP lease information is saved in /var/lib/dhcp/dhclient.eth0.leases

# tail -14 /var/lib/dhcp/dhclient.eth0.leases 
lease {
  interface "eth0";
  option subnet-mask;
  option routers;
  option dhcp-lease-time 604800;
  option dhcp-message-type 5;
  option domain-name-servers,;
  option dhcp-server-identifier;
  option domain-name "hsd1.ca.comcast.net.";
  renew 6 2015/01/10 10:38:07;
  rebind 2 2015/01/13 16:19:22;
  expire 3 2015/01/14 13:19:22;

Having said that, it doesn't hurt to also parse the output of ifconfig

  • 4
    My /var/lib/dhcp/dhclient.leases is empty, no other files (Linux Mint connected on eth0)
    – Xen2050
    Jan 8, 2015 at 19:28
  • For that DHCP client. There are many.
    – nobody
    Jan 9, 2015 at 0:39
  • I mean there are no other files in /var/lib/dhcp/ at all, except the one empty file.
    – Xen2050
    Jan 9, 2015 at 2:58
  • 1
    ifconfig not always there, e.g. not on CentOS 7.0
    – Dirk
    Jan 9, 2015 at 5:10

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