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.
Setting Ubuntu hostname as offered by DHCP server
On Ubuntu18.04, this dhclient hook is not necessary anymore.
The required modification is only on the following file:
/etc/hostname
It shall contain “localhost”. Nothing more.
Then, the name given by DHCP will be automatically set.