5

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?

closed as unclear what you're asking by Braiam, Anthon, Networker, John WH Smith, jimmij Jan 9 '15 at 1:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What language are you using? Usually there is a language specific method. – Mark Wagner Jan 8 '15 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 '15 at 20:42
  • 1
    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. – Crunchex Jan 8 '15 at 22:32
5

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 '15 at 22:21
  • @Random832, from the link that I have referred to, it says little endian. – Ramesh Jan 8 '15 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 '15 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 '15 at 22:30
  • /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 '15 at 10:57
4

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 127.0.0.1, 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 == "0.0.0.0" {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 8.8.8.8. In one short one-liner: ip route get 8.8.8.8 | sed -nr 's/.*src ([0-9.]+).*/\1/p' – Digital Trauma Jan 9 '15 at 1:37
  • 1
    ifconfig not always there, e.g. not on CentOS 7.0 – Dirk Jan 9 '15 at 5:11
1

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 192.168.1.2), you could pick one of these (perhaps the last one in case it changes frequently)

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

1

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

# tail -14 /var/lib/dhcp/dhclient.eth0.leases 
lease {
  interface "eth0";
  fixed-address 192.168.0.13;
  option subnet-mask 255.255.255.0;
  option routers 192.168.0.1;
  option dhcp-lease-time 604800;
  option dhcp-message-type 5;
  option domain-name-servers 75.75.75.75,75.75.76.76;
  option dhcp-server-identifier 192.168.0.1;
  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

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

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