How to Change or Set Hostname on CentOS 8 / RHEL 8

August 10, 2020

Introduction

The hostname of a machine is a label that differentiates one system from another on a single network.

During the installation of Centos 8, either the admin defines a hostname or one is automatically created. The assigned system name is not always easy to remember. Later, if you add more machines to the network, you may want to organize hostnames.

In this tutorial, learn how to change the hostname on Centos 8 / RHEL.

tutorial on setting or changing the hostname on centos 8

Prerequisites

  • CentOS 8 / RHEL system
  • A user with Sudo or root permissions
  • Access to a terminal or command line
  • Any built-in text editor

Understanding the Types of Hostnames

There are three classes of hostnames:

  • The traditional hostname, located at /etc/hostname. The guide focuses on this type.
  • A human-readable hostname that allows special characters (e.g., pnap’s server). The system only uses it as a presentation to the user. It is located in the /etc/machine-info directory.
  • A changeable hostname that the Linux kernel maintains. By default, it is the same as static, but DHCP and mDNS can change it, or you can change it manually.

The static hostname is defined by default as localhost, while the other two need to be specified separately.

How to Display Current Hostname

To check a machine’s hostname, open the terminal and enter:

hostname
Checking CentOS hostname in the terminal.

Our virtual machine has the default hostname, localhost.localdomain.

Check out our article on the hostname command to learn more about this command and how to use it.

Another way to check the static hostname and get more details about the machine is to use the hostnamectl command:

hostnamectl

Change Hostname on CentOS 8 / RHEL without Restarting the System

In some circumstances, rebooting a machine is not an option. There are multiple methods to change the hostname on CentOS 8 or RHEL without having to restart the machine.

Select the option you prefer and then log out and log back in to make sure the changes have taken effect.

Note: It does not matter if you are creating virtual machines or booting physical servers, always assign hostnames in a logical manner. The maximum number of characters you can use is 64.

Use hostnamectl to Change Hostname

The hostnamectl tool allows you to set all classes of hostnames. The command works on other Linux distributions as well, not only CentOS / RHEL.

Set Static Hostname with hostnamectl

The syntax to set a static hostname with hostnamectl is:

sudo hostnamectl set-hostname host.domain

For example, we will change the hostname to vm1.phoenixnap.com:

sudo hostnamectl set-hostname vm1.phoenixnap.com

The terminal returns a new line. Use one of the two methods we mentioned to verify if the hostname was changed:

Using hostnamectl to change a hostname on CentOS 8

Set Pretty Hostname with hostnamectl

To modify the pretty hostname, append --pretty to the command:

sudo hostnamectl set-hostname "New Pretty Hostname" --pretty

For example:

sudo hostnamectl set-hostname "PNAP's Test VM" --pretty

Enter hostnamectl to verify the changes:

Set pretty hostname CentOS 8 with hostnamectl

Set Transient Hostname with hostnamectl

Following the pattern from the previous example, set a transient hostname by adding --transient to the command.

For example:

sudo hostnamectl set-hostname testvm1.phoenixnap.com

Check the changes with hostnamectl:

Using hostnamectl to set the transient hostname in CentOS 8

Use nmtui to Change Hostname

Launch the NetworkManager TUI with the nmtui command to change the hostname. This is not the primary function of the tool, but it allows users to set a new hostname.

1. In a terminal window, enter:

nmtui

The tool opens in the same window.

2. Select Set system hostname using the keyboard arrows.

Using nmtui to set the hostname in CentOS 8

3. Enter the desired hostname and select <OK>.

Entering the hostname in the nmtui tool in CentOS 8

4. Enter your password if prompted, and confirm the changes.

5. Quit the tool.

6. You may need to restart the system-hostnamed to apply the changes:

sudo systemctl restart system-hostnamed

You have successfully changed the hostname on your CentOS 8 or RHEL machine.

Use nmcli to Change Hostname

The nmcli tool is also used to control the NetworkManager, but without the GUI.

The command to change the hostname with nmcli is:

sudo nmcli g hostname newhostname.domain

For example:

sudo nmcli g hostname vm.phoenixnap.com

Restart sytemd-hostanmed to apply the changes:

sudo systemctl restart system-hostnamed

Check the hostname using one of the methods we mentioned previously. In this case, we will use nmcli to check the hostname:

sudo nmcli g hostname

Set Hostname by Editing /etc/hostname File (Restart Required)

If you are accustomed to editing system files, change the hostname by making the following changes:

1. Open the /etc/hostname file using your preferred text editor. In this example, we will use nano:

sudo nano /etc/hostname

2. Change the current hostname to the desired hostname.

3. Save the changes and exit the editor.

4. Restart the system to apply the changes.

Changing the hostname on CentOS 8 or RHEL by editing the hostname file in a text editor.

Note: For more details on how to change the hostname other Linux systems, refer to our tutorials How To Change Hostname On CentOS 7, How To Change Hostname On Ubuntu 18.04, How To Change Hostname On Ubuntu 20.04 or How to Change Hostname in Debian 10.

Conclusion

This tutorial explained different options to change the hostname on CentOS 8 /RHEL. Select the system name you prefer, and quickly modify the hostname on your CentOS 8 system.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Install LAMP Stack on CentOS 8
July 19, 2020

The LAMP stack consists of open-source software required for web development. Install and configure the LAMP...
Read more
How to Change Hostname on Ubuntu 18.04
February 27, 2019

Learn The Simple Way How to Change Hostname on Ubuntu 18.04. Multiple options, including using Linux command...
Read more
How to Edit Hosts File on Mac
November 21, 2023

The simplest way to edit the hosts file is by using the Mac Terminal app. Follow the steps in the article...
Read more
How to Set or Change a Hostname in CentOS 7
October 21, 2018

This technical guide is for users who already have a server running the CentOS 7 operating system on a...
Read more