Setting / Changing hostname of a CentOS host

This basic guide shows you how to change the hostname of a CentOS install using your favorite editor from the command line.

Open the file /etc/sysconfig/network using your favorite editor, auted as root or using sudo. We'll use nano for this example.

[root@daddylinux~]# nano -w /etc/sysconfig/network

The file will contain something along the lines of this:

NETWORKING="yes"
GATEWAY="10.0.0.1"
HOSTNAME="www.example.com"

In order to change the hostname, you need to edit the HOSTNAME value. For an example if you wanted your hostname to be server1.example.com you would set it to:

NETWORKING="yes"
GATEWAY="10.0.0.1"
HOSTNAME="server1.example.com"

Once you have set the desired hostname, save the changes and close the editor. If you used nano, this is done by pressing CTRL+x and then Enter to write/save the modified file. The new hostname will not be applied until your next reboot.

If you would like to temporarily apply the new hostname (until the next reboot), you can use the hostname command:

[root@daddylinux~]# hostname server1.example.com

In order to verify the new hostname, simply issue the command on it's own:

[root@daddylinux~]# hostname
server1.example.com


In order for your new hostname to resolve properly, you should also update your /etc/hosts file to reflect the change. For an example:

127.0.0.1 server1.example.com localhost localhost.localdomain

source : centosforge.com

2 comments:

Post a Comment