This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

A computer hostname represents a unique name that gets assigned to a computer in a network in order to uniquely identify that computer in that specific network.

During the installation of the CentOS, the default hostname is set as “localhost”. But if you want to change the hostname to your own choice, you can follow the instructions in this tutorial. We will guide you to change the hostaname of your CentOS 7.

Displaying Current Hostname

You can display the current hostname of the system by running the following command

root@codesposts:~$ hostnamectl

   Static hostname: server1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: kvm
  Operating System: CentOS Linux 7 (core)
            Kernel: Linux 3.10.0-1062
      Architecture: x86-64

Instructions To Change Hostname

To change your current hostname to a new one, you can follow the instructions below

Changing Hostname Using hostnamectl Command

Run the following command to change your hostname.

root@codesposts:~$ hostnamectl set-hostname codesposts
root@codesposts:~$ hostnamectl set-hostname codesposts --pretty
root@codesposts:~$ hostnamectl set-hostname codesposts --static
root@codesposts:~$ hostnamectl set-hostname codesposts --transient

Replace the “codesposts” with your new hostname.

The hostnamectl command does not produce output in case of successful execution.

To apply the new hostname, you need to reboot the system.

root@codesposts:~$ init 6
root@codesposts:~$ systemctl reboot
root@codesposts:~$ shutdown -r

Changing Hostname By Editing /etc/hostname

Another method to change the hostname on CentOS 7 is to edit the file /etc/hostname using the following command

root@codesposts:~$ vim /etc/hostname

Look for your old hostname in the file and replace it with your new hostname then save the file and exit.

You need to restart your system to apply the settings.

Changing Hostname By Using nmcli Command

Another method to change your hostname is to use nmcli command as follows

To Display your current hostname, run the following command

root@codesposts:~$ nmcli general hostname

To change the hostname, run the following command

root@codesposts:~$ nmcli general hostname codesposts

Replace “codesposts” with your new hostsname

Then run the following command to apply the changes

root@codesposts:~$ systemctl restart systemd-hostnamed

Verification Of New Hostname

You can verify the new hostname by running the following command

root@codesposts:~$ hostnamectl

   Static hostname: codesposts
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: kvm
  Operating System: CentOS Linux 7 (core)
            Kernel: Linux 3.10.0-1062
      Architecture: x86-64