Setting or Changing hostname of a CentOS host

How to set or change your host name in CentOS

Open the file /etc/sysconfig/network using your favorite editor,  I like nano.
[root@yourServer~]# sudo nano -w /etc/sysconfig/network

The file will look something like this:
NETWORKING="yes"
GATEWAY="10.0.5.1"
HOSTNAME="yourServer"

To change the hostname, you need to edit the HOSTNAME value.
For an example if you wanted your hostname to be yourserver01.example.com you would set it to:
NETWORKING="yes"
GATEWAY="10.0.5.1"
HOSTNAME="yourserver01.example.com"

Once you have set the desired hostname, save the changes and close the editor.
In nano this is done by pressing CTRL+x  then Y followed by  Enter to write the modified file. Your new hostname will not be applied until your next network restart.
[root@yourServer~]# sudo reboot

In order to verify the new hostname, simply issue the command on it’s own:
[root@yourServer~]# hostname
yourserver01.example.com

You should also update your /etc/hosts file to reflect the change. For an example:
127.0.0.1 yourserver01.example.com localhost localhost.localdomain

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *