How to Change Hostname on Ubuntu 18.04

Table of Contents
Change Hostname on Ubuntu 18.04
The hostname will be set when you are installing the operating system and for virtual machines, it is dynamically assigned when instance created. By using hostname a network differentiates between two systems so it is important to have a different hostname for each machine in the same network. In this tutorial, you are going to learn how to change the hostname on Ubuntu 18.04 without restarting the machine.
Prerequisites
Before you start to change the hostname on Ubuntu 18.04. You must have the non-root user account on your system with sudo privileges.
Check the hostname
Run below command to check the hostname for your system:
hostnamectl
The output should be like:
Static hostname: linux4one Icon name: computer-laptop Chassis: laptop Machine ID: 2381ec6bc94945e781c843f399507ea2 Boot ID: 2902f9a594944fe28d8ac90759229ef0 Operating System: Ubuntu 18.04.1 LTS Kernel: Linux 4.15.0-43-generic Architecture: x86-64
As shown in the above output the hostname for the machine is linux4one
.
Change hostname using hostnamectl
hostnamectl
command is also used to change the hostname of the system in an easy way.
Following is the basic syntax for changing the hostname of the system using hostnamectl
command:
sudo hostnamectl set-hostname NEW_NAME
Where NEW_NAME is the new hostname of the system.
Now to change hostname of the system from linux4one
to new_hostname run following command:
sudo hostnamectl set-hostname new_hostname
Now the hostname of the system is changed from linux4one
to new_hostname
.
Edit hostname in /etc/hosts file
Here you can also change the hostname of the system editing /etc/hosts
file.
Now open /etc/hosts
file with your favorite text editor:
sudo nano /etc/hosts
Now change the hostname in the opened file with the new one:

Verify the hostname
As of now, we have changed the hostname of the system using one of the above methods. Now to verify the change run below command:
hostnamectl
The output should look like
Static hostname: new_hostname Icon name: computer-laptop Chassis: laptop Machine ID: 2381ec6bc94945e781c843f399507ea2 Boot ID: 2902f9a594944fe28d8ac90759229ef0 Operating System: Ubuntu 18.04.1 LTS Kernel: Linux 4.15.0-43-generic Architecture: x86-64
As you can see the hostname is successfully changed from linux4one
to new_hostname
.
Conclusion
You have successfully learned how to change the hostname on Ubuntu 18.04. If you have any queries regarding this then please don’t forget to comment below.
LATEST POSTS
-
How to Install Node.js with npm on Debian 10
-
How to Install Redis on Debian 9
-
How to install phpMyAdmin on Debian 9
-
How to Install Microsoft PowerShell 6.2.0 on Ubuntu 18.04
-
How to Install XAMPP on Linux Mint 19
-
How to Install Go on Debian 9
-
How to List Installed Packages on Linux Mint 18
-
How to Safely Remove Files and Directories Using Linux Command Line
-
How to Install RubyMine on CentOS 7
-
How to Install Django Web Framework on Debian 10
-
How to Setup Cron Jobs on Fedora
-
Ping Command in Linux with Examples