Updating / Changing The Hostname On Ubuntu 18.04 LTS Server

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:

/etc/hostname
/etc/hosts

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

to

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.

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.

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.