Changing server hostname RHEL7 CentOS7


CentOS 7 supports three types of Hostnames:

Static – The static host name is traditional host which can be chosen by the user and is stored in /etc/hostname file.

Transient – The transient host name is maintained by kernel and can be changed by DHCP and mDNS.

Pretty – It is a free form UTF -8 host name for the presentation to the user.

A Hostname rules –

  • Up to 64 character in a length
  • Consisting of only a-z,A-Z, 0-9, “-”, “_” and “.”
  • Recommend defining FQDN

How to change –

Before changing the hostname, check the current hostname.

[root@localhost ~]# hostname
localhost.localdomain

1. nmtui tool:

NetworkManaget tool is used to set the static hostname in /etc/hostname

nmtui -Select Set HostName
nmtui -Select Set HostName

Set the hostname.

nmtui - Change HostName 2
nmtui – Change HostName 2

restart hostnamed to change the static hostname

[root@localhost ~]# systemctl restart systemd-hostnamed

Verify the change in hostname

[root@server ~]# hostname
server.itzgeek.com
[root@server ~]# cat /etc/hostname
server.itzgeek.com
[root@server ~]# cat /etc/sysconfig/network
# Created by anaconda
HOSTNAME=server.itzgeek.com

2. hostnamectl:

hostnamectl is used in RHEL7 & CentOS7 to change the hostname, this tool changes all three types of host names; such as static host name.

Check the current host name.

[root@server ~]# hostnamectl status
Static hostname: server.itzgeek.com
Icon name: computer-vm
Chassis: vm
Machine ID: 565ea8b749544aca9d5563308f9e4bc2
Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
Virtualization: vmware
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

Set the hostname.

[root@server ~]# hostnamectl set-hostname client.itzgeek.com

Using hostnamectl to change the hostname does not require a reboot, only a logoff / logon to take affect.

[root@client ~]# hostnamectl status
Static hostname: client.itzgeek.com
Icon name: computer-vm
Chassis: vm
Machine ID: 565ea8b749544aca9d5563308f9e4bc2
Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
Virtualization: vmware
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

3. nmcli tool:

Is used to query and setup a static host name in /etc/hostname file.

Check the hostname

[root@client ~]# nmcli general hostname
client.itzgeek.com

Change the hostname

[root@client ~]# nmcli general hostname server.itzgeek.com

restart hostnamed to cause an update in static host name.

[root@client ~]# systemctl restart systemd-hostnamed

4. Edit /etc/hostname

This method requires a reboot of the server to take effect

Leave a comment