How to change the Hostname on CentOS 8

The hostname is also known as a computer, device, or domain name that is given to a computer when it connects with the internet. The hostname has to be unique in the network and is assigned during the operating system installation. It may also assigned dynamically when a virtual machine is created.

In CentOS 8, the hostnamectl and nmcli commands can be used to change the hostname of Desktop and Server systems. By using the hostname command, you can see the hostname of your system.

The domain or hostname is usually present in the system startup file (/etc/hostname). In this article, you will learn how to change the hostname of your system on CentOS 8 without going to restart the computer system.

Pre-requisites

Before moving further must ensure that you are login as an admin account that has Sudo command privileges.

Hostnames understanding

As mentioned above, the hostname is a unique label assigned to a system or device that is connected with a network. It must be noted that two or more don’t have a similar hostname on the same network.

There are three types of hostname:

  1. Static
  2. Pretty
  3. Transient

Static Hostname

It is a traditional hostname type. The static hostname is stored in the /etc/hostname file and can change by the user.

Pretty Hostname

It is a UTF8 hostname and user used for presentation. For example CentOS PC.

Transient Hostname

It is a dynamic hostname that is managed by the kernel. The static hostname and transient hostname are the same by default.

Display the current hostname

To display the current hostname, open terminal window and run the following command:

$hostnamectl

Another command is used to display the current hostname. Type the following command on the terminal.

$hostname

Here the hostname of my CentOS 8.0 system is k8s-master. Display Hostname on CentOS

Three Different commands to change the hostname

There are the following methods to change the name of the hostname on CentOS 8:

The hostnamectl command

The hostnamectl command is used to change the hostname on CentOS 8 Linux system. Run the following command to change the hostname:

$ sudo hostnamectl set-hostname {change-name}

The hostnamectl command will not produce output. For verification that the hostname has been successfully changed, run the hostnamectl command. Here, the hostname set as CentOS-PC. Change hostname using hostnamectl command

Nmtui command

It is a curses-based tool used to communicate with NetworkManager. The nmtui command also used to change or set the hostname.

To launch this tool run the following command on the terminal:

$ sudo nmtui

Once the interface has launched, the arrows keys are used to navigate through the all options, Choose option Set system hostname and hit Enter key:

nmtui command

Enter the hostname that you want to assign. Here, hostname CentOS-PC, move-in the downward direction and select <OK> option.

Set new hostname

A dialogue box would display on-screen “Set hostname to CentOS-PC”. Select ok option.

Confirm new hostname

After successfully changed the hostname of the system. Now, type hostnamectl command to verify the change hostname or restart the systemd-hostnamed service for the hostname changes to take effect:

$sudo systemctl restart systemd-hostnamed

Restart hostnamed

The hostname has been changed successfully.

The nmcli command

It is a command-line tool used for managing the NetworkManager and nmcli is also used to set the hostname of the system.

Type the following command on the terminal to see the current hostname:

$sudo nmcli g hostname

Change hostname using nmcli on-line command

Run the following command to change the hostname to CentOS-PC:

$sudo nmcli g hostname CentOS-PC

To verify the change of the system hostname, restart the systemd-hostnamed service:

$sudo systemctl restart systemd-hostnamed

The hostname has been changed in the system by using the nmcli command.

Note: It is the best way to use a fully-qualified domain name for both transient and static names such as host.abc.com

Conclusion

In this tutorial, we have learned how to change or set the hostname of the system on CentOS 8. Moreover, we have explored different commands hostnamectl, nmtui, and nmcli to change the hostname. I hope this article would be so beneficial for you and now you are able to change the hostname of your system. These commands are recommended for every CentOS user.