Assigning Hostname to Linux System

Assigning Hostname to Linux System

This article provides the steps required to give a name to your linux installation. By default the hostname  is localhost, you can follow these steps to change it to some other name.

There are two ways to change the hostname of the machine

Temporary Hostname Change

  • Issuing command hostname

Changing the hostname of the linux/CENTOS machine, issue the following command

hostname master.cluster.com

Doing this will change the hostname to master.cluster.com but this change will be effective until the end of current session and would be lost once the machine is rebooted. 

Permanent Hostname Change

  • Adding an entry for hostname in /etc/hosts file

You can add an entry for hostname in the /etc/hosts file using the editor by issuing 

vi /etc/hosts

Contents of host file looks like below

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

You can add a line to the bottom to include the ip address and the hostname as shown by highlighted text

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.21 master.node.com                                                  

  • Changing the Hostname in /etc/sysconfig/network file

You should change the value of Hostname variable in /etc/sysconfig/network by editing the file using 

vi /etc/sysconfig/network

Open your /etc/sysconfig/network file, locate the HOSTNAME line and change the hostname to your preferred node name.

NETWORKING=yes                     

HOSTNAME=master.node.com           

.Now when you restart the machine it will have the hostname setup in the host file.