How to Change Hostname on CentOS 7

How to Change Hostname on CentOS 7
How to Change Hostname on CentOS 7

Change Hostname on CentOS 7

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 7 without restarting the machine.

Prerequisites

Before you start to change the hostname on CentOS 7. 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 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 7. If you have any queries regarding this then please don’t forget to comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here