Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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:

# 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" ]

if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
   && [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]

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.

share|improve this answer
this 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) – Ale Feb 12 '15 at 10:21

You can get your hostname from your DHCP server - it is part of the DHCP specification.

"This option specifies the name of the client"

share|improve this answer
How do you get the server to send it? – Olathe Oct 24 '13 at 12:18

You don't get your hostname from the DHCP server.

You can send your hostname to the server, which may change the IP you're assigned. You can change what name is sent either by editing your Network Manager connection (the field is called DHCP Client ID) or you can edit (as root) /etc/dhcp/dhclient.conf. Look for the line that says:

send host-name "<hostname>";

... and change <hostname> to whatever you like.

By default Ubuntu will get its DNS settings from the router (if it sends them) but I suspect you're talking about local DNS/mDNS where you can access other computers by their hostname. This is called Ahavi or Zeroconf in Ubuntu and it's installed by default.

You should be able to access your computer by <hostname>.local

share|improve this answer
I 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 typing hostname -d. so it's fqdn should be hostname.domain – Oguz Bilgic Feb 17 '12 at 1:31
AFAIR 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 – oddfellow Feb 17 '12 at 20:54
By 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. – Móż Nov 22 '13 at 3:09
Although zeroconf is installed by default, it's horribly unreliable. I'm frankly surprised when it works, as pinging hostname or hostname.local rarely results in success even though nothing's changed on my network in months. – Cerin Apr 18 '15 at 19:14
This is not quite true. While you can send your host name to DHCP server with send host-name ""; in order to obtain a specific IP address, you can just as well request one with request host-name;. – Dmitry Grigoryev Nov 17 '15 at 10:33

d_inevitable's answer almost solved my problem, but not completely. The problem was that although:

  1. 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

  2. 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


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


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 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 }

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 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 }

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.

share|improve this answer

If found that can be a dhcpclient scripts bug.

Try to clean $old_host_name on ip renew

echo unset old_host_name > /etc/dhcp/dhclient-enter-hooks.d/unset_old_hostname

Also static /etc/hostname seems to has prority over dhcp answer so leave it empty

> /etc/hostname

Tested on ubuntu 14.04 and dnsmasq server.

share|improve this answer

The answer depends on whether or not you are using static leases on your DHCP server. If you are, it is unnecessary to get the hostname from DNS. You can change this line in d_inevitable's solution

hostname=$(host $new_ip_address | cut -d ' ' -f 5)



But this should happen automatically if your hostname is originally set to localhost.localdomain, so you don't have to write a script. However, if you want to set the hostname to the FQDN, you'll need to change d_inevitable's script to


Again, all this only works if you're using static leases.

share|improve this answer

Don't have enough reputation to comment, but I'd like to piggy-back on the previous answer as it almost solved the problem for me using a dhclient hook.

I've found that using the standard ISC DHCP Server for some reason, the aforementioned hook outputs a host name with a '.' period character at the end of the hostname for some reason.

So, in the previous answer you might need "cut out" the extraneous period with a sed:

hostname=$(host $new_ip_address | cut -d ' ' -f 5)

Would become:

hostname=$(host $new_ip_address | cut -d ' ' -f 5 | sed -e "s/\.$//g")
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.