Sub topic 8
How the System Turns a Name into an Address
Assume you can use a Linux shell, read configuration files, and run commands with sudo. You should also know the difference between an IPv4 address, a subnet, and a DNS server.
When an application connects to web01.example.com, it usually needs an IP address. Linux does not send every lookup directly to DNS. Instead, it follows a name service process controlled by /etc/nsswitch.conf.
The name resolution path
The hosts line in /etc/nsswitch.conf defines the order of lookup sources. A common entry is:
hosts: files mdns4_minimal [NOTFOUND=return] dnsThe important entries are:
filesmeans/etc/hosts.dnsmeans DNS servers configured for the system.mdns4_minimalsupports multicast DNS, often used for local names ending in.local.
For a normal name, Linux checks /etc/hosts first. If it finds a matching entry, it returns that address and normally does not contact DNS. If the file has no match, the resolver queries the configured DNS servers.
The name service switch is a library interface. Programs normally call functions such as getaddrinfo() rather than reading /etc/hosts or /etc/resolv.conf themselves. This gives different applications a consistent lookup process.
`/etc/hosts` and local names
/etc/hosts is a local table of addresses and names. Each entry contains an address followed by one or more names:
127.0.0.1 localhost
192.168.1.50 web01.example.com web01The fully qualified domain name is the complete name, such as web01.example.com. The short name is web01. Keeping both names on the same line lets local programs resolve either form.
The file is useful for the server's own name and for a small number of stable local mappings. It is not a replacement for DNS across a large network. An incorrect entry can override a correct DNS record when files appears before dns.
You can inspect the active order with:
grep '^[[:space:]]*hosts:' /etc/nsswitch.conf
getent hosts web01.example.comgetent uses the configured name service switch. It is therefore a better test of what most applications see than reading /etc/hosts directly.
The resolver and DNS configuration
When the name service switch selects DNS, the system resolver reads its resolver configuration. On many modern distributions, /etc/resolv.conf is managed by systemd-resolved, NetworkManager, or another service. It may be a symbolic link to a generated file.
Typical contents look like this:
search example.com
nameserver 192.168.1.1
nameserver 9.9.9.9A nameserver line gives a DNS server address. The resolver tries the listed servers according to its implementation and current state. A search domain allows a short name such as db01 to be expanded to db01.example.com during suitable queries.
Inspect both the resolver service and the file that applications may read:
resolvectl status
ls -l /etc/resolv.conf
cat /etc/resolv.confresolvectl status shows per-interface DNS settings when systemd-resolved is active. Do not edit a generated /etc/resolv.conf by hand. Configure DNS through the network manager that owns the interface, or your changes may disappear when the network restarts.
Common lookup failures
Use the failure location to narrow the cause:
getent hosts namefails, but an address works: check/etc/nsswitch.conf,/etc/hosts, DNS server addresses, and search domains.- A fully qualified name works, but a short name fails: check the
searchsetting and the application's use of it. resolvectl query namefails for every name: check resolver status, routes to DNS servers, and firewall rules.- One application fails while
getentworks: that application may use its own resolver, cache, or configuration. - An old address appears: inspect
/etc/hosts, DNS records, and local caches before changing network settings.
Exercise
The command getent hosts app.example.com returns an address, but resolvectl query app.example.com fails. What should you check first?
Show answer
Check /etc/hosts and the hosts order in /etc/nsswitch.conf. getent may be succeeding from the local hosts file without using DNS.