hostname in CentOS

Introduction

A hostname is a unique label that identifies a machine on the network.

There must be a mapping table between the hostname and IP address this is needed for packet routing.

A host may have several names, but the preferred option is when there is only one name.

There are three classes of hostname: static, pretty and transient.

It is recommended to use a fully qualified domain name (FQDN) for both the static and the transient names, such as host.example.com .

Since CentOS 7 supports only Fully Qualified Domain Names (FQDNs), I advise you to carefully check the hostname you plan to use.

Valid values:

Find your hostname

There are several ways to find your hostname.

Often it is enough just to look at the terminal

An example of my terminal

[andrei@localhost Downloads]$

andrei - this is the user name

localhost - this is the hostname

Downloads - the name of the current directory

You can see the prompt format by running

echo $PS1

[\u@\h \W]\$

u - user

h - host

W - Working Directory

Other ways

hostname

localhost.localdomain

hostname -f

localhost

uname -n

localhost.localdomain

hostnamectl

Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: 35e254eda578c54084b96e06d5f285cf Boot ID: afb44fef8d764f30bb89550849b02fde Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.36.2.el7.x86_64

Change the hostname

With the hostname command

hostname dns-server
hostname

dns-server

If you open the terminal again, the changes will be visible in the command line.

[andrei@dns-server~]$

This hostname will persist only until the system is rebooted

If you run hostnamectl you can see that only the dynamic name of the host has changed

hostnamectl

Static hostname: localhost.localdomain Transient hostname: dns-server Icon name: computer-vm Chassis: vm Machine ID: cff8a80b9c356243b5238452511a8ade Boot ID: 64d9ddab85ec4c219ac46c6a2940e628 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.42.2.el7.x86_64 Architecture: x86-64

If you examine the file hostname , you can also see that nothing has changed.

cat /etc/hostname

localhost.localdomain

With hostnamectl

hostnamectl set-hostname dns-server.andrei.com
hostname

dns-server.andrei.com

This is a permanent change. That is, the new hostname has the persistent class.

cat /etc/hostname

dns-server.andrei.com

pretty name

To set a pretty name you need to use quotes

hostnamectl set-hostname "andrei's.host.com"
hostnamectl

Static hostname: andreis.host.com Pretty hostname: andrei's.host.com Icon name: computer-vm Chassis: vm Machine ID: cff8a80b9c356243b5238452511a8ade Boot ID: 64d9ddab85ec4c219ac46c6a2940e628 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.42.2.el7.x86_64 Architecture: x86-64

A pretty hostname has appeared, while the static hostname is the same , but without the forbidden characters

In /etc/hostname displays only the static hostname

cat /etc/hostname

andreis.host.com

the pretty hostname can be seen in the file /etc/machine-info

cat /etc/machine-info

PRETTY_HOSTNAME="andrei's.host.com"

hosts

You can examine information about the names of other hosts in the file hosts

In my example there are two hosts on the network:

dns-server.andrei.com with the IP address 192.168.56.149

host113.andrei.com     with the IP address 192.168.56.113

Let's connect to it using ssh and see whether it has records about the names of other hosts on the network.

For clarity, from now on I will show the terminal prompt so that the hostname is visible.

[andrei@dns-server~]$ ssh andrei@192.168.56.113

Let's run ping by IP

[andrei@host113 ~]$ ping 192.168.56.149

PING 192.168.56.149 (192.168.56.149) 56(84) bytes of data. 64 bytes from 192.168.56.149: icmp_seq=1 ttl=64 time=0.650 ms 64 bytes from 192.168.56.149: icmp_seq=2 ttl=64 time=0.731 ms ^C --- 192.168.56.149 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.650/0.690/0.731/0.048 ms

It pings successfully by IP

And ping by hostname

[andrei@host113 ~]$ ping dns-server.andrei.com

ping: dns-server.andrei.com: Name or service not known

host113 does not know how to resolve the name dns-server.andrei.com

This is not surprising - after all, in the file hosts there are no records about it. Let's check the contents:

[andrei@host113 ~]$ cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

Let's add a record for dns-server.andrei.com

sudo vi !$

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.56.149 dns-server.andrei.com

Let's repeat ping

[andrei@host113 ~]$ ping dns-server.andrei.com

PING dns-server.andrei.com (192.168.56.149) 56(84) bytes of data. 64 bytes from dns-server.andrei.com (192.168.56.149): icmp_seq=1 ttl=64 time=0.698 ms 64 bytes from dns-server.andrei.com (192.168.56.149): icmp_seq=2 ttl=64 time=0.735 ms 64 bytes from dns-server.andrei.com (192.168.56.149): icmp_seq=3 ttl=64 time=0.739 ms ^C --- dns-server.andrei.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 0.698/0.724/0.739/0.018 ms

Now everything works

alias

To avoid writing long hostnames, you can come up with an alias

The format is as follows

IP FQDN ALIAS

FQDN - Fully Qualified Domain Name - the fully qualified name of a domain.

192.168.56.149 dns-server.andrei.com vbdns

[andrei@host113 ~]$ ping vbdns

PING dns-server.andrei.com (192.168.56.149) 56(84) bytes of data. 64 bytes from dns-server.andrei.com (192.168.56.149): icmp_seq=1 ttl=64 time=0.619 ms 64 bytes from dns-server.andrei.com (192.168.56.149): icmp_seq=2 ttl=64 time=0.674 ms ^C --- dns-server.andrei.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.619/0.646/0.674/0.037 ms

Pay attention to dns-server.andrei.com

Although the ping was by the alias, the reply comes as from the static fqdn

If you change the order so that vbdns comes first and the FQDN dns-server.andrei.com second, then dns-server.andrei.com will be considered an alias

192.168.56.149 vbdns dns-server.andrei.com

In that case the reply will come from vbdns

[andrei@host113 ~]$ ping vbdns

Or

[andrei@host113 ~]$ ping dns-server.andrei.com

PING vbdns (192.168.56.149) 56(84) bytes of data. 64 bytes from vbdns (192.168.56.149): icmp_seq=1 ttl=64 time=0.721 ms 64 bytes from vbdns (192.168.56.149): icmp_seq=2 ttl=64 time=1.01 ms ^C --- vbdns ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1004ms rtt min/avg/max/mdev = 0.721/0.866/1.012/0.148 ms

This can become important when working with certificates

Article author: Andrei Olegovich