How to Change Hostname on CentOS 7/8

A hostname is assigned to a computer, workstations/servers or network node in a network for identification. It is best practice to use a unique hostname for the appliances and systems so that it is easy to identify within the network.

While installing CentOS 7/8, by default hostname has been set by itself as localhost.localdomain.

In this tutorial we will guide you how to change the hostname.

Prerequisites

Logged with user who is having sudo access.

Types

There are three types/classes of hostnames.

  • Static hostname: Static hostname is stored in the /etc/hostname file and most commonly used. The best approach for permanent hostname change.
    • Example: “webserver”, “webserver.linuxyogi”, or “webserver.linuxyogi.com”.

  • Transient/dynamic hostname: For temporary purposes, this approach can be followed. It is a dynamic approach and maintained by the Linux kernel, which means the changes will be lost after a reboot. It can be changed by DHCP or mDNS at runtime.

  • Pretty hostname: Any valid free-form UTF8 name can be used for the hostname. It does not have the limitation of the internet domain.
    • Example: "Yogi's PC".

To view all the current host names, run the below command.

hostnamectl status
or
hostnamectl

Output

[root@localhost ~]# hostnamectl status
     Static hostname: localhost.localdomain
  Transient hostname: status
           Icon name: computer-vm
             Chassis: vm
          Machine ID: d05cfc4db968457e994b2622ed8270f3
             Boot ID: beac3d2cf4394f8298ef29254df0bf5e
      Virtualization: microsoft
    Operating System: CentOS Linux 8
         CPE OS Name: cpe:/o:centos:centos:8
              Kernel: Linux 4.18.0-240.el8.x86_64
        Architecture: x86-64

Below are the acceptance values for the hostname.

  • Letters (from a to z).
  • Digits (from 0 to 9).
  • Hyphen ( – ) and dot ( . ) as a special character.
  • Must start and end with a letter or a number.
  • Characters length must be between 2 and 63 long.

There are many ways to set a hostname. In this tutorial we will use few of them.

Method 1 : "hostnamectl" command
Static:

hostnamectl set-hostname linuxyogi.com

Output

[root@localhost ~]# hostnamectl
    Static hostname: linuxyogi.com
          Icon name: computer-vm
            Chassis: vm
         Machine ID: d05cfc4db968457e994b2622ed8270f3
            Boot ID: beac3d2cf4394f8298ef29254df0bf5e
     Virtualization: microsoft
   Operating System: CentOS Linux 8
        CPE OS Name: cpe:/o:centos:centos:8
             Kernel: Linux 4.18.0-240.el8.x86_64
       Architecture: x86-64

Transient:

hostnamectl set-hostname temporary.linuxyogi --transient

Output

[root@Linuxyogi ~]# hostnamectl
    Static hostname: linuxyogi.com
 Transient hostname: temporary.linuxyogi
          Icon name: computer-vm
            Chassis: vm
         Machine ID: d05cfc4db968457e994b2622ed8270f3
            Boot ID: 474796cd184c4393b17fdf08643e60a5
     Virtualization: microsoft
   Operating System: CentOS Linux 8
        CPE OS Name: cpe:/o:centos:centos:8
             Kernel: Linux 4.18.0-240.el8.x86_64
       Architecture: x86-64

Pretty:

hostnamectl set-hostname "Yogi's PC" --pretty
or
hostnamectl --prettyset-hostname “Yogi's PC”

Output

[root@Linuxyogi ~]# hostnamectl
    Static hostname: linuxyogi.com
    Pretty hostname: Yogi's PC
 Transient hostname: temporary.linuxyogi
          Icon name: computer-vm
            Chassis: vm
         Machine ID: d05cfc4db968457e994b2622ed8270f3
            Boot ID: 474796cd184c4393b17fdf08643e60a5
     Virtualization: microsoft
   Operating System: CentOS Linux 8
        CPE OS Name: cpe:/o:centos:centos:8
             Kernel: Linux 4.18.0-240.el8.x86_64
       Architecture: x86-64