The easiest way to change the hostname on CentOS 7 is to use the hostnamectl command.
First I will check the current hostname by running "hostnamectl status" on the shell of my server:
hostnamectl status
The output on my system is:
[[email protected] ~]# hostnamectl status Static hostname: server1.example.com Icon name: computer-vm Chassis: vm Machine ID: d89865d34b5a4637a9a4ff0ce0f6da02 Boot ID: 56d1685056d743b39e57a7b9cbfe467c 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
Then I change the hostname with the set-hostname option of the hostnamectl command to server2.example.com
hostnamectl set-hostname server2.example.com
Afterward, I check with the command hostname and hostname -f if the hostname change has succeeded.
hostname
hostname -f
The result should be:
[[email protected] ~]# hostname server2.example.com [[email protected] ~]# hostname -f server2.example.com
Instead of the hostname command you could also use the "hostnamectl status" command again to check if the new hostname has been set:
[[email protected] ~]# hostnamectl status Static hostname: server2.example.com Icon name: computer-vm Chassis: vm Machine ID: d89865d34b5a4637a9a4ff0ce0f6da02 Boot ID: 56d1685056d743b39e57a7b9cbfe467c 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