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

HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
A2 Hosting
$2.99 /mo
Starting price
Visit A2 Hosting
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.8
  • Pricing
    4.0
Hostens
$0.90 /mo
Starting price
Visit Hostens
Rating based on expert review
  • User Friendly
    4.5
  • Support
    3.8
  • Features
    4.3
  • Reliability
    4.5
  • Pricing
    4.8

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Add Swap Space on Your Ubuntu 16.04 Server

Step 1: First of all you need to see if there is some swap space availab
5 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install the MEAN Stack on Your Ubuntu 17.10 Server

The article is step-to-step instruction that shows how to install the MEAN (Mong
3 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Configure Nginx and Apache on the same Ubuntu VPS or Dedicated Server

Nginx and Apache are great and powerful web servers. However, they both have dra
2 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.