Categories

ID #1150

How do I make hostname -f return the FQDN for a dynamic IP address (on the non-bootable network interface)?

How do I make hostname -f return the FQDN for a dynamic IP address (on the non-bootable network interface)?


This comes in useful for some SGE  configurations.

The following code should be added to /etc/dhcp/dhclient-enter-hooks (the exact path of the hook script may be different):
fix_hosts() {
  INTERFACE_NAME=eth1
  DOMAIN=example.com
  HOSTNAME=´hostname´
  if [[ $interface == $INTERFACE_NAME ]]; then
    grep $HOSTNAME.$DOMAIN /etc/hosts
    if [ $? -eq 1 ]; then
      echo $new_ip_address $HOSTNAME.$DOMAIN $HOSTNAME >> /etc/hosts
    else
      sed -i s/.*$HOSTNAME.$DOMAIN.*/$new_ip_address\ $HOSTNAME.$DOMAIN\ $HOSTNAME/g /etc/hosts
    fi
  fi
}
fix_hosts

Now, each time the dynamic IP is bound to $INTERFACE_NAME, the line in /etc/hosts that has this form:
<IP> <HOSTNAME>.<DOMAIN> <HOSTNAME>

will be added if it doesn't exist, or replaced if it does exist.

The result of this is that hostname -f will now return the FQDN.

Tags: /etc/hosts dhclient

Related entries:

You cannot comment on this entry