How to change the hostname of a Linux system
Change the hostname on a running system
On any Linux system you can change its hostname with the command “hostname” (surprised?)
Here are some quick usages of the command line hostname:
Without any parameter it will output the current hostname of the system.
# hostname
This will output the fully qualified domain name (or FQDN) of the system.
# hostname --fqd
This will set the hostname of the system to <NEW_HOSTNAME>.
# hostname <NEW_HOSTNAME>
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 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“
Just edit the this file: “/etc/sysconfig/network” and enter the appropriate name using the HOSTNAME variable.
NETWORKING=yes
HOSTNAME=”rhserver”
GATEWAY=”192.168.0.1″
GATEWAYDEV=”eth0″
FORWARD_IPV4=”yes”
Permanent hostname change on Debian based systems
Debian based systems use the file “/etc/hostname” to read the saved hostname at system boot.
This is set using the init script “/etc/init.d/hostname.sh”
Just edit this file: “/etc/hostname” and enter the appropriate name.
dblinux
To make the change active you can run:
/etc/init.d/hostname.sh start
The hostname saved in this file, will be preserved on system reboot (and will be set using the same script we used hostname.sh).
Use sysctl to change the hostname
You can also change the hostname using sysctl command, it is on kernel.hostname variable.
There is no reason to do it this way, just another possibility for you to know.
To read the current hostname:
sysctl kernel.hostname
To change it, run:
sysctl kernel.hostname=<NEW_HOSTNAME>

