How to Set or Change Hostname in CentOS and Rocky Linux

November 14, 2024

Introduction

A hostname is a unique label assigned to a device on a network. The primary purpose of the hostname is to provide a way for the device to identify itself to other network hosts.

This article shows how to set or change a hostname in CentOS and Rocky Linux.

How to Set or Change Hostname in CentOS and Rocky Linux.

Prerequisites

How to Change CentOS and Rocky Linux Hostname

There are three types of hostnames on Linux:

  • Static hostname. The primary hostname used by servers to identify another server.
  • Pretty hostname. This hostname type allows for more characters and punctuation. However, since it uses non-standard characters, the machines cannot read it. Its purpose is to provide users with easy-to-read system IDs.
  • Transient hostname. A temporary name that a device on a network receives from network configuration services such as DHCP and mDNS. It usually resets after a system reboot.

The following sections explain how to set and modify all three hostname types.

Set Static Hostname via CLI

CentOS and Rocky Linux only allow Fully Qualified Domain Names (FQDNs) to act as hostnames. An FQDN must:

  • Have between two and 63 characters.
  • Consist of lowercase letters (a-z), numbers (0-9), and symbols ('.' and '-').
  • Start and end with a lowercase letter or a number.

Set a new hostname by typing the following command in the terminal, replacing [hostname] with a valid string of your choice:

hostnamectl set-hostname [hostname]

For example:

hostnamectl set-hostname phoenixnap

The command provides no output. To verify the hostname, use the following command:

hostname

The output shows the currently set hostname.

The current hostname in the output of the hostname command.

Alternatively, execute the hostnamectl command without any arguments:

hostnamectl

The output returns the static hostname and other information about the network configuration and operating system.

Output of the hostnamectl command showing the static hostname.

Set Static Hostname by Editing /etc/hosts File

While the hostnamectl command is the recommended method to change the hostname on CentOS and Rocky Linux. It is also possible to perform this action by editing the hosts file on the system. Follow the steps below to change the hostname in the hosts file:

1. Open the file in a text editor such as Nano:

sudo nano /etc/hosts

2. Look for the line that begins with 127.0.0.1 (the IP address that refers to the system you are working on). The line contains the reference to the current hostname in the following format:

127.0.0.1  localhost localhost.localdomain localhost 4 localhost4.localdomain4 [hostname]

3. Change [hostname] to a new hostname.

The contents of the hosts file.

4. Save the file and exit.

5. Restart the system.

Note: To learn more about the hosts file, check out our articles How to Edit Hosts File on Mac, How to Edit hosts File on Linux or How to Edit hosts File on Windows.

Set Static Hostname with nmtui

nmtui is a terminal user interface (TUI) program for network management that can set and modify the hostname on the system. Proceed with the steps below to change the hostname using nmtui:

1. Launch nmtui by entering:

nmtui

2. Use arrows to select the Set system hostname option, then press Enter.

The nmtui options menu.

3. Type the new hostname in the Hostname text field, then select OK.

Setting a hostname in nmtui.

4. Use administrative credentials to authenticate and confirm the hostname change.

Set Pretty Hostname

The hostnamectl command can also set pretty hostnames in CentOS and Rocky Linux. Use the following syntax to set a pretty hostname on the system:

hostnamectl set-hostname "[pretty_hostname]"

For example:

hostnamectl set-hostname "phoenixNAP's server"

The system reads the complex characters inside the quotes as a pretty hostname. It also removes the non-FQDN characters and uses the rest to form the static hostname.

Checking the hostname info confirms the changes:

hostnamectl
Using a pretty hostname on CentOS.

To set a pretty hostname only, i.e., without changing the static hostname, add the --pretty option to the hostnamectl command:

hostnamectl --pretty set-hostname "[pretty_hostname]"

Set Transient Hostname

Set a transient hostname by using the --transient option:

sudo hostnamectl --transient set-hostname [hostname]

Note: The transient hostname is not used if a static hostname is already set on the system.

The change lasts until the first reboot.

Conclusion

After reading this tutorial, you know how to change the hostname on your CentOS and Rocky Linux systems. The article covered different ways to set the static hostname and described the procedure for changing pretty and transient hostnames.

If you are new to Linux networking, read how to find your IP address in Linux.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Change or Set Hostname on CentOS 8 / RHEL 8
August 10, 2020

If you decide to change your hostname, you have multiple available options to do so. Follow the methods in...
Read more
How to Change Hostname on Ubuntu
February 27, 2019

Learn The Simple Way How to Change Hostname on Ubuntu. Multiple options, including using Linux command...
Read more
How to Flush DNS Cache Locally in Windows, macOS, & Linux
September 10, 2024

DNS cache can be corrupted for a number of different reasons, including network attacks or viruses. When that...
Read more
How to Edit Hosts File on Windows
December 6, 2023

Find out how to edit the hosts file in Windows and control domain name resolution on your local machine.
Read more