Hostname is used to identify the Linux hosts. We generally prefer to put related host names to the systems like web1, db5 etc. In this tutorial we will look how to find and print hostname and change it with different methods. Following examples can be used for other Linux distributions like CentOS, Fedora, RedHat too.
Find Current Hostname
There are different ways to find hostname. But the most easy and stable way is printing related host name configuration. Host name configuration is stored in /etc/hostname
which is very easy to find. We will print this configuration file like below.
$ cat /etc/hostname

Find Current Hostname with hostnamectl Command
hostnamectl
is a new tool in order to manage host name configuration. We can print current hostname just providing hostnamectl
command like below. As we can more information can be found in the output like Operating system, machine UID etc.
$ hostnamectl

Change Hostname Temporary
There is command named hostname
which is used to change hostname temporarily. We can provide the name we want to set. But we should have root
privileges for host name change.
$ sudo hostname poftut2

Change Host name Permanently
We can change hostname permanatly too. Previous example will change hostname for current power up. After a reboot the host name will be reverted back. We will edit the hostname configuration file /etc/hostname
and put whatever we want.
$ sudo echo "poftut3" > /etc/hostname
Change Host name with hostnamectl
hostnamectl
command provides different options to list and set host name and related information. We will use set-hostname
option in order to change host name. We will set host name poftut4
like below.
$ sudo hostnamectl set-hostname poftut4