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:


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:


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

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:  www.example.com localhost localhost.localdomain

You would then change it to be like so:  ftp.example.com localhost localhost.localdomain