Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a change of IP address in Linux

Tags:

linux

ip

dhcp

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/ ?

like image 940
Joel Holdsworth Avatar asked Apr 29 '10 16:04

Joel Holdsworth


1 Answers

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.

like image 84
kmartimo Avatar answered Oct 12 '22 06:10

kmartimo