The best way to change hostname on CentOS 7 is editing the actual file that controls the hostname.

vi /etc/hostname

Originally, it is:

localhost.localdomain

But, we’ll delete that line and change it to:

buhpc2

buhpc is our hostname, but it also has the subdomain, 2.buhpc.com, that I have configured. Now, the second file that we also have to change is /etc/hosts.

vi /etc/hosts

The original file looks like:

You create a new line with IP address hostname subdomain. If you don’t have a subdomain, leave the subdomain blank. We’ll make a minor change and change the hosts file to:

Restart the network.
systemctl restart network

Let’s see if it worked:

hostnamectl status

Static hostname is set to: 2.buhpc.com
Now, we will have to set nameservers because NetworkManager may have deleted our old nameservers. Nameservers allow us to access subdomains and domains.

vi /etc/resolv.conf

If /etc/resolv.conf does not have any nameservers, you can add this line somewhere in the file:

nameserver 8.8.8.8