How to Change Hostname on CentOS 8.

How To Change Hostname On CentOS 8.

This guide explains how to set or change the hostname on CentOS 8 without needing to restart the system.

There are a number of reasons why you may need to change the hostname. The most common is when the hostname is automatically set upon the instance creation.

 

Understanding Host Names

A hostname is a label that identifies a device on a network. You shouldn’t have two or more machines with the same hostname, on the same network.

There are three classes of hostname: static, pretty, and transient.

  • static – The traditional hostname. It is stored in the /etc/hostname file and can be set by the user.

  • pretty – A free-form UTF8 hostname used for presentation to the user. For example, nexonhost desktop.

  • transient – A dynamic hostname that is maintained by the kernel. DHCP or mDNS servers can change the transient hostname at run time. By default, it is the same as the static hostname.

It is recommended to use a fully-qualified domain name (FQDN ) for both static and transient names such as host.example.com.

 

Displaying the Current Hostname

To view the current hostname, execute the following command:

hostnamectl

In this example, the current hostname is set to centos8.localdomain.

 

Changing the Hostname

 

Method 1: Using the hostnamectl command

In CentOS 8 and all other Linux distributions that are using systemd, you can change the system hostname and related settings with the hostnamectl command. The syntax is as follows:

sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient

For example, to change the system static hostname to host.nexonhost.com, you would use the following command:

sudo hostnamectl set-hostname host.nexonhost.com

To set the pretty hostname to nexonhost’s desktop, enter:

sudo hostnamectl set-hostname "nexonhost's desktop" --pretty

The hostnamectl command does not produce output. On success, 0 is returned, a non-zero failure code otherwise.

To verify that the hostname was successfully changed, use the hostnamectl command.

 

Method 2: Using the nmtui command

nmtui is a curses-based tool for interacting with NetworkManager. It can also be used to set or change the hostname.

Launch the tool by typing its name in the terminal:

sudo nmtui

Use the arrow keys to navigate through the options, select Set system hostname and press Enter.

Type the new hostname:

Press Enter to confirm the new hostname:

Finally, restart the systemd-hostnamed service for the changes to take effect:

sudo systemctl restart systemd-hostnamed

 

Method 3: Using the nmcli command

nmcli is a command-line tool for controlling the NetworkManager and can also be used to change the system’s hostname.

To view the current hostname, type:

sudo nmcli g hostname

To change the hostname to host.nexonhost.com use the following command:

sudo nmcli g hostname host.nexonhost.com

For the changes to take effect, restart the systemd-hostnamed service:

sudo systemctl restart systemd-hostnamed

 

Conclusion

To set or change the hostname on CentOS system, use the hostnamectl set-hostname command followed by the new hostname.