Learn what is hostname, how to set hostname and how to change hostname in Debian and RedHat based Linux systems.
The hostname is a prime identity of Linux servers in the human world! Obviously, IP address is the main component to identify the system in the environment. In this article, we are going to see anything and everything about hostname. We will walk through what is the hostname, how to set hostname, how to change hostname etc. Let’s start with basics of the hostname.
What is hostname
Hostname is the humanly readable identity of the server. Any server is identified by IP address in the network but to identify easily hostname is also given. Normally FQDN (Fully Qualified Domain Name) is expected for the system but even Domain name (name before the dot) is also fine for systems under private networks. Hostname can be alpha-numeric
Generally hostname standards to the maximum of 255 bytes long. But normally people prefer to keep it 10-12 characters long so that it’s easy to remember. Kernel variables
HOST_NAME_MAX defines your current max limit of hostname. You can get their values using
getconf command like below :
# getconf HOST_NAME_MAX
How to set hostname in Linux
A quick command in all new Linux distros is
set-hostname switch and your new hostname as an argument.
# hostnamectl set-hostname kerneltalks
For more details read on …
Hostname is defined in files
- /etc/hosts for networking
/etc/hostname: This will be read by boot scripts on boot time and set its value.
/proc/sys/kernel/hostname: Current hostname.
/etc/sysconfig/network: Networking (HOSTNAME=”server1″ parameter)
In above files, you can only view current hostname (being used by the live kernel) under proc file only. Rest all files are used to lookup or set hostname at boot time. So if you change hostname using hostname command then it won’t reflect in rest files. It will only reflect in proc file.
You can set hostname of your choice in
/etc/sysconfig/network and restart network service to notify kernel about it.
How to change hostname in Linux
The current hostname can be checked by typing
hostname command without any argument. The hostname can be changed by simply using
hostname command followed by name of your choice.
Cautions : Do not change hostname on live production systems!
# hostname kerneltalks.com
Please make a note that change is dynamic and not permanent. After system reboot, hostname will be return to what it was earlier.
Change hostname permanently in Linux
On RedHat systems : You can edit file
/etc/sysconfig/network (define in HOSTNAME=”xyz”) & reboot system
# cat /etc/sysconfig/network
On Debian systems : You can edit file
/etc/hostname & call
/etc/init.d/hostname.sh script (
You can even change hostname using system control command. Use parameter
kernel.hostname and define its value like below :
# sysctl kernel.hostname=kerneltalks
kernel.hostname = kerneltalks
On Suse systems : Edit file
/etc/HOSTNAME and add hostname in it. There will be no parameter and value format. Only you have to enter hostname like below :
# cat /etc/HOSTNAME
Change hostname permanently in clone, template VM & cloud clones
If you have system which is prepared using clone, template from vmware or cloud clone deploy then you should do following :
/etc/cloud/cloud.cfg and change parameter
'preserve_hostname' to true. You can do it using one line sed script as below :
root@kerneltalks # sed --in-place 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg
Also, change DHCP related parameter
DHCLIENT_SET_HOSTNAME in file
/etc/sysconfig/network/dhcp to no. So that hostname wont be changed by DHCP in next reboot. Again, you can use one line sed to do that as below :
root@kerneltalks # sed --in-place 's/DHCLIENT_SET_HOSTNAME="yes"/DHCLIENT_SET_HOSTNAME="no"/' /etc/sysconfig/network/dhcp
That’s it. These are two extra steps you need to take on cloud or VM servers.