Chapter 12: Networking

Host names and name resolution

People use names; networks use addresses. Something has to translate, and when it goes wrong the symptom is confusing: the server is up, you can ping its address, but "the host cannot be found". This lesson covers the host's own name, the three places a name can be resolved, and the order in which they are tried.

The host's own name

A machine has a short name (servera) and a fully qualified domain name, FQDN (servera.lab.example.com): the short name followed by its DNS domain. RHEL stores the name in /etc/hostname and you manage it with hostnamectl:

[root@servera ~]# hostnamectl --static
servera.lab.example.com
[root@servera ~]# cat /etc/hostname
servera.lab.example.com
[root@servera ~]# hostname -s; hostname -f
servera
servera.lab.example.com
[root@servera ~]# hostnamectl set-hostname alpha.lab.example.com
[root@servera ~]# hostname
alpha.lab.example.com

hostnamectl set-hostname writes /etc/hostname and applies the name at once (open a new login to see it in your prompt). It changes the machine's own idea of its name. It does not create a DNS record: other machines find a host by name only if the DNS server (or their /etc/hosts) knows it.

Three pieces of configuration

How a name becomes an address: Step through what happens when a program asks for serverb.

FileRole
/etc/nsswitch.confThe order of name sources. The line to know: hosts: files dns myhostname
/etc/hostsA local table of address-to-name lines, searched first
/etc/resolv.confThe DNS servers (nameserver) and the search domain (search), written by NetworkManager
[root@servera ~]# grep ^hosts /etc/nsswitch.conf
hosts:      files dns myhostname
[root@servera ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search lab.example.com
nameserver 172.25.250.254
[root@servera ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

Do not edit /etc/resolv.conf by hand: NetworkManager rewrites it. To change DNS servers permanently, change the connection (nmcli connection modify NAME ipv4.dns ADDRESS, then connection up), as the previous lesson showed.

The search domain is added to short names. Asking for serverb tries serverb.lab.example.com.

Testing a name correctly

Different tools look in different places, which is why they can disagree:

CommandAsks
getent hosts NAMEThe system resolver, exactly as programs do: /etc/hosts first, then DNS (follows nsswitch.conf)
dig NAME, dig +short NAMEDNS only, and shows which server answered
host NAMEDNS only, in a short form
ping NAMEThe system resolver, then the network
[root@servera ~]# getent hosts serverb
172.25.250.11   serverb.lab.example.com
[root@servera ~]# dig +short serverb.lab.example.com
172.25.250.11
[root@servera ~]# host serverb
serverb.lab.example.com has address 172.25.250.11

When a local entry overrides DNS, you see the difference immediately:

[root@servera ~]# echo "10.1.1.1 serverb" >> /etc/hosts
[root@servera ~]# getent hosts serverb
10.1.1.1        serverb
[root@servera ~]# dig +short serverb.lab.example.com
172.25.250.11

getent (and so every program) now goes to 10.1.1.1, while DNS still says .11. A forgotten line in /etc/hosts is a classic cause of "it works on that machine but not on this one". dig -x ADDRESS does the reverse lookup, from address to name.

Try it

Check your understanding