Linux self recovering DHCP after really long downtime?
I have a few CentOS 6/7 systems (non-production, experimental purposes) that have been configured to use DHCP for their IP address. Last week there was a big network disruption and I found that those systems had lost their IP address and the DHCP client had terminated. I guess after too many/too long retries.
What is the proper way to make it try to recover forever? Is there a dhcp client setting that can do this? Or should I add a cron entry that does something like 'ifup eth0' every hour? Or is there a much better way to do this?
I know CentOS 6 and CentOS 7 do these things differently and I'm looking for answers for both these cases.
Update:
For now I have created this script (which I put in /etc/cron.hourly/ ) that seems to work in the specific situation of CentOS 6. This is probably not the best solution but it "Works on my machine".
#!/bin/bash
IF=eth0
ifconfig ${IF} | fgrep 'inet addr' > /dev/null
if [ $? -ne 0 ];
then
echo "Network is dead, trying restart"
ifup ${IF}
fi
Still looking for the proper way to do this so I do not think this is the right answer to my question.
Solution 1:
Both CentOS 6 and 7 should accept the PERSISTENT_DHCLIENT option within /etc/sysconfig/network-scripts/ifcfg* files .e.g.
# /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=dhcp
PERSISTENT_DHCLIENT=yes
ONTBOOT=yes
DEVICE=eth0
This instructs the ifup-eth script to run the dhclient without the (default) -1 option
if is_true "${PERSISTENT_DHCLIENT}"; then
ONESHOT="";
else
ONESHOT="-1";
fi;
...
DHCLIENTARGS="${DHCLIENTARGS} ${ONESHOT} -q ${DHCLIENTCONF} -lf ${LEASEFILE} -pf /var/run/dhclient-${DEVICE}.pid"
With that option dhclient will try once. From the man page
-1 Try once to get a lease. One failure, exit with code 2.
Without that option dhclient should retry occasionally (every 5 minutes by default) again from dhclient.conf man page
retry time;
The retry statement determines the time that must pass after the client
has determined that there is no DHCP server present before it tries
again to contact a DHCP server. By default, this is five minutes.