Detecting a change of IP address in Linux

31,485

Solution 1

This is an old question, but I will answer for those who will arrive by Google (such as myself). After struggling for a while, I found out that you don't necessarily need to poll or hack a C solution for this. For my case, I wanted to update my home server's (dynamic dns) domain when the IP changes.

If you are running dhcpcd, you are in luck. dhcpcd will run hook scripts when anything happens. See man dhcpcd-run-hooks (online here). Basically you will want to modify or create your own dhcpcd.enter-hook or dhcpcd.exit-hook depending on what you want to do with the data provided by the event.

Solution 2

The command

ip monitor

will show you this kind of thing happening. It uses some the netlink API which is rather tricky and not documented well (at least for humans to understand). However, it is able to get notified by the kernel of various events, such as changes of assigned IPs, routing tables and link status (e.g. someone unplugged the network)

Solution 3

Since DHCP activity is sent to syslogd you could create a named pipe, direct syslog traffic to it and watch the stream for IP address updates. See 'man syslogd' and 'man syslog.conf'.

Edit: Another approach would be to use inotify to monitor the DHCP leases file for the interface. Under Ubuntu 9.10 that is in the /var/lib/dhcp3 directory.

Solution 4

What I thought of was running this script from cron every 10 or so minutes, depending on your link. If I wrote this right, it only nsupdates when there is an IP change, so no undue load is creater on the zone's master server.

#!/bin/bash

OLD_IP=`cat ip.txt`

NEW_IP=`/sbin/ifconfig  | awk -F "[: ]+'{ print $4}'` #adapted from something I got from the internets.

if [ $NEW_IP != OLD_IP ]; then
    nsupdate <commands> #it seems like the keys need to be in the same directory from where nsupdate was called
fi

echo $NEW_IP > ip.txt

exit 0 #not sure if this is necessary

Not tested!

Solution 5

This is an older thread but in case someone finds it like I did, I wrote something that does network change detection/notification in Linux awhile back (mostly targeted at helping VPN users), and thanks to some pushy friends I put it up for others to use. It's a pet project now and I'm actively maintaining it, so feature requests and feedback are welcome.

http://code.google.com/p/ipcheck/source/browse/ipcheck.sh

Share:
31,485

Related videos on Youtube

Joel Holdsworth
Author by

Joel Holdsworth

Updated on July 09, 2022

Comments

  • Joel Holdsworth
    Joel Holdsworth almost 2 years

    Does anyone know a way to detect a change of IP address in Linux. Say I have dhcpcd running, and it assigns a new IP address, is there a way I can get a notification when it changes? I can't use D-Bus, because this is an embedded ucLinux build that doesn't have it.

    inotify on something in /proc/net/ ?

  • Joel Holdsworth
    Joel Holdsworth almost 14 years
    D-bus would be nice, but this is embedded ucLinux. Sorry I should have made that clear.
  • Joel Holdsworth
    Joel Holdsworth almost 14 years
    That looks good - very good; thank you... but not available on the BusyBox version of ip in ucLinux!
  • Joel Holdsworth
    Joel Holdsworth almost 14 years
    The solution I think I'll go with is to inotify monitor /proc/net/route , and then use the SIOCGIFADDR ioctl to query the address every time the routing table changes.
  • Snowhare
    Snowhare almost 14 years
    You probably can't monitor /proc files using inotify. /proc isn't 'actually' a file system but rather a kernel interface.
  • Felipe
    Felipe over 4 years
    what do you do when you're behind a NAT? your IP is fixed but your global one changes, how can you detect that?
  • Kamafeather
    Kamafeather over 3 years
    @Felipe you can't do that with DHCPcd; you might find it in some other way, maybe running something still in dhcpcd-run-hooks, but in general is not dhcpcd responsibility to know the public IP.
  • Fuseteam
    Fuseteam about 3 years
    i actually came here by googling how to detect when a the public IP changes
  • EdFred
    EdFred about 2 years
    There is the $ missing for the OLD_IP. This helped me a lot, i changed it to get the external IP of my Home Internet and write it to nsupdate.info to get a free dynDNS. It might help someone so: OLD_IP=cat ip.txt NEW_IP=dig +short myip.opendns.com @resolver1.opendns.com if [ $NEW_IP != $OLD_IP ]; then /usr/bin/curl --silent "CUSTOMDOMAIN.nsupdate.info:[email protected]/nic‌​/…" fi echo $NEW_IP > ip.txt

Related