How to Change Hostname in CentOS?

In CentOS, the “hostname” is a unique name that identifies a specific system on a network. The hostname is utilized to identify the system in various network contexts, such as when connecting to a remote server or when resolving a domain name to an IP address. They are also used in various network protocols and services, such as SSH, FTP, and email, to identify the system connecting or sending data.

This guide will demonstrate various methods to change the hostname in CentOS. 

Method 1: Using the hostnamectl Command

To change the hostname, utilize the “hostnamectl” command with the “set-hostname” option. For instance, the following command will change the current hostname to “new_host” in the following command:

$ hostnamectl set-hostname new_host

The new hostname “new_host” has been changed in the operating system.

Verify Changed Hostname 

User can open a new terminal to verify the changed hostname in CentOS:

The above screenshot confirms the changed hostname “new_host” in the new terminal.

Method 2: Using the hostname command

This method can be utilized for systems running CentOS 6 and earlier. To change the hostname, use the “newhostname” in the following command:

$ hostname newhostname

In the above output, the “newhostname” is the new hostname for our system.

Verify Changed Hostname 

To verify the changed hostname in CentOS, open the “New Tab” from the “File” tab:

The output authenticates the new hostname.

Method 3: Editing the /etc/hostname file

You can also change the hostname by directly editing the “/etc/hostname” file. For this, open the file in a text editor (such as “nano”) and change the hostname: 

$ nano /etc/hostname

Change the hostname according to the requirement. In our case, specify the “peter_host” in the configuration file:

Once you have made the changes, you will need to save the file and restart the system via “reboot” for the changes to take effect.

Method 4: Using nmtui Command (Terminal User Interface)

NMTUI is the network utility that offers a Graphical Interface on the terminal and can change the hostname. For this, invoke the “nmtui” as follows: 

$ nmtui

From the opened interface, choose the option, “Set system hostname”:

Note: You need to use the “tab” key to switch between options of the NMTUI:

Now, change the hostname to “centos_host” and press the “OK” button:

Verify Changed Hostname

After pressing the “OK” button, a pop window confirms that “centos_host” is the new hostname:

Conclusion

CentOS offers the “hostnamectl”, “hostname”, and “nmtui” commands to change the hostname. Additionally, users can modify the hostname from the “/etc/hostname” file that requires the reboot. This article has explained all possible methods to change the hostname in CentOS.