How to Set Hostname on CentOS 8/RHEL 8

Why do we need Hostname?

Typically the hostname is set during the installation of the operating system or dynamically assigned to the virtual machine when it is created.

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 Hostname

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, thestack.net.
  • 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 myweb.thestack.net.

Note: If logging in as a user other than admin or root, you will need to use sudo command to gain root access.

Displaying the Current Hostname

To view the current hostname, execute the following command:

hostnamectl

In this example, the current hostname is set to myweb.thestack.net.

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. For example, to change the system static hostname to myweb.thestack.net, you would use the following command:

 hostnamectl set-hostname myweb.thestack.net

To set the pretty hostname to thestack.net, enter:

 hostnamectl set-hostname "thestack.net" --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:

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:

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:

nmcli g hostname

To change the hostname to ‘myweb.thestack.net’ use the following command:

 nmcli g hostname myweb.thestack.net`

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

systemctl restart systemd-hostnamed

Related Articles