Home » Tutorial » How to Change Hostname on Ubuntu 18.04 Linux

How to Change Hostname on Ubuntu 18.04 Linux

Update on:
Aug 27, 2019

The hostname is known as the system’s identifier, which is set at the time when we are installing the Ubuntu operating system.

This tutorial will explain setting the hostname for your system or virtual machine after Ubuntu 18.04 installation.

Prerequisites

Before going through with this tutorial, make sure your system has Ubuntu 18.04 Linux installed and have user access with sudo privileges to execute administrative commands.

Display the Current Hostname

To check the current hostname on the Ubuntu system, use the hostnamectl command as shown below:

$ hostnamectl
 Static hostname : satishsystem
       Icon name : satish-vm
         Chassis : vm
      Machine ID : 6f17445f53074505a008c9abd8ed64a5
         Boot ID : 1c769ab73b924a188c5caeaf8c72e0f4
  Virtualization : kvm
Operating System : Ubuntu 18.04 LTS
          Kernel : Linux 4.15.0-22-generic
    Architecture : x86-64

You can see in the above output; the current hostname is set to the satishsystem.

Change the Hostname

To set the new hostname for your Ubuntu system, use the following steps.

Step 1 – Change hostname using hostnamectl command.

You can change the hostname in Ubuntu 18.04 using the hostnamectl command.

For example, we can change the system static hostname to linuxconcept by using the following command:

$ sudo hostnamectl set-hostname linuxconcept

The command hostnamectl does not return any output on success. It returns 0 on success or failure code on failure results.

Step 2 – Edit the /etc/hosts configuration file

Open the /etc/hosts file in your preferred editor and change the old hostname with a new desire hostname.

127.0.0.1   localhost
127.0.0.1   linuxconcept
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Step 3 – Edit the cloud.cfg configuration file

If you are using the cloud-init package in your Ubuntu machine, then you need to edit the cloud.cfg file also. Using your Ubuntu box on a cloud like AWS, this package will be preinstalled on those instances used to handle the instance’s initialization.

You can check the package is installed in your Ubuntu machine or not use the ls command:

$ ls -l /etc/cloud/cloud.cfg

If the package is not installed, you will get output on the screen like below:

output:
ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory

But, if the package is installed, you will get output like this:

output:
-rw-r--r-- 1 root root 3169 Apr 27 09:30 /etc/cloud/cloud.cfg

In the case of the package is installed, to change the hostname, open the file with your text editor:

$ sudo vim /etc/cloud/cloud.cfg

Now, search for string preserve_hostname and change the value of it from false to true, as shown below:

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

And close the file after saving it.

Verify new hostname after change

You can verify the hostname has changed in your Ubuntu system by checking hostname using the hostnamectl command, as shown below:

$ hostnamectl

You will get your new hostname on the output screen.

 Static hostname : linuxconcept
       Icon name : satish-vm
         Chassis : vm
      Machine ID : 6f17445f53074505a008c9abd8ed64a5
         Boot ID : 1c769ab73b924a188c5caeaf8c72e0f4
  Virtualization : kvm
Operating System : Ubuntu 18.04 LTS
          Kernel : Linux 4.15.0-22-generic
    Architecture : x86-64

Conclusion

Now, you have learned to change the hostname for your installed Ubuntu 18.04 Linux server or system without restarting the system.

If you get any problem with changing the hostname of your system, please leave a comment below.

Related Posts

How to Install CouchDB on Ubuntu 21.04 Linux Operating System

How to Install CouchDB on Ubuntu 21.04 Linux Operating System

The CouchDB is an open-source database system, managed by the Apache Software Foundation. It is fault-tolerant, and schema-free NoSQL database management system.   CouchDB store data in document or files with JSON data structure. Each document contains fields and...

How to add swap space on Ubuntu 21.04 Operating System

How to add swap space on Ubuntu 21.04 Operating System

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM it use swap space to move inactive pages from RAM. Swap space can be created into Linux system in two ways, one we can create a...

How to Install Nginx on Ubuntu 21.04 Server

How to Install Nginx on Ubuntu 21.04 Server

Nginx is the most potent, open-source, and a high-performance Web server. It can work as a reverse proxy server also, nowadays, is used by most of the most significant websites on the internet. People pronounced “engine x” for Nginx; it is the hot choice for every...

How to Install VirtualBox on Ubuntu 21.04 Linux

How to Install VirtualBox on Ubuntu 21.04 Linux

VirtualBox is an Open Source tool, known as a cross-platform virtualization application or software. It used to run multiple operating systems or virtual machines simultaneously on a single hardware. In this VirtualBox installation tutorial, we will explain the...

Follow Us

Our Communities

More on Ubuntu

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

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

thirteen + 17 =

Shares