[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

DHCP and problems with apache, samba etc



When using dhcp, since the hostname doesn't resolve to an IP address right
away, there are a few packages that barf, like apache and samba.

These appear to attempt to find the IP address from the system name.  In
the case of dhcp, this may not be known.  You can't hardcode an address in
the /etc/hosts file, since dhcp may change it.  You can't put it in the
DNS, same reason.

For samba with Micro$oft, it's a catch-22.  You can set the MS dns to use
WINS for a last-ditch name resolution, which would work if samba had
registered with WINS as configured in the smb.conf.  But if won't config
until it knows its own IP addr.  Deadlock.

So, I have an extremely small script in /usr/local/bin/dhcp-update-hosts

if [ -f /etc/dhcpc/config ] ; then
        source /etc/dhcpc/config
fi
if [ -f /etc/dhcpc/hostinfo-$IFACE ] ; then
        source /etc/dhcpc/hostinfo-$IFACE
fi
/bin/cp /etc/hosts.src /etc/hosts
echo "$IPADDR       `cat /etc/hostname`" >> /etc/hosts

This is kicked of by /etc/cron.d/dhcp-update-hosts every 10 minutes.

*/10 * * * * root /usr/local/bin/dhcp-update-hosts

Very simple, it just checks the /etc/dhcpcd/config file for the
interface being used, then uses that to pull the IP address from the
/etc/dhcpc/host-info file.  This is then added to a pristine copy of the
/etc/hosts file. 

I've also modified the /etc/init.d/dhcpc startup file to include the -c
option pointing to the /usr/local/bin/dhcp-update-hosts file, so it gets
run as soon as an address is obtained.

The end result is that as soon as an address is had, it's in the
/etc/hosts file, available for name resolution.  Whenever the lease
expires and is *not* renewed (replaced with another) there's a maximum
window of 10 minutes where /etc/hosts is wrong.  That can be easily
shortened by modifying the /etc/cron.d/dhcp-update-hosts file.

Has anyone else had similar problems ?  How did you solve them ?

--
Dean Carpenter		deano@areyes.com			94TT :)
Areyes, Inc.		Dean.Carpenter@pharma.com

"No matter where you go, there you are"  sayeth Buckaroo 								 across the Eighth Dimension


Reply to: