If you are a Linux system administrator or trying to learn Linux, then you know how important it is to properly change the hostname of your server. 

Recently, I received a request of changing the hostname for one of the machines that is running on rhel8 therefore I decided to write this article to share the different ways of changing the hostname in RHEL8.

In this blog post, we will explore different ways with the simple tutorial guide to successfully change the hostname in RHEL (Red Hat Enterprise Linux)  8 so that you can confidently make the changes you need to keep your system running smoothly.

What are the different types of hostnames that you can configure?

In RHEL 8 (Red Hat Enterprise Linux 8) and other modern Linux distributions, there are three different types of hostnames that can be configured. Here’s a brief explanation for the different hostnames you will see in RHEL8

Static Hostname

This is the traditional hostname, which is set at system startup from /etc/hostname or a variety of other configuration files depending on the system’s configuration.

This is the most crucial one, as it’s used by the system and its networking services for identifying itself. Unless you manually change it or a system administrator changes it, this hostname remains the same.

Transient Hostname

It is a dynamic hostname assigned to the system at runtime, maintained only until the next reboot. It’s used by the kernel and is managed dynamically by the system manager (systemd). The transient hostname takes precedence over the static hostname. DHCP or mDNS servers often set it, and it might be more temporary compared to the static hostname.

Pretty Hostname

This is a free-form UTF-8 hostname for presentation to the user. It’s not used in any protocol communications.

The pretty hostname is typically a descriptive name that might include spaces, special characters, or international characters. An example might be something like “Zac’s Laptop”.

Remember that the static and transient hostnames should be in the format defined by Internet standards, including RFC 952 and RFC 1123. This means they should consist only of 7-bit ASCII lower-case characters, no special chars or spaces, and a length limit of 63 characters. The pretty hostname doesn’t have these restrictions.

How to View Hostname on RHEL 8?

Firstly, we will touch on how to display your current RHEL 8  machine’s hostname first before we change the hostname.

There are a few ways to display the current hostname (static hostname)  using the commands below:

Using hostname command

You can use the hostname command and refer to the example below:

using hostname command to display hostname for rhel8 and centos 8

Using cat /etc/hostname

You also can use cat /etc/hostname where this is the file containing the name of the local system. The output is similar to using the hostname command

cat /etc/hostname
using cat /etc/hostname to show current hostname in rhel8 and centos 8

Using nmcli command

You can use nmcli command too for changing the hostname:

nmcli general hostname

Using hostnamectl command

The hostnamectl command is a quick and easy way to gather a lot of system information in one place, making it one of the most useful tools for system administrators to understand the system’s configuration.

using hostnamectl command to show system info in rhel

Running hostnamectl without any additional arguments will display the current system hostname settings, including other information the server running like:

Icon Name: This is used by some graphical interfaces to display an appropriate icon for the machine.

Operating System: This is the name and version of the operating system currently running on the machine.

Chassis: This indicates the type of the machine. It can show whether the system is a desktop, laptop, server, vm or some other type of machine.

Machine ID: This is a unique identifier for the machine, generated when the operating system is first installed. It remains constant across system reboots.

Boot ID: This is another unique identifier, but unlike the Machine ID, this one changes each time the system boots.

Virtualization: This field indicates whether the system is running inside a virtual machine and, if so, which virtualization technology is being used (for example, qemu, kvm, xen, vmware, etc.).

Kernel: This indicates the version of the Linux kernel that is currently running on the system.

Architecture: This is the CPU architecture of the machine, such as x86_64, arm, arm64, etc.

How to change the hostname in the RHEL 8 machine?

In order to change the hostname,  you will need to use either vi or nano text editor in your RHEL server.  Use the command to edit /etc/hostname:

sudo nano /etc/hostname
sudo vi /etc/hostname

You should able to see the current hostname in the first and only line. Delete the old hostname and input the new hostname

sudo vi /etc/hostname

Save the file simply hit your ESC button exit the insert mode and type  :wq for vi editor

Next, you also need to check the host file which is located /etc/hosts 

sudo vi /etc/hosts 
sudo nano /etc/hosts

Remove any old hostname and replace it with the new hostname. Save the file and exit the nano or vi editor

Reboot your server in order for the changes to take effect. You can use the following command: reboot or shutdown -r

Lastly, verify the new hostname after the machine reboot by using the hostname or hostnamectl command etc to confirm the new hostname.

How to change the hostname in RHEL 8 without restarting the system?

Alternatively, you also can change or set the hostname without restarting the system by using this command below:

sudo hostname <your new hostname>
How to change the hostname in RHEL 8 without restarting the system?

As you can see, it is updated as Transient hostname first. Next, you will need to update hostname file using vi or nano editor as well in the /etc/hostname by using this command

sudo vi /etc/hostname or sudo nano /etc/hostname

Lastly, do the same for the host file in /etc/hosts using vi or nano editor.

How to change the hostname with hostnamectl command?

To use the hostnamectl command to change the hostname of your RHEL 8 system,  use the following command to set a new name for your server :

sudo hostnamectl set-hostname <yournewhostname>
How to change the hostname with hostnamectl command?

How to change the hostname using nmtui command?

To access to NetworkManager Text User Interface, type the following command:

nmtui stands for “NetworkManager Text User Interface“. It is a command-line utility that provides a graphical text interface to configure networking by controlling and managing network connections, editing connection details, setting your system hostname, and more.

sudo nmtui

Select Set system hostname in the option and press enter

nmtui - set system hostname

Enter the new hostname and select OK

set hostnamein networkmanagertui

It will prompt the confirmation message and select the OK button to proceed

set new hostname with nmtui

You may want to verify the hostname again using the command we covered earlier:

set hostname with nmtui

How to change hostname with nmcli command?

To set or change the hostname using nmcli command, you can try the following

sudo nmcli general hostname <your_newhostname>
How to change hostname with nmcli command?

How to set or change the pretty hostname?

To configure a new pretty hostname in rhel8, you can use the hostnamectl command with –pretty . You can see the command below:

sudo hostnamectl set-hostname "<your new pretty hostname>" --pretty
How to set or change the pretty hostname?

Can I use all these commands for the Centos 8 operating system too?

Yes, all these tutorials above with hostnamectl,  nmcli , nmtui to change the hostname also can be used on Centos 7 & 8 as well.

This is because CentOS and RHEL (Red Hat Enterprise Linux) are very closely related. In fact, CentOS is essentially a free version of RHEL, with only minor differences. So most of the command line utilities and their usage, including hostnamectl and nmtui, are the same between the two distributions.

Wrapping Up

I hope this blog post was helpful, and I hope you found it informative, especially for those who are new to Linux.  

 In this blog post, we have shown you different ways of changing hostname on  RHEL 8 operating system. Let me know if you have any questions. I would like to hear from you.

Categorized in: