Changing hostname on RHEL

For RHEL 6:

1. Change the HOSTNAME line in /etc/sysconfig/network

2. Change the hostname (FQDN and alias) in /etc/hosts

3. Run /bin/hostname new_hostname for the hostname change to take effect immediately.

4. Run /sbin/service syslog restart for syslog to log using the new hostname.

A reboot is not required to change the system hostname.

For RHEL 7:

Method 1 : hostnamectl

Get the current hostname of the system :

hostnamectl status
Static hostname: localhost.localdomain
Icon name: computer
Chassis: n/a
Machine ID: 55cc1c57c7f24ed0b0d352648024cea6
Boot ID: a12ec8e04e6b4534841d14dc8425e38c
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

To set new hostname (butterfly) for the machine :

hostnamectl set-hostname butterfly

Re-login and verify the new hostname :

hostnamectl
Static hostname: butterfly
Pretty hostname: Geeks LAB
Icon name: computer
Chassis: n/a
Machine ID: 55cc1c57c7f24ed0b0d352648024cea6
Boot ID: a12ec8e04e6b4534841d14dc8425e38c
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

Method 2 : nmcli

Get the current hostname :

nmcli general hostname
localhost.localdomain

To change the hostname to butterfly :

nmcli general hostname butterfly

We need to restart the systemd-hostnamed service for the changes to take effect :

service systemd-hostnamed restart

Re-login and verify the hostname change :

hostname
butterfly

Method 3 : nmtui

We can also change the hostname using the nmtui tool :

nmtui

Select the option to “set the hostname” and hit enter

change hostname nmtui

Set the hostname

Restart the systemd-hostnamed service for the changes to take effect.

service systemd-hostnamed restart

Re-login and verify the hostname change.

Method 4 : Edit /etc/hostname

cat /etc/hostname
localhost.localdomain

To change the hostname to “butterfly”, replace the content of the /etc/hostname file with “butterfly”

echo "butterfly" > /etc/hostname
cat /etc/hostname
butterfly

Restart the system and verify.

shutdown -r now

Sources:

  1. https://www.thegeekdiary.com/centos-rhel-7-how-to-change-set-hostname/
  2. https://my-hlam.livejournal.com/34233.html?utm_source=3userpost