Recently we got a request from one of our visitor to post article related to hostname change in Linux operating systems. I am going to cover this now.
There are two general way to do this
1) Temporary
2) Permanent
First we go and learn about how to check host name (system name) of the server.
Use "hostname" command to list the system system name.
[root@umser1 ~]# hostname
umser1.unixmantra.com
[root@umser1 ~]#
-s, --short short host name
-a, --alias alias names
-i, --ip-address addresses for the hostname
-I, --all-ip-addresses all addresses for the host
-f, --fqdn, --long long host name (FQDN)
-A, --all-fqdns all long host names (FQDNs)
-d, --domain DNS domain name
-y, --yp, --nis NIS/YP domainname
-F, --file read hostname or NIS domainname from given fil
In Cent OS we have another command additionally
[root@umser1 ~]# sysctl kernel.hostname
kernel.hostname = umser1.unixmantra.com
[root@umser1 ~]#
Change the hostname on a running system (Temporarily) :
This is pretty simple
#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.
How Do I Change Hostname Permanently?
For Debian 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
# /etc/hostname
umser2.unixmantra.com
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).
For Redhat/Fedora/Cent OS Systems:
As you know if you need changes to be permanent you need to hard-code the relevant configuration files.
To make the hostname name permanent in RH variants ,you must edit the /etc/sysconfig/network file to change "HOSTNAME" value to your new hostname.
#vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME="umser2.unixmantra.com"
GATEWAY="192.168.1.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"
Verification:
Open new session and there you go ,we can see our new hostname
[root@umser2 ~]# hostname
umser2.unixmantra.com
[root@umser2 ~]#