Changing Your Hostname in Linux

Posted by Sheldon Finlay on January 27, 2009

Here’s a quick and painless way to change your server’s hostname in linux without having to reboot the server. This should work for most flavors of linux, although I have only tested it on CentOS and RedHat. So you may have to adjust things according to your server’s configuration.

Step 1: Edit your network file.

cd /etc/sysconfig
vi network

Look for your current hostname to the right of HOSTNAME= and edit it to reflect your new hostname. When done, save the document.

Step 2: Edit the hosts file.

cd /etc
vi hostsĀ 

Again, you will want to edit out your old hostname and replace it with the new hostname.

Step 3: Use hostname program to set hostname.

hostname your-new-host-name

Step 4: Restart network services

service network restart

You should be all set. You can test your hostname by typing:

hostname

This should return the current hostname. You can do a further test by logging out of your current session and re-logging in. Your command prompt should reflect the new hostname:

[toor@rack10]