How to get the hostname from a DHCP server
There is a way to do it with a little script for a dhcp hook as described here.
Create a new file:
sudoedit /etc/dhcp/dhclient-exit-hooks.d/hostname
and paste the following code:
#!/bin/sh
# Filename: /etc/dhcp/dhclient-exit-hooks.d/hostname
# Purpose: Used by dhclient-script to set the hostname of the system
# to match the DNS information for the host as provided by
# DHCP.
#
# Do not update hostname for virtual machine IP assignments
if [ "$interface" != "eth0" ] && [ "$interface" != "wlan0" ]
then
return
fi
if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
then
return
fi
echo dhclient-exit-hooks.d/hostname: Dynamic IP address = $new_ip_address
hostname=$(host $new_ip_address | cut -d ' ' -f 5 | sed -r 's/((.*)[^\.])\.?/\1/g' )
echo $hostname > /etc/hostname
hostname $hostname
echo dhclient-exit-hooks.d/hostname: Dynamic Hostname = $hostname
Replace eth0
and wlan0
with the names of the interfaces from which you want to obtain the hostname. In most cases eth0
and wlan0
should stay the same.
Make sure it is readable...
chmod a+r /etc/dhcp/dhclient-exit-hooks.d/hostname
That's all. On the next dhcp response your hostname will update automatically.
You can get your hostname from your DHCP server - it is part of the DHCP specification.
https://tools.ietf.org/html/rfc1533#section-3.14
"This option specifies the name of the client"
Oli's answer is demonstrably false ("You don't get your hostname from the DHCP server"), as evidenced by the other answers here, and also by my recent experience on a RHEL7 system. Said system got its host name from the DHCP server.
And, indeed, there are things in the DHCP configuration files that are supposed to make it happen. For example:
host host4 { # verified
hardware ethernet 41:88:22:11:33:22;
fixed-address 192.168.0.4;
option host-name "host4";
}
Is supposed to tell that host that his name is host4.
As it turns out, isc's dhclient DOES NOT APPEAR TO SUPPORT THIS!
However, dhcpcd5 does, out of the box. Stop dhclient, install dhcpcd5, run dhcpcd, renew your lease, and poof, your hostname on your DHCP client is set to the name sent from the DHCP server. No dhclient-exit-hooks.d
scripting, no hacks in rc.local
, nothing.
As an end-note, I've spent a lot of time trying to get this to work using ISC's dhclient. Absolutely no joy, even when the server sends the host name.
My initial solution to the problem was writing some cute code in rc.local
to detect when the network came up and forcing a (in my case) search of /etc/hosts
to get the hostname and then running hostname
with that host name. It works, but until the network comes up your hostname is probably wrong (when first deploying a host, I remove /etc/hostname
, so the host name is localhost
until I can run /etc/init.d/hostname.sh start
once the network comes up - so when first getting a new name you need to boot twice - once to get your hostname, and once to have that name available when everything starts up...).