I have a Debian server at home. The server is my router and provides VPN access to the outside.
I do not have a static IP address; the lease time given by the ISP is two hours.
This cable cell which services the area I live, seems to have at least two different different netblocks for customers, and it is not entirely unusual obtaining a different IP address after a reboot of the Linux server, or less usually, but more importantly in what concerns this question, after some ISP maintenance operations.
I have some services that are dependent on the IP address; and the public IP adddress is used either for external (VPN) access, and for internal reference.
In some services, I use the dynamic DNS name from FreeDNS in order not to have to change an IP address in several locations.
As such, the best method I devised until now is running a script on dhclient-exit hooks. The script is called after DHCP gives/renews an IP, and restart services if the IP is changed.
I also change the IP of my dynamic DNS name on /etc/hosts
, for solving possible problems of using the old IP before the change at FreeDNS side trickles down to me.
The script I wrote for dhclient-exit-hooks.d
is this one; exit_status
should be 0 if all went ok with dhclient
.
#!/bin/bash
PATH=$PATH:/usr/bin
if ! [[ -v exit_status ]]
then
exit 1
fi
if [ $exit_status -eq 0 ]
then
IP=`ip addr show eth0.101 | grep inet | awk ' { print $2 } ' | cut -f1 -d "/"`
OLDIP=`awk ' /xxxx.mooo.com/ { print $1 } ' /etc/hosts`
if [ $reason = "REBOOT" ] || [ $reason = "BOUND" ] || [ $IP != $OLDIP ]
then
sed -i "s/^[0-9\.]* xxxx.mooo.com/$IP xxxx.mooo.com/g" /etc/hosts
timeout 60 /opt/bin/iptables.sh
timeout 60 /etc/init.d/ipsec restart
timeout 60 /etc/init.d/asterisk restart
timeout 120 /etc/init.d/bind9 restart
timeout 60 /usr/bin/wget -O - http://freedns.afraid.org/dynamic/update.php?XXXXXXXXXXXX > /dev/null
fi
fi
I am aware of other posts that also recommend using dhclient-exit-hooks.d
; however my question is a way to automate the restarting and configuration of those services upon an IP address change.