
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 the 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 Using /etc Directory
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 to it. Use the nano editor to open the file like this:
sudo nano /etc/hostname
The existing hostname is Ubuntu-3.
Let’s change the file and save it to Ubuntu-6. Once the new name has been replaced in the file, we will save the file and exit it.
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.
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
Now, run the hostname command to see the new hostname we just configured.
hostname
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 the Static hostname is the existing hostname on the system.
Let’s change it to a new hostname like this:
hostnamectl set-hostname Pc-04
Now run the following command again to see the updated hostname:
hostnamectl
The changes have taken place:
Conclusion
In today’s guide, we saw in detail how to change 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.