How To Change the Hostname on an Ubuntu 18.04 VPS or Dedicated Server

Ubuntu 18.04 has been described as awesome by Mark Shuttleworth – the founder of Canonical. The latter is the company behind Ubuntu. On April 26th 2018, we saw the final release of Ubuntu 18.04 and that means that everyone can start using it even on production servers. Ubuntu 18.04 has lots of improvements including faster boot times and bug fixes.

Just like with a brand new computer, setting an Ubuntu 18.04 hostname is a pivotal process. This might sound tricky when it comes to provisioning your VPS server for the first time. Some people might not care about the Linux’s hostname during the initial process. However, this can lead to a lot of confusion if an administrator is logging to different VPS instances.

A hostname should give quick relevant information about the system where one is logged in. This doesn’t come out of the box and most data centres and hosting company will really set some weird hostnames on new Ubuntu 18.04 instances.

A hostname is simply a label that is assigned to a device on a network to distinguish it from the rest of the devices. Or you might refer to the hostname as another name for “Computer Name“. Hostnames play an important role when it comes to resolving common easy to remember names to IP addresses through the DNS service.

You may use any hostname for your Ubuntu 18.04 server but most people name it after planets or even animals and others will pre-fix the location of the server on the name .e.g New_York_Pluto, Miami_proxima_b.

In this guide, I will show you how to change the name of your Ubuntu 18.04 server.

Prerequisites

  • An VPS server running Ubuntu 18.04
  • A non-root user with sudo privileges

Special Note: Looking for a Linux hosting service? Find the best Linux hosting services including expert and user reviews on HostAdvice.

Step 1: Checking the default hostname

First, login to your terminal window and type the command below to get your current hostname.

hostname
or
hostnamectl

The command hostname will display your Ubuntu 18.04’s current hostname. If you use the second alternative hostnamectl, you will see a lot of information about your server as shown in the image below

Step 2: Edit the cloud-config script

On a terminal window run the command below to edit the configuration package file

 sudo nano /etc/cloud/cloud.cfg

Then find the line preserve hostname and change its value from false to true.

The configuration package above runs tasks earlier in the boot process. It is responsible for .setting the default locale and hostname of the server among other tasks.

Step 3: Changing the hostname

In Ubuntu 18.04, you will be required to edit two files to change your hostname. To edit the first file, type the command below on a terminal window:

sudo nano /etc/hostname

Editing the /etc/hostname

As seen above, the hostname will be displayed at the top of the file. You need to overwrite that value with your desired hostname. Then, press CTR+X and Y to save the changes.

with your desired hostname. Then, press CTR+X and Y to save the changes.

Changing Ubuntu 18.04 server name on the hosts file

You will also need to edit the /etc/hosts file. To do this, type the command below on a terminal window:

sudo nano /etc/hosts

As seen above. The first line on the hosts file displays the localhost on the IP address 127.0.0.1. Leave that line intact. Then you will need to edit/add the hostname on the second line after the loop back IP address 127.0.1.1.  Also, you might find it necessary to reference your VPS hostname under your Ubuntu 18.04 server’s public IP address by adding a third line as show below.

127.0.0.1 localhost

127.0.1.1 variety_village

x.x.x.x variety_village

Step 4: Reboot the system

In order for the changes to take place, you need to restart your Ubuntu 18.04 server. Type the command below

sudo reboot

Conclusion

That’s it; you have changed your Ubuntu 18.04 hostname. Remember you can always check if your hostname was changed successfully by typing the command ‘hostname’ one more time on the terminal window. If everything went as expected, you will be able to login on your Ubuntu VPS instance without any confusion.

 

Check out the top 3 Linux hosting services

Was this article helpful?