[Linux]: How to Change Hostname on CentOS/RHEL – 7/8

Updated on July 3, 2023

My system administrator installed minimal CentOS server and forgotten to setup the hostname. Now it has localhost.localadmin. How would I change the hostname on CentOS?

Solution: A computer hostname represents a unique name that identifies a machine on a network. You need follow few rules in setting up the hostname:

    • You shouldn’t assign the same hostname for two different machines on the same network.
    • hostnames can contain letters, digits (from a to z, 0 to 9)
    • hostnames can contain only the hyphen character ( - ) as a special character
    • hostnames can contain the dot special character ( . )
    • hostnames can contain a combination of all three rules but must start and end with a letter or a number
    • hostnames letters are case-insensitive.
    • hostnames must contain between 2 and 63 characters long.
    • recommended to use a fully-qualified domain name (FQDN)

This tutorial guides you through to change hostname on CentOS system.

Display the hostname on CentOS/RHEL 7/8

Below are few different ways you can view the current hostname:

-s flag is for the short name and -f for the FQDN.

# hostname

# hostname -s 

# hostname -f

# cat /etc/hostname
# hostnamectl
   Static hostname: controller.tg.com
         Icon name: computer-server
           Chassis: server
        Machine ID: 79844c6ca7f24d85a5becbd55aa4a6c9
           Boot ID: 406b7146411e403eaceefdc06a769eff
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.80.1.el7.x86_64
      Architecture: x86-64

Change Hostname on CentOS/RHEL 7/8

Open the file /etc/hostname and change the hostname appropriately.

# vim /etc/hostname

Edit /etc/hosts file and replace any occurrence of the existing computer name with the new name.

# vim /etc/hosts

You can also use the hostnamectl command as below:

# hostnamectl set-hostname your-new-hostname

Reboot the server for changes to take effect.

# systemctl reboot

Verify the Hostname on CentOS/RHEL 7/8

After the machine is rebooted, use the hostnamectl command to verify that the hostname was successfully changed.

# hostnamectl

Was this article helpful?

Related Articles

Leave a Comment