Change hostname in Linux


#1

Changing hostname in Linux based system requires root privileges.

If you want to change hostname in current session, you can use hostname command with the parameter of new hostname:

$ sudo hostname new-hostname

But most of the time you want to change hostname permanently. To do this, you have to enter new hostname into correct file which can be different for distributions.

For the Debian based distributions, you must change the contents of the /etc/hostname file with the new hostname:

$ cat /etc/hostname
SRV-DB-01

For the Redhat based distributions, you must change the HOSTNAME variable in /etc/sysconfig/network file:

$ cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME="SRV-DB-01"

Changes will be permanent after reboot.

If you change the hostname permanently, it is good to check /etc/hosts file against new hostname. Some programs wants to try to resolve ip address of your hostname. For this reason, installation programs of distributions puts an entry into /etc/hosts file like that:

127.0.1.1        SRV-DB-01

To make these types of programs happy and prevent ip lookup timeouts, you have to change the hostname entry in /etc/hosts too.