8

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.

Niels Basjes
  • 2,176
  • 3
  • 18
  • 26
  • 1
    I have not tested this, but I believe your answer would be somewhere in the man page of `dhclient.conf` as you can define default lease options that can override what the client is told by the dhcpd. Specifically I would look at the `interface` stanza options around lease time and change timeouts. – Aaron Mar 26 '18 at 19:27
  • As an FYI, the renewal phase (T1) and rebinding phase (T2) are a function of the lease time. For a DHCP client in the rebinding phase (T2) the client will release it's ip address and return to the INIT (no ip address) state after the T2 phase expires. – joeqwerty Aug 03 '18 at 01:51

1 Answers1

5

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.
Brian Keffer
  • 51
  • 1
  • 2