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.

This method still works as is in the Desktop version of Ubuntu.

Ubuntu 18.04 Server

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 creating a new cloud file like so:

sudo vim /etc/cloud/cloud.cfg.d/50-preserve-hostname.cfg

then adding the preserve_hostname variable and setting it 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 far, though, I have not experienced any issues because of this.

Otherwise, you can simply comment out that variable or delete that new file after your next reboot. If anything, I'm pretty sure I've seen my hostname go back to some default if that parameter is reversed to false. So I do not recommand it.

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.