How to Change Hostname in RHEL 8 / CentOS 8

Hostname is the name or label of a computer or a network device. Computer or network device has it own IP address but it is very difficult for the humans to remember it that’s why hostname is set on computer and network devices. Hostname is a human readable string which one can easily remember.

In this guide, we will learn how to change hostname on RHEL 8 and CentOS 8 system. Basically, there are three different methods through which we can change hostname.

  • hostnamectl
  • nmtui
  • nmcli

Types of Hostname that we can set on RHEL 8 / CentOS 8 systems.

  • Static Hostname : It is conventional hostname that we set on the servers and as the name suggest hostname will be static and persistent across the reboot.Static hostname is stored in the file /etc/hostname.
  •  Transient Hostname : It is the hostname which is obtained from DHCP and mDNS. Transient hostname might be temporary because it is only temporarily written to kernel hostname.
  •  Pretty Hostname : It is a hostname that can include all kind of special characters. Pretty hostname is stored in the file /etc/machine-info .

Note : Default hostname on RHEL and CentOS system is ‘localhost.localdomain’, when it not set at the time of installation.

How to View Hostname on RHEL / CentOS ?

We can use hostname and hostnamectl commands to view the current hostname of our RHEL or CentOS system. Open the terminal and run,

$ hostname
OR
$ hostnamectl status
OR
$ hostnamectl

Sample output of above commands would look like below,

hostname-rhel-centos

Change hostname with hostnamectl Command 

hostnamectl command is used to set, change and query hostname. Basic syntax is listed below :

$ hostnamectl set-hostname <new_hostname>

Let’s set the static hostname ‘cloud.linuxtechi.local’

$ sudo hostnamectl set-hostname "cloud.linuxtechi.local"
$ exec bash

Verify the new hostname using hostnamectl and hostname command :

View-Hostname-RHEL-CentOS

How to remove or clear hostname ?

If you want to remove or clear hostname on RHEL or CentOS  system, then use beneath hostnamectl command,

$ sudo hostnamectl set-hostname ""
$ exec bash
$ hostname
localhost
$

Remove-hostname-hostnamectl

How to Set Pretty Hostname RHEL / CentOS  ?

To set the pretty hostname, use the following command

$ sudo hostnamectl set-hostname "LinuxTechi's RHEL 8 Laptop" --pretty

Set-Pretty-Hostname-RHEL-CentOS

How to remove or clear pretty hostname ?

Use the following hostnamectl command to remove or clear  pretty hostname,

$ sudo hostnamectl set-hostname "" --pretty

How to set hostname on the remote system from local machine ?

Using hostnamectl command we can also set hostname on remote system from the local system. Example is shown below,

Syntax :

$ hostnamectl set-hostname -H <Remote-System-IP> @<new_hostname>

Above command will use ssh for connecting and authentication for remote server.

$ sudo hostnamectl set-hostname -H 192.168.1.13 @cloud.linuxtechi.local
[email protected]'s password:
$

Change hostname with nmtui command 

nmtui stands for ‘Network Manager Text User Interface‘, it is a text user interface which is used to configure networking along with hostname on modern Linux distributions.

When we type nmtui command below window will appear

$ sudo nmtui

nmtui-command-rhel-centos

Choose “Set system hostname” and then click on OK..

Set-Hostname-nmtui-RHEL-CentOS

Type the hostname whatever you want to set and then click on OK.

Choose-Ok-Set-Hostname-nmtui-command

To make the above changes into the effect, restart hostnamed service, run

$ sudo systemctl restart systemd-hostnamed

Now , we can verify new hostname using ‘hostname’ & ‘hostnamectl’ commands.

Change hostname with nmcli command

nmcli is a command line utility for configuring network connections and hostname on RHEL and CentOS systems.

To view the current hostname via nmcli command, run

$ nmcli general hostname
cloud.linuxtechi.local
$

To change the hostname, run

Syntax :

$ sudo nmcli general hostname <new_hostname>

$ sudo nmcli general hostname web.linuxtechi.local

Restart hostnamed service using beneath systemctl command

$ sudo systemctl restart systemd-hostnamed
$ hostname
web.linuxtechi.local
$

That’s all from this guide, I hope you found it informative. Kindly post you queries and feedback in below comments section.

Also Read : How to Create Sudo User on RHEL | Rocky Linux | AlmaLinux

Leave a Comment

Your email address will not be published. Required fields are marked *