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

5 comments:

  1. how can i change a relayhost password on centos?

    ReplyDelete
  2. Have been manually changing my hostname after every reboot because I did not know how to change it permanently - until now - Thank you!

    ReplyDelete
  3. In CentOS 7 use the command hostnamectl

    https://der-linux-admin.de/2014/10/centos-7-hostnamen-aendern/

    ReplyDelete
  4. I absolutely loved how you have created your website, it's simple, neat, simple to navigate and extremely easy on the eyes. Is it possible to let me know which theme or designer did you use.
    cpanel

    ReplyDelete