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
localhost.localdomain

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

$ sysctl kernel.hostname
localhost.localdomain

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

$ cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain

Changing the hostname


Edit /etc/hosts file


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

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

Lets assume my machine IP is 111.232.196.12, and I need to assign it hostname my212.box21.com.

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

$ cat /etc/hosts
127.0.0.1         localhost.localdomain    localhost
::1               localhost6.localdomain6  localhost6
111.232.196.12    my212.box21.com          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.

NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=my212.box21.com

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

2 comments:

  • 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