How to get the hostname from a DHCP server
Solution 1
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.
Solution 2
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...).
Solution 3
You can get your hostname from your DHCP server - it is part of the DHCP specification.
https://www.rfc-editor.org/rfc/rfc1533#section-3.14
"This option specifies the name of the client"
Solution 4
Note that when using Ubuntu 18.04 the tie-in scripts are no longer necessary. If the hostname of the install is set to localhost
in /etc/hostname
the DHCP client will set the hostname automatically at startup using the name issued by DHCP, if present. When running hostnamectl
it will list localhost
as the permanent hostname, and whatever DHCP issues as a transient hostname.
testaccount@dhcp-hostname:~$ hostnamectl
Static hostname: localhost
Transient hostname: dhcp-hostname
Solution 5
d_inevitable's answer almost solved my problem, but not completely. The problem was that although:
-
The DHCP server was sending a hostname (by adding the
option host name 'client1'
in the dhcpd.conf) and I actually verified it by capturing and analyzing the contents of the DHCP offer with wireshark
-
The DHCP client was expecting the hostname from DHCP server (by adding
request host-name
in the dhclient.conf)
The client was not getting a new hostname (easily verified by typing
hostname
in terminal and getting the old hostname, or no hostname if I had deleted the contents/file). As a result, the proposed solution by d_inevitable was only copying an empty string.
To solve that, I applied a crud solution, that generally should not be followed unless you are desperate to make it work, like I was.
First, open with edit capability the DHCP client control script:
sudo vi /sbin/dhclient-script
There, you will have to locate the function
set_hostname()
Just use the search and it should come right up. Now, at least on my computer, this function has three if-then-else conditions, encapsulated to each other:
# set host name set_hostname() { local current_hostname
if [ -n "$new_host_name" ]; then current_hostname=$(hostname) # current host name is empty, '(none)' or 'localhost' or differs from new one from DHCP if [ -z "$current_hostname" ] || [ "$current_hostname" = '(none)' ] || [ "$current_hostname" = 'localhost' ] || [ "$current_hostname" = "$old_host_name" ]; then if [ "$new_host_name" != "$old_host_name" ]; then hostname "$new_host_name" fi fi fi }
Now, what you need is to force the assignment of the new hostname to your host, no matter what. Therefore you want to comment out the two encapsulated if-then-else. The result should look something like:
# set host name set_hostname() { local current_hostname
if [ -n "$new_host_name" ]; then current_hostname=$(hostname) # current host name is empty, '(none)' or 'localhost' or differs from new one from DHCP #if [ -z "$current_hostname" ] || # [ "$current_hostname" = '(none)' ] || # [ "$current_hostname" = 'localhost' ] || # [ "$current_hostname" = "$old_host_name" ]; then # if [ "$new_host_name" != "$old_host_name" ]; then hostname "$new_host_name" # fi #fi fi }
Now the d_inevitable's or this should work as expected. Hope that helps if you are in a similar desperate frustration as I was.
Related videos on Youtube
jackzong
Updated on September 18, 2022Comments
-
jackzong over 1 year
I want Ubuntu to get the hostname and DNS name from a DHCP client. The default installation of Ubuntu 11.10 (Oneiric Ocelot) does not do that.
The same question was asked and is unsolved on Ubuntu Forums.
-
jackzong about 12 yearsI have dns and dhcp (windows 2008) server on my network. Ant it assigns hostname and dns name to each ip, but ubuntu does not update it's hostname and dns name. I should be able to see this assigned hostname by typing
hostname
and domain by typinghostname -d
. so it's fqdn should be hostname.domain -
oddfellow about 12 yearsAFAIR if the client is not joined to the Windows domain, the Windows DHCP server will not dynamically update the Windows DNS entries. I'm not really sure though... depends on the Windows DHCP/DNS configuration
-
Olathe over 10 yearsHow do you get the server to send it?
-
Móż over 10 yearsBy default 12.04 has this line "send host-name = gethostname();" and you can see that value by typing "hostname" on the command line. By default, this will not have any effect on a windows network. As per the original question.
-
Ale about 9 yearsthis script actually uses a DNS query (using the
host
command) to extract the hostname associated with the assigned IP address. DHCP servers can actually explicitly set a host name option (option 12), which can potentially be different from the hostname you obtain from a DNS query on the IP (this could potentially also fail, if no PTR record for the IP exists on the DNS) -
Dzintars about 3 years> This option is only honored by dhclient-script(8) if the hostname for the client machine is not set. kb.isc.org/docs/isc-dhcp-44-manual-pages-dhcp-options