Setting Ubuntu hostname as offered by DHCP server

A DHCP server might be setup in such a way that it offers a hostname to its clients. By default, the Ubuntu hostname is set during installation and it disregards the one in the DHCP lease, but we can change this behaviour.

Create a file in the /etc/dhcp/dhclient-exit-hooks.d/ directory, for example /etc/dhcp/dhclient-exit-hooks.d/sethostname and give it the following content:

hostname $new_host_name

Now when the DHCP client gets a new lease, it will set the hostname to the one given to it by the DHCP server.

One thought on “Setting Ubuntu hostname as offered by DHCP server

  1. Permalink  ⋅ Reply


    January 16, 2019 at 8:02pm

    On Ubuntu18.04, this dhclient hook is not necessary anymore.

    The required modification is only on the following file:
    It shall contain “localhost”. Nothing more.
    Then, the name given by DHCP will be automatically set.

Leave a Reply

Your email will not be published. Name and Email fields are required.