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.
- Show and set the host name with
hostnamectl, and explain the fully qualified name. - Explain
/etc/hosts,/etc/resolv.confand/etc/nsswitch.conf, and the order they are used in. - Test resolution correctly with
getent,digandhost.
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.comhostnamectl 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.
| File | Role |
|---|---|
/etc/nsswitch.conf | The order of name sources. The line to know: hosts: files dns myhostname |
/etc/hosts | A local table of address-to-name lines, searched first |
/etc/resolv.conf | The 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.localdomain6Do 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:
| Command | Asks |
|---|---|
getent hosts NAME | The system resolver, exactly as programs do: /etc/hosts first, then DNS (follows nsswitch.conf) |
dig NAME, dig +short NAME | DNS only, and shows which server answered |
host NAME | DNS only, in a short form |
ping NAME | The 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.11When 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.11getent (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
- A name resolves differently with dig and with getent. Which one shows what programs will see?
- Where do you set the host name permanently?
- /etc/hosts and DNS disagree about a name. Which wins on a default RHEL?
- Which file lists the DNS servers?
- What does the search domain do?
- Which command shows the A record only, briefly?