The Linux Page

Changing your Linux computer hostname on Ubuntu 18.04+

Changing the name of your computer (especially if it's Hal, that's dangerous!)

Ubuntu 16.04 and older

Changing your computer hostname used to be really easy:

sudo vim /etc/hostname

Change the name there and reboot.

If you reference the hostname in /etc/hosts, you also want to update that there and any other settings (such as tripwire).

The fact is that worked great in older versions. Change the name in that file, reboot and voilà the new name was the current name. The reboot is important because otherwise some of your services may not catch the modification. Yes. You can do:

hostname <new-name>

and it works as expected (i.e. try to log out and back in and you'll see <new-name>).

However, any service that was started before that event will not know of the new name.

Ubuntu 18.04

Somehow, this changed in 18.04 (maybe already in 17.10). Editing the /etc/hostname file and the command line both change the name temporarilly. To make the change permanent you must let the system know that you want the change to be preserved.

This is done by editing the cloud settings:

sudo vim /etc/cloud/cloud.cfg

and changing the preserve_hostname variable to true.

# This will cause the set+update hostname module to not operate
# (if true)
preserve_hostname: true

As you can see, they say that having true may cause problems with other system functions so I suggest that once you changed the hostname you re-edit the cloud file and fix it back to false.

What about Ubuntu 16.10 to 17.10?

You know, I did not test those. I suggest you try the solution for 16.04 first. So edit the /etc/hostname and reboot. If the name doesn't stick after a reboot, try the 18.04 solution. This time it should work.