how to change or edit the hostname of your linux system

The hostname is the name set to identify your machine by other users (or yourself) in a human readable way. The name of a Linux machine is usually set during the installation process. Sometimes you want to change the name of the machine so that it best represents the function or purpose of the machine.

Before you go about changing it, it might be worthwhile to find out what it is currently set to.  You can find the current name of the machine by using the command hostname. This is also the command that can be used to change the name.

You have the option of changing the host name temporarily, meaning the change lasts only till the next reboot when it will revert back to the old name. You can also make this name change permanent by making it persistent across restarts. Depending on the type of Linux distro, you will also have to modify a couple of configuration files.

We will change the name first using the hostname command, which is not a persistent method but is important especially if you want the change to be effective without a reboot. Then we will modify the configuration files to make those changes permanent. For this example, let us assume that the old name is “Foo” and the new name to be “Bar“.

Using hostname command

First, print out the existing name:

bash$ hostname

prints out


Now, to change the name to Bar

bash$ hostname Bar

You can print out the name again using the hostname command to verify the change


Using sysctl command

There is another command that you can use in lieu of the hostname command. This involves changing the kernel hostname value using the sysctl. First print out the existing name,

bash$ sysctl kernel.hostname

prints out


Now change the name to Bar

bash$ sysctl kernel.hostname=Bar

Note: You need to use either the hostname command or the sysctl, not both.

Change host name using sysctl command in linux

Making the changes permanent

Next, you will need to change the specific configuration file where the name of the machine is specified. Though specific to the Linux distribution, they are usually found in some well known locations. Check if you can find them in one of the following directories/files

/etc/hostname (or /etc/HOSTNAME) : This is usually the location for Debian, Ubuntu, Slackware and Suse distributions and its variations.

/etc/sysconfig/network : This is the location for Redhat, Centos and Fedora distributions

/etc/conf.d/hostname : This is the location Gentoo distribution.

Open the file in a text editor and modify the contents. You just have to find the occurrences of your old machine name and change it to the new one. In this example, you will change all Foo to Bar. Leave the original format of the file as it is. For example, in the hostname file all you will find is the host name in single line, but in the network file (eg. Redhat) you will find other information and the hostname specified as Hostname=Foo.

Modify and save this file.

Another configuration file you need to modify is the /etc/hosts file, so that the new name now maps correctly to the IP address. Again, open the /etc/hosts file in a text editor and look for occurrences of the old name.  These may vary slightly between systems, but you should see entries that look something like this     localhost  Foo

and/or    Foo

and/or something like this.  Foo

Change all the occurrences of the old name (eg: Foo) to the new name (eg: Bar), leaving everything else in place. You are only changing the machine name, not the IP addresses so nothing else needs to change.

After you have made these changes, you can either restart your system to verify that the changes you have made is correct or you can wait for the next time you restart whenever that might be. Depending on the distribution, you can also restart the network interface for the new changes to take effect.  You will need to use one of the following

bash$ service network restart

The above command is for Redhat and its variants…

Other systems can use

bash$ /etc/init.d/network restart