Monday, 7 November 2011

How To Find And Change Hostname In Linux

Several times,we want to change the hostname we have set during installation. In most cases, we don't care hostname during installation and later we might need to change it to something mandatory. So I will discuss on how we can view the hostname and change it according to our wish.

To simply know the hostname, you can open the terminal(gnome-terminal for ubuntu) where you will find a entry which is something similar to samar@Techgaun: ~$. This entry is in the format user@hostname so in this example, samar is the user and Techgaun is the hostname. So this way you could simply know your hostname.

Also, there is a hostname command which also can be used to know the hostname. It can also be used to edit the hostname of your system.

To view hostname with this command, open terminal and type:

hostname

To view the FQDN, you can type:

hostname -f

Now to change the hostname, just type:

sudo hostname NEW_HOSTNAME

The above might need restart of shell(or simply do Ctrl + Shift + T) to see the effect.

Also, the file containing the host name is /etc/hostname so to change the hostname permanently, you can edit this file as well.

sudo gedit /etc/hostname

and then change the entry for host name as per your requirement. Then you need to reboot your system to see the effect or you could alternatively do:

sudo /etc/init.d/hostname stop

This will perfectly work for debian based systems. For the Redhat and similar variants, you need to edit /etc/sysconfig/network file and set its HOSTNAME field.

I hope this helps you. :)