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, the 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 the hostname. We will walk through what is the hostname, how to set hostname, how to change hostname etc. Let’s start with the basics of the hostname.
What is hostname
The 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 (the name before the dot) is also fine for systems under private networks. The 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 a command like below :
# getconf HOST_NAME_MAX 64
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 the proc file.
You can set the 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 the name of your choice.
Cautions : Do not change hostname on live production systems!
# hostname server5 # hostname kerneltalks.com # hostname kerneltalks.com
Please make a note that change is dynamic and not permanent. After the system reboot, the hostname will be returned 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 HOSTNAME=kerneltalks.com
On Debian systems : You can edit file
/etc/hostname & call
/etc/init.d/hostname.sh script (
You can even change the hostname using the 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 kerneltalks.com
Change hostname permanently in clone, template VM & cloud clones
If you have a system which is prepared using clone, template from VMware or cloud clone deploy then you should do the 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 the 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.
How to configure FQDN in Linux
Another thing around the hostname is to set FQDN for Linux server i.e. Fully Qualified Domain Name. Generally you should be doing in via DNS in your environment but
/etc/hosts always get checked first. So its good practise to define FQDN at
Use <IP> <FQDN> <Hostname> format to add/edit entry in
/etc/hosts and you are good to go. Sample entry below –
root@testsrv1 # echo "10.1.1.5 testsrv1.kerneltalks.com testsrv1">>/etc/hosts
You can verify Linux server’s FQDN by using command
root@testsrv1 # hostname -f testsrv1.kerneltalks.com