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?
-
1What language are you using? Usually there is a language specific method.– Mark WagnerJan 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.– BraiamJan 8, 2015 at 20:42
-
2I 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.– CrunchyJan 8, 2015 at 22:32
-
See unix.stackexchange.com/a/48315– Stéphane ChazelasJan 9, 2015 at 0:08
4 Answers
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
.
-
-
@Random832, from the link that I have referred to, it says little endian.– RameshJan 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 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 :)– RameshJan 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
.– kasperdJan 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 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'
Jan 9, 2015 at 1:37 -
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 afixed-address xxx.xxx...
line that looks like the pasted file from linuxfan's answerentries 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
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