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

Hostname is defined as label or name of a computer and network device. In this article we will discuss how to set and change hostname on CentOS 7 & RHEL 7 system. There are three different commands through which we can query, set and change hostname.

  • hostnamectl
  • nmtui
  • nmcli

Types of Hostname that we can set on CentOS 7 and RHEL 7 server.

  • 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 .

How to check the Hostname on CentOS 7 / RHEL 7 ?

Using the hostname and hostnamectl command, we can view the hostname of our CentOS 7 and RHEL 7 system,

[[email protected] ~]# hostname
[[email protected] ~]# hostnamectl status
[[email protected] ~]# hostnamectl

Hostnamectl Command 

hostnamectl command is used to configure,modify and query hostname. Basic syntax is listed below :

# hostnamectl <options> <new_hostname>

Let’s set the static hostname ‘

[[email protected] ~]# hostnamectl set-hostname ""
[[email protected] ~]#

Verify the new hostname using hostnamectl and hostname command :


How to remove or clear the hostname ?

If you want to remove or clear hostname on CentOS 7 and RHEL 7 system, then use the following hostnamectl command,

[[email protected] ~]# hostnamectl set-hostname ""
[[email protected] ~]# 
[[email protected] ~]# hostname
[[email protected] ~]# hostnamectl 
 Static hostname: n/a
Transient hostname: localhost
 Icon name: computer
 Chassis: n/a
 Machine ID: a5c10f2a26324894a3b0b83d504c1ff2
 Boot ID: c3ff9d084b364b56bb0d588b64ff6f1a
 Virtualization: oracle
 Operating System: CentOS Linux 7 (Core)
 CPE OS Name: cpe:/o:centos:centos:7
 Kernel: Linux 3.10.0-123.el7.x86_64
 Architecture: x86_64
[[email protected] ~]#

How to Set Pretty Hostname on CentOS 7 / RHEL 7 ?

[[email protected] ~]# hostnamectl set-hostname "Pradeep Antil's CentOS-7 Laptop" --pretty
[[email protected] ~]#


How to Remove or flush pretty hostname ?

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

[[email protected] ~]# hostnamectl set-hostname "" --pretty

How to set hostname on the Remote Server from your local machine.

Using hostnamectl, we can also set the hostname on remote system from the local system,

Syntax :

# hostnamectl set-hostname -H <Remote-Server-ip> @<new_hostname>

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

[email protected]:~# hostnamectl set-hostname -H
[email protected]'s password:
[email protected]:~#

nmtui command 

nmtui stands for ‘Network Manager Text User Interface‘, it is a text user interface which is used to configure hostname.

When we type nmtui command below window will appear

[[email protected] ~]# nmtui


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


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


Restart the hostnamed service

[[email protected] ~]# systemctl restart systemd-hostnamed
[[email protected] ~]#

Verify new hostname using ‘hostname’ & ‘hostnamectl’ command

nmcli command

nmcli is a command line utility for configuring and query hostname.

Check the hostname :

[[email protected] ~]# nmcli general hostname
[[email protected] ~]#

Change or set hostname :

Syntax :

# nmcli general hostname <new_hostname>

[[email protected] ~]# nmcli general hostname

Restart the hostnamed service using below systemctl command

[[email protected] ~]# systemctl restart systemd-hostnamed
[[email protected] ~]# hostname
[[email protected] ~]#

Note : hostnamectl command is recommended to query and configure hostname

Leave a Reply

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