Change the Hostname of a CentOS Server

Overview

This short tutorial will guide you through renaming your CentOS 6 server. Like many settings in the Linux world, you can make the change temporary or persistent. I show you both ways and explain why you would even consider a temporary change.

Temporary Change

You can temporarily change the name of your server using the hostname command. You might be wondering why someone would do this. One reason for doing this is a persistent hostname change requires a reboot. You may want to hold off on rebooting until other tasks are completed, however, some tasks require the hostname to be set. This approach allows you to do that.

  1. Run the hostname command with the fully qualified host name you want the server to have.
    hostname my-new-server.serverlab.intra

Permanent Change

In order for our hostname change to survive a reboot, we need to make the configuration persistent. We do this by setting the host name in our global network configuration file. Without defining it there, our hostname will be reset the next time our server boots.

  1. Open the global network configuration file into a text editor.
    nano /etc/sysconfig/network
  2. Find the HOSTNAME setting. It’s default value will depend on your environment.
    HOSTNAME=localhost
  3. Replace the HOSTNAME value with the server’s chosen fully qualified domain name.
    HOSTNAME=my-new-server.serverlab.intra
  4. Save your changes and exit the text editor.
  5. To apply the hostname changes immediately, reboot the server.