How to configure or set 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 modify hostname on CentOS 7 & RHEL 7. There are three different commands through which we can query, set and modify 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 accross 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 .

Commands to check the hostname :

[[email protected] ~]# hostname
OR
[[email protected] ~]# hostnamectl status
OR
[[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 ‘cloud.linuxtechi.com

[[email protected] ~]# hostnamectl set-hostname "cloud.linuxtechi.com"
[[email protected] ~]#

Verify the new hostname using hostnamectl and hostname command :

hostnamectl-command-output

Remove or clear the hostname (cloud.linuxtechi.com)

[[email protected] ~]# hostnamectl set-hostname ""
[[email protected] ~]# 
[[email protected] ~]# hostname
localhost.localdomain
[[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] ~]#

Set Pretty Hostname :

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

hostnamectl-transisent

Remove Pretty hostname :

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

Set Hostname on the Remote Server from your local machine.

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 192.168.1.13 @cloud.linuxtechi.com
[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

nmtui-command

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

set-hostname-nmtui

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

numtui-ok-option

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
cloud.linuxtechi.com
[[email protected] ~]#

Change the hostname :

Syntax :

# nmcli general hostname <new_hostname>

[[email protected] ~]# nmcli general hostname antil.linuxtechi.com

Restart the hostnamed service using below systemctl command

[[email protected] ~]# systemctl restart systemd-hostnamed
[[email protected] ~]# hostname
antil.linuxtechi.com
[[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 *

Pin It on Pinterest