Updating / Changing the Hostname on Ubuntu 18.04 LTS Server

I recently downloaded Ubuntu 18.04 LTS Server on my Windows 10 test machine running VMware Workstation 14 Pro so I can do some testing. By far the big changes coming will be noticeable on the desktop and less so on the servers.

Ubuntu 18.04 LTS is doing away with Unity.. you probably already know that… and on the server side, Apache2, PHP and other server software are being upgraded.. besides that, not much so far.

If you’re testing Ubuntu 18.04 LTS server, this is how one renames or changes the server hostname. The hostname is a unique name for the server. It’s how the server gets identified on the network.

To change your server name, follow the steps below:

Step 1: Change the Server name in the Hostname File

There are two files that must be edited to successfully change Ubuntu computer name. These files are located in the /etc directory. They are:


First edit /etc/cloud/cloud.cfg and set the parameter “preserve_hostname” from “false” to “true

To do that, run the commands below:

sudo nano /etc/cloud/cloud.cfg

Then change the line:

preserve_hostname: false


preserve_hostname: true

Save the file and exit and continue below…

Next, go and change Ubuntu name in the hostname file… Run the commands below to open the file.. your account must have administrative rights to making changes to this file.

sudo nano /etc/hostname

Then change whatever name in there to be the server new name.

ubuntu hostname

Save the file and exit.

Step 2: Change Server name in the Hosts File

The next file to edit is the hosts file. There you should also replace all the old computer name with the new one.. To open the hosts file, run the commands below…

sudo nano /etc/hosts

The first line is always going to be for localhost with the loopback IP address. The second line is where you change the hostname… If you want to reference the hostname with the server public IP address and not the loopback, you can add a third line with server public IP and hostname.

ubuntu hostname

Save the file and exit.

Step 3: Restart the Server

Finally, restart the server for the new name to apply… If you don’t restart, the new name will not properly be applied. Run the commands below to restart the server.

sudo reboot

When you log back on, type hostname and the new computer name should be displayed.

This is how one changes or renames Ubuntu servers

You may also like the post below:


  1. Your only real feind

    Seriously, do your homework. You and all the other posers who just are trying to direct traffic your way with worthless unrehearsed posts who purport to know what you are doing but just copied information form old post from other people This DOES NOT WORK for 18.04

    The correct way to change your hostname ion 18.04 is to use hostnamectl set-hostname yourhostname. It doesn;t work unless you edit /etc/cloud/cloud.cfg and set the parameter “preserve_hostname” from “false” to “true”

    Seriously, quit wasting peoples time when you don’t know WTF you are talking about.

    down vote

    First edit /etc/cloud/cloud.cfg and set the parameter “preserve_hostname” from “false” to “true” and then edit /etc/hostname.

    1. Thanks, post updated…

    2. Alexander Rykovanov

      sudo hostnamectl set-hostname mysuperhost

    3. No /etc/cloud/cloud.cfg on minimal install ubuntu server 18.04.1 LTS on ESXi here. Editing /etc/hosts directly therefore.

      You may want to take notice of your own post.

  2. Had never seen ‘/etc/cloud/cloud.cfg’ before. Was going crazy changing my hostname just to have it change back after each restart. Thanks!

  3. Both methods work. No need to flame Fiend.

    1. Correction: Alexander’s and Peter’s methods work. Feind’s method works on older versions of Ubuntu, but not on 18.04.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.