Saturday, October 31, 2020

How to Change Hostname on Ubuntu 20.04 Linux

Must read

How to Setup a Firewall with UFW on Ubuntu 20.04

Nowadays, a Firewall is an essential utility and property of any system for security; by default Ubuntu Operating system having a firewall...

How to Install Ruby on Ubuntu 20.04 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install Eclipse IDE on Ubuntu 20.04 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a...
SATISH KUMAR
I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.
- Advertisement -

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

- Advertisement -

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

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

Change the Hostname

- Advertisement -

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
- Advertisement -

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

- Advertisement -
- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Latest article

How to Setup a Firewall with UFW on Ubuntu 20.04

Nowadays, a Firewall is an essential utility and property of any system for security; by default Ubuntu Operating system having a firewall...

How to Install Ruby on Ubuntu 20.04 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install Eclipse IDE on Ubuntu 20.04 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a...

How to Reset the MySQL Root Password

Do you want to reset the MySQL root password? Sometimes we forget the root password and not able to log in on...