Change hostname in Ubuntu 18


ubuntu-logo-sm In Ubuntu 18.04, when you update the /etc/hostname, the name doesn't stay after the reboot. Why that doesn't work anymore is a story for another day. What we are interested in now is changing the hostname to something that survives the reboot. Note that as any other admin command, you either have to run as sudo or root.

To check the current hostname, run the following command:


It will display, among other things, the current hostname:

Static hostname: terra

To change it, run the following command (where terra2 is the new name):

hostnamectl set-hostname terra2

The command above actually just goes and modifies the /etc/hostname file, but that's not enough to make it survive the reboot. You have to go in and modify a special file, asking it to preserve the settings.

In the file /etc/cloud/cloud.cfg locate preserve_hostname and set it to true from false:


preserve_hostname: true


After that, reboot the system and your hostname should stick.