Device or system hostnames are used to easily recognize a machine within a network in a human readable format. It is not much of a surprise, but on Linux system, the hostname can be easily changed by using simple command as “hostname“.
Running hostname on its own, without any parameters, will return the current hostname of your Linux system like this:
If you want to change or set hostname of your Linux system, simply run:
$ hostname NEW_HOSTNAME
Of course, you will need to replace “NEW_HOSTNAME” with the actual hostname that you wish to set. This will change the hostname of your system immediately, but there is one problem – the original hostname will be restored upon next reboot.
There is another way to change the hostname of your system – permanently. You might have already figured it out that this will require change in some configuration files and you will be correct.
Set System Hostname Permanently in Linux
Newer version of different Linux distributions such as latest Ubuntu, Debian, CentOS, Fedora, RedHat, etc. comes with systemd, a system and service manager that provides a hostnamectl command to manage hostnames in Linux.
To set system hostname on SystemD based distributions, we will use hostnamectl command as shown:
[php]$ sudo hostnamectl set-hostname NEW_HOSTNAME[/php]
For Older Linux distributions, which uses SysVinit in short init, can have their hostnames changed by simply editing the hostname file located in:
[php]# vi /etc/hostname[/php]
You then have to add another record for the hostname in:
[php]# vi /etc/hosts[/php]
You then need to run:
[php]# /etc/init.d/hostname restart[/php]
On RHEL/CentOS based systems that use init, the hostname is changed by modifying:
[php]# vi /etc/sysconfig/network[/php]
Here is a sample of that file:
To keep a permanent hostname change the value next to “HOSTNAME” to the one of your hostname.
This simple article meant to show you a simple Linux trick and I hope that you learned something new.