Categories: Ubuntu

How to Change Hostname on Ubuntu 20.04 Linux

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 20.04 installation.

Prerequisites

Before going through with this tutorial, make sure your system has Ubuntu 20.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 20.04 LTS
          Kernel : Linux 4.15.0-22-generic
    Architecture : x86-64
Sponsored

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 20.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

Sponsored

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 20.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 20.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.

The post How to Change Hostname on Ubuntu 20.04 Linux appeared first on Linux Concept.

Ubuntu Server Admin

Recent Posts

How to Fix VMware’s “Could not open /dev/vmmon” Error on Ubuntu

You’ve recently installed VMware Workstation on your Ubuntu system and encountered the frustrating “Could not…

7 hours ago

How to Fix Ubuntu 404 Errors While Fetching Dependencies

Have you ever found yourself staring at a terminal full of 404 errors while trying…

7 hours ago

How to Fix ‘Please Install All Available Updates’ Error When Upgrading Ubuntu 18.04 to 20.04 LTS

One particularly frustrating error that many users face when trying to upgrade from Ubuntu 18.04 …

7 hours ago

How to fix “Release is not valid yet” Error in Docker Containers

In the world of containerization, time synchronization issues can create unexpected roadblocks when working with…

7 hours ago

How to fix “Externally Managed Environment” Pip Errors on Ubuntu

If you’ve recently upgraded to Ubuntu 23.04 or newer, you might have encountered a frustrating…

7 hours ago

Ubuntu now officially supports NVIDIA Jetson: powering the future of AI at the edge

Canonical announces the General Availability of Ubuntu for the NVIDIA® Jetson Orin™ for edge AI…

14 hours ago