How to Change Hostname in Linux

January 12, 2005
A hostname is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication such as the World Wide Web, e-mail or Usenet.

The following are the steps you need to execute to change the hostname of your device in Linux.

Find out your hostname

To find what is your current hostname, run the hostname command without any parameters.

$ hostname

Another way of finding the hostname of your machine is as follows -

$ sysctl kernel.hostname

Here is yet another way of determining the hostname of your device.

$ cat /etc/sysconfig/network

Changing the hostname

Edit /etc/hosts file

The following is the content of my /etc/hosts file.

$ cat /etc/hosts      localhost.localdomain    localhost
::1            localhost6.localdomain6  localhost6

Lets assume my machine IP is, and I need to assign it hostname

To do this, first I have to edit /etc/hosts and append a single line as follows.

$ cat /etc/hosts         localhost.localdomain    localhost
::1               localhost6.localdomain6  localhost6          my212

Next I have to make my changes permanent.

To change the hostname of your machine, you should log in as super user (root).

Make the changes permanent

Edit the file /etc/sysconfig/network and change the value of HOSTNAME variable as follows.


Now in the next reboot, my machine will reflect the new hostname.


  • Cool blog, found it on /. in yer sig.

    It is now in my bookmarks :)

  • rcjhawk

    One additional note: if you're running sendmail (please, no one start on me), edit the /etc/mail/local-host-names file to include the new name. This prevents sendmail from hanging up when the system's rebotted.

    -- mjm