This morning I decided to see if I couldn't figure out how to
dynamically assign a hostname to my machine when PPP connects, because
I get a dynamic IP address, and web browsers (among other things)
don't like it when linux's idea of the hostname disagrees with what
the rest of the world believes (as reported by DNS).
After some investigation I determined that the script
/etc/sysconfig/network-scripts/ifup-post
was already attempting to take care of this for me. Yet it wasn't
working. The addition of some debugging code revealed the problem:
ifup-post tries to use ifconfig to determine the IP address associated
with the interface that just came up, but at the time that pppd
invokes ifup-post, ifconfig is not yet correctly reporting that IP
address. So the code in ifup-post fails to set the hostname.
Here is my solution, in the form of an ip-up.local script. (Save this
as /etc/ppp/ip-up.local and make it executable):
|
#!/bin/bash
# This file does the same thing as ip-up, only it handles dynamically
# assigned hostnames correctly by working around a bug wherein ifup-post
# tries to set the hostname at an earlier point, but fails, because ifconfig
# is not yet correctly reporting the IP address associated with the ppp
# interface. Instead, we take advantage of the fact that pppd passes us the
# interface's new IP address.
#
# We also handle the situation where the hostname was set during a previous
# ppp session, overwriting it with the hostname assigned in this session.
#
# If there is a "real" hostname assigned (i.e. not empty, localhost, nor
# assigned by a previous PPP session) then we leave it alone.
LOGDEVICE=$6
REALDEVICE=$1
IPADDR=$4
LASTNAME='~NOT SET~'if [ -r /var/run/ppp-hostname ]; then
LASTNAME=`cat /var/run/ppp-hostname`
fi
/etc/sysconfig/network-scripts/ifup-post ifcfg-${LOGDEVICE}
CURNAME=`hostname`
if [ $CURNAME = '(none)' -o $CURNAME = localhost -o $CURNAME = $LASTNAME -a ${R\
EALDEVICE} != lo ]; then
host=`host ${IPADDR} | grep Name: | awk '{ print $2 }'`
if [ -n "$host" ]; then
logger -p daemon.info -t ip-up.local \
"Setting hostname to $host based on IP $IPADDR"
echo "$host" > /etc/HOSTNAME
echo "$host" > /var/run/ppp-hostname
hostname $host
fi
fi
jim@apocalypse.org |