The hostname is set when you are installing the Ubuntu operating system. If it is a virtual machine then it will assigned dynamically when instance created. In this guide, we will show you how to change hostname on Ubuntu 22.04 system.
Before you continue with tutorial, make sure that you are logged in with non-root user with sudo privileges.
Change Hostname on Ubuntu
Using a hostname you can identifies a system on a network. Ensure that each machine have different hostname on a same network.
Get Current Hostname
First of all you need to check current hostname. You can view the current hostname by running the below command:
Static hostname: local.tecnstuff.net Icon name: computer-laptop Chassis: laptop Machine ID: 39bb66809520461594c31b34a4829d7b Boot ID: 5b6b4bc9e7584e4ba62313eeaab9dd61 Operating System: Ubuntu 22.04.0 LTS Kernel: Linux 5.15.0-46-generic Architecture: x86-64
In above output you can see that the current hostname is
Changing the Hostname using hostnamectl
We can change hostname using multiple methods. First one is using
hostnamectl command. Following is the basic syntax to change hostname using
sudo hostnamectl set-hostname NEW_HOST_NAME
Below is the command to change hostname:
sudo hostnamectl set-hostname server.tecnstuff.net
As you can see here, we have changed the hostname from
hostnamectl command. Now the hostname is
server.tecnstuff.net for Ubuntu machine, you can check it again using
Change the Hostname by Edit /etc/hosts file
Second method to change hostname is to modify
/etc/hosts file manually. So open /etc/hosts file using your favorite text editor:
sudo nano /etc/hosts
In that file, find the hostname and change as per you choice at line which start with
127.0.0.1 localhost 127.0.1.1 tecnstuff
Save and close file. You should restart your system to get changes in effect.
After changing hostname using second method, you can again check hostname using
hostnamectl command and now it will show output with new hostname as below:
Static hostname: server.tecnstuff.net Icon name: computer-laptop Chassis: laptop Machine ID: 39bb66809520461594c31b34a4829d7b Boot ID: 5b6b4bc9e7584e4ba62313eeaab9dd61 Operating System: Ubuntu 22.04.0 LTS Kernel: Linux 5.15.0-46-generic Architecture: x86-64
That’s all. As you can see above the hostname is successfully changed from local.tecnstuff to server.tecnstuff.net .
Finally, you have learned how to change the hostname on Ubuntu 22.04 system using multiple methods.
If you are getting any issue to change hostname or have any suggestions you can leave a comment below.