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.
- 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:
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
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:
Set Pretty Hostname with hostnamectl
To modify the pretty hostname, append
--pretty to the command:
sudo hostnamectl set-hostname "New Pretty Hostname" --pretty
sudo hostnamectl set-hostname "PNAP's Test VM" --pretty
hostnamectl to verify the changes:
Set Transient Hostname with hostnamectl
Following the pattern from the previous example, set a transient hostname by adding
--transient to the command.
sudo hostnamectl set-hostname testvm1.phoenixnap.com
Check the changes with
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:
The tool opens in the same window.
2. Select Set system hostname using the keyboard arrows.
3. Enter the desired hostname and select <OK>.
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
sudo nmcli g hostname newhostname.domain
sudo nmcli g hostname vm.phoenixnap.com
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.
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.