Centos: How to Set Hostname in Centos


Setting the hostname in CentOS (RHEL).

Sometimes during setup of your CentOS system you forget to set the hostname of the server, or you may get situations where you need to change the hostname. This quick tutorial will show you just how easy it is to do this.

Firstly, check the current hostname by entering the command:

# hostname

You can also check hostname by issuing following command:

# sysctl kernel.hostname
kernel.hostname = localhost.localdomain

You can temporarily change hostname by issuing following command, but upon reboot, it will revert to its previous setting

# hostname myservername.domain.com

While this temporary change maybe useful for troubleshooting, to make it permanent, just change the following network file at /etc/sysconfig/network

vi /etc/sysconfig/network

It will look something like:


Hit "i" to insert and change the HOSTNAME value to your FQDN, such as myservername.domain.com


Hit Escape, then :wq to save and exit.

Check that the hostname is now set:

# hostname
# sysctl kernel.hostname
kernel.hostname = myservername.domain.com

Finally, log out and back in using a new session and you'll see you're now logged on as root@myservername

To properly check, reboot your server to see that the change is now permanent.