Home / Changing the hostname on CentOS

Changing the hostname on CentOS

If the hostname setting is incorrect on your CentOS/RedHat/Fedora machine, it’s really easy to change the hostname from the command line. There are also GUI tools for doing this but we’ll just look at the CLI tools for doing this.

The file /etc/sysconfig/network contains the hostname and will look something like this:

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

Simply open up the file in your favourite text editor, either as root or using sudo, and change the HOSTNAME value to what you want it to. For example, if we wanted to change www.example.com from the above example to ftp.example.com then you’d end up with the following:

NETWORKING="yes"
GATEWAY="10.1.1.1"
HOSTNAME="ftp.example.com"

This change won’t take affect until the next reboot, but you can make the change happen immediately using the hostname command like so:

$ hostname ftp.example.com

Simply issuing the command on its own will return the current hostname, eg

$ hostname
ftp.example.com

You may also need to add/change the hostname in the /etc/hosts file. By default this would look something like this, using our www.example.com example again:

127.0.0.1  www.example.com localhost localhost.localdomain

You would then change it to be like so:

127.0.0.1  ftp.example.com localhost localhost.localdomain