Wednesday 16 April 2008

How to change the hostname of a Linux system

Change the hostname on a running
On any Linux system you can change its hostname with the command ‘hostname‘

#hostname NEW_NAME

will set the hostname of the system to NEW_NAME. This is active right away and will remain like that until the system will be rebooted (because at system boot it will set this from some particular file configurations - see bellow how to set this permanently). You will most probably need to exit the current shell in order to see the change in your shell prompt.

Permanent hostname change on Debian based systems
Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh

#vi /etc/hostname
NEW_NAME

So on a Debian based system we can edit the file /etc/hostname and change the name of the system and then run:

#/etc/init.d/hostname.sh start

to make the change active. The hostname saved in this file (/etc/hostname) will be preserved on system reboot (and will be set using the same script we used hostname.sh).

Permanent hostname change on RedHat based systems
RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit

#vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=”NEW_NAME”
GATEWAY=”192.168.0.1″
GATEWAYDEV=”eth0″
FORWARD_IPV4=”yes”

So in order to preserve your change on system reboot edit this file and enter the appropriate name using the HOSTNAME variable.

reference : http://www.ducea.com/2006/08/07/how-to-change-the-hostname-of-a-linux-system/

No comments: