How to Change Hostname on CentOS 7

In computer networking, a hostname is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication, such as the World Wide Web. Hostnames may be simple names consisting of a single word or phrase, or they may be structured.

Displaying the Current Hostname

Find out the hostname of your system by using the below command,

hostnamectl

Output:

[root@vps ~]# hostnamectl 
   Static hostname: vps.server.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a7b84c64685a4294dbc1ddb664be9be6
           Boot ID: bfa9b013aee34b51bc945c6d40fff795
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.92.1.el7.x86_64
      Architecture: x86-64
[root@vps ~]# 

Changing the System Hostname

Now that you know the current hostname, you can change the hostname by using the following command, and Optionally you can also set the pretty hostname as follows.

hostnamectl set-hostname crowncloud.net

hostnamectl set-hostname "crowncloud production server" --pretty

Next, add the record for the hostname in the /etc/hosts file.

127.0.0.1 crowncloud.net

Edit the file using your favorite editor.

nano /etc/hosts

After you've added/edited the file, save and exit the file.

Output:

[root@vps ~]# cat /etc/hosts
127.0.0.1 crowncloud.net

This will also add an entry to the /etc/hostname file automatically.

Restart the systemd-hostnamed service.

systemctl restart systemd-hostnamed

Reboot the server to cross-check the hostname,

reboot 

Alternatively Set or Change the Hostname

Alternatively, you can set or change the hostname of your system using NetworkManager's Text User Interface. Run the following command and follow the steps as shown below,

nmtui

Select set system hostname.

image

Enter the new hostname.

image

Restart the systemd-hostnamed service.

systemctl restart systemd-hostnamed

Verify the Change

To verify the hostname has been fully changed, enter the hostnamectl command.

[root@crowncloud ~]# hostnamectl
   Static hostname: crowncloud.net
   Pretty hostname: crowncloud production server
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a7b84c64685a4294dbc1ddb664be9be6
           Boot ID: 1ca998868b3848e2b0a6a762cde4b452
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.92.1.el7.x86_64
      Architecture: x86-64