Skip to content

How to Change hostname on CentOS 7

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

vi /etc/hostname

Originally, it is:


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


buhpc is our hostname, but it also has the subdomain,, 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:
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: