How to change Hostname in Ubuntu 22.04

It is the name of any computer system that is connected to a network. Hostname allows the computer to be uniquely identified over a network. It can be accessed without using a particular IP address. By default, the hostname of a system is configured during the installation of OS, but you can always change it.

Today, we will explore how to change a hostname in a Linux system. We have made use of Ubuntu 22.04 on which we will run easy to follow Linux commands to update the hostname.

Without any further ado, let’s get started!

Change Hostname

When the hostname is configured in an Ubuntu system, it is saved in a file with the name ‘hostname’ in /etc folder. To change the hostname, we will edit the hostname file and make changes in it. Use nano editor to open the file like this:

sudo nano /etc/hostname

existing hostname in Ubuntu

 The existing hostname is Azifa. Let’s change it to AZIFA. Once the new name has been replaced in the file, we will save the file and exit it.

changed hostname in Ubuntu

For changes to be updated we need to reboot the system.

sudo reboot

Once the system has restarted, open the command line, and run this command to check the hostname:

hostname

As you can see, the hostname has been updated.

updated host name

Change the hostname without a reboot

 What if we don’t want to go through the entire reboot process and just want hostname to be directly updated? That’s doable too.

 To change hostname, run this command:

 sudo hostname new-server-name-here

existing host name without reboot

 Now run the hostname command to see the new hostname we just configured.

hostname

updated host name without reboot

Change Hostname Using hostnamectl

There’s another way you can change the hostname. For that, you will make use of hostnamectl command.

Run this command to see the existing hostname:

hostnamectl

The hostname written against Static hostname is the existing hostname on the system.

hostnamectl existing host name

Let’s change it to a new hostname like this:

hostnamectl set-hostname azifa

Now run the following command again to see the updated hostname:

hostnamectl

The changes have taken place:

hostnamectl updated hostname

Conclusion

 In today’s guide, we saw in detail how to change the hostname in an Ubuntu system using the command line (Terminal). We saw multiple ways through which we can achieve replacing the old hostname with a new one. The process follows running basic Linux commands that are easy to understand.

We hope you liked the tutorial. For more Linux how-tos and tutorials, visit Linux Genie.

Share This:

Leave a Comment

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