How to Change Hostname on CentOS 8

Table of Contents
Change Hostname on CentOS 8
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 CentOS 8 without restarting the machine.
Prerequisites
Before you start to change the hostname on CentOS 8. 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: CentOS Linux 7 (Core) 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 the 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: CentOS Linux 7 (Core) 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 CentOS 8. If you have any queries regarding this then please don’t forget to comment below.
LATEST POSTS
-
How to List Installed Packages on Debian
-
How to install Pip on Ubuntu 18.04
-
How to Install PhpStorm on CentOS 8
-
How to Add User to Group in Linux
-
How to Install Go on Debian 10
-
How to Search and Replace Text in Vim or Vi
-
How to Install Apache on Ubuntu 18.04
-
How to Install DataGrip on Linux Mint 19
-
How to Install Apache Tomcat 9 on CentOS 7
-
How to Create Symbolic Links Using ln Command in Linux
-
How to Install PhpStorm on Debian 10
-
How to Install PHP 7.3 on Fedora 28/29/30