Hostname is used to name the systems. Normally the computer networks work with IP addresses in order to identify and communicate. But hostname provides human friendly way to name, identify, communicate with other systems.
Example Hostname
Hostname is just a meaningful word for the system administrators to name a system. For example we can use db1
for first database server or app3
for the third application server.
Default Hostname
Hostnames generally set during the system installation. But may change after the installation. Different Linux distributions uses different default hostnames during installation. Here are some of them.
Debian
debian
Ubuntu:
ubuntu
Fedora:
localhost
CentOS:
localhost
Print Hostname with hostname Command
There are different ways to print default hostname of the system. hostname
command is the most known and popular way in order to print hostname of the current system. This command will work on all Linux distributions like Debian, Ubuntu, Mint, Kali, CentOS, RedHat, Fedora etc.
$ hostname

As we can see the hostname of the given system is poftut1
Print Hostname with hostnamectl Command
There is a new command which is supported by all popular Linux distributions like Debian, Ubuntu, Mint, Kali CentOS, RedHat, Fedora. We can use hostnamectl
command which will provide the hostname of the current system and more like Chassis
, Operating System
etc.
$ hostnamectl

Change Hostname For Ubuntu, Debian, Mint, Kali
Now we will start to learn how to change hostname of the deb
based Linux distributions like Ubuntu, Debian, Mint, Kali. These distributions stored hostname settings in /etc/hostname
file. So changing this file will change hostname but this will require a reboot to make changes affect.
In this example we will change hostname to the POF1
. We will also use sudo
because hostname change operation requires root
privileges.
$ sudo echo "POF1" > /etc/hostname
Change Hostname For CentOS, RedHat
Hostname configuration of rpm
based disributions like CentOs, RedHat, Fedora are stored in /etc/sysconfig/network
. HOSTNAME
line provides the hostname of the current system. Example configuration file is like below.
NETWORKING=yes HOSTNAME="poftut1.domainname.com" GATEWAY="192.168.1.1" GATEWAYDEV="eth0" FORWARD_IPV4="yes"
Just put the hostname you want to set to the poftut
. But there is also domain name part which is domainname.com
. We can set as default or change according to our settings. But if we do not need we can set whatever we want.
In order to make change affect we need to restart network services with the following command.
$ sudo systemctl restart network
Change Hostname with sysctl Command For Ubuntu, Debian, Mint, Kali, CentOS, RedHat
sysctl
is a command use to configuration low level settings of a Linux system and kernel. We can use it to set hostname. In this example we will set as poftut1
.
$ sudo sysctl kernel.hostname="poftut1"
Change Hostname with hostnamectl Command For Ubuntu, Debian, Mint, Kali, CentOS, RedHat
We can also use hostnamectl
command in order to change current hostname configuration. In this case we will use set-hostname
parameter. In this example we will set the hostname as poftut1
$ sudo hostnamectl set-hostname "poftut1"