12 July 2019

How to change hostname in Ubuntu 18.04 LTS

If you have tried to change hostname in Ubuntu 18.04, then it probably will be lost after next reboot. So I digged for a almost full working day to sort it out and it turns out, you only need to change it in one file. No, it’s not dhclient.conf nor /hosts nor /etc/hostname, you don’t need to change them at all.

First you need to edit following user-data file, particularly the line which starts with hostname:

nano /var/lib/cloud/seed/nocloud-net/user-data

After that you need to commit those changes using those lines:

cloud-init clean
poweroff

Cloud-init command will clean the cloud configuration so on the next reboot the machine will get the info from the just modified user-data file.

Poweroff is needed for dhcp lease to be released, so at the next boot the machine will obtain a new lease using the new hostname. And when your DNS is integrated with DHCP, then also the correct A and PTR records will be placed in DNS server. Otherwise reboot can also be used.