Linux › Section I · Your network and its configuration › Topic I.8

Resolving hostnames and localhost

A network connection that cannot turn names into addresses is almost useless, and on Linux several files and services share that job. This topic follows a name lookup from a program through /etc/hosts, nsswitch.conf and resolv.conf to systemd-resolved, and explains the special addresses that always mean this machine.

How a program looks up a name

DNS itself, with its servers, records and caching, is taught in the Networking topic DNS: the internet's directory service. On Linux, the question is how a program gets an answer. 1. The program calls a C library function, usually getaddrinfo. 2. The library reads /etc/nsswitch.conf to decide which sources to try and in what order. 3. A typical first source is the file /etc/hosts. 4. If that has no entry, the library asks a DNS server, whose address it finds in /etc/resolv.conf, or passes the query to a local resolver service. 5. The answer goes back to the program. Every program that uses the library follows the same path.

/etc/hosts

/etc/hosts is a plain text file of manual overrides. Each line gives an address, a canonical name and optional aliases, separated by spaces: for example 192.168.1.50 nas.home.arpa nas. You will almost always find 127.0.0.1 localhost and an IPv6 line for ::1, and Debian-family systems add 127.0.1.1 followed by the machine's own hostname. The file is useful for a few hosts on a small private network and for early boot, before the network is up. Keep it short: if a name is in DNS, it should not also be in /etc/hosts, because a stale entry will silently override the real address later.

/etc/nsswitch.conf

The name service switch file sets the order of sources for many kinds of lookup, from user accounts to host names. The hosts line matters here. The classic form is hosts: files dns, meaning try /etc/hosts first, then DNS. Distributions add modules: myhostname always resolves the machine's own name, resolve sends queries to systemd-resolved, and mdns4_minimal handles .local names by multicast DNS. Bracketed actions change the flow. In resolve [!UNAVAIL=return], any answer from systemd-resolved, even not found, is final; only if the service is unavailable does the library carry on to the next source. When tracing an odd lookup, start here.

/etc/resolv.conf

The traditional DNS client file lists name servers and search rules. nameserver lines give up to three server addresses, tried in order. A search line, such as search home.arpa, lists domains to append to a short name: a lookup of printer is also tried as printer.home.arpa. Today the file is usually generated. On systems running systemd-resolved, /etc/resolv.conf is normally a symbolic link to /run/systemd/resolve/stub-resolv.conf, and its only nameserver is 127.0.0.53. ls -l /etc/resolv.conf shows whether it is a link. Editing a generated file by hand is pointless, because the next network change overwrites it.

systemd-resolved

Plain resolv.conf has two weaknesses: nothing on the host caches answers, and every interface must share one list of servers. systemd-resolved, standard on Ubuntu and Fedora among others, fixes both. It caches replies, keeps separate DNS servers and domains for each link, so a VPN can answer only for its own company domain, and supports the zero-configuration protocols multicast DNS (mDNS) and LLMNR for finding hosts on the local network. Programs reach it through the resolve module or through its stub listener at 127.0.0.53. Its settings live in /etc/systemd/resolved.conf. Other caching resolvers you may meet are dnsmasq and nscd.

resolvectl

resolvectl controls systemd-resolved; older releases called it systemd-resolve. resolvectl status, or plain resolvectl, shows the global settings and then each link's DNS servers, domains and protocols. resolvectl query www.example.org resolves a name through the service and reports where the answer came from, much as getent does. resolvectl dns enp3s0 192.0.2.53 sets a link's server and resolvectl domain enp3s0 home.arpa its search domain, until the network manager next reconfigures the link. resolvectl flush-caches empties the cache after a DNS change, and resolvectl statistics shows cache hits and misses.

Worked example: following a short name

A laptop's nsswitch.conf says hosts: files dns, its /etc/resolv.conf says nameserver 127.0.0.53 and search home.arpa, and /etc/hosts has no entry for printer. A user runs ping printer. 1. ping calls the library, which reads the hosts line: files first. 2. /etc/hosts has no printer, so it moves on to dns. 3. printer has no dot, so the search domain is appended, giving printer.home.arpa. 4. The query goes to 127.0.0.53, where systemd-resolved answers from cache or forwards it to the link's real server. 5. The address returns to ping. Check: getent hosts printer follows exactly these steps, while dig printer would not apply the search domain unless told to.

Worked example: reading resolvectl status

resolvectl status on a laptop shows, for Link 2 (enp3s0): Current Scopes: DNS, then DNS Servers: 192.168.1.1, then DNS Domain: home.arpa. It also shows Link 5 (wg0) with DNS Servers: 10.20.0.53 and DNS Domain: ~corp.example (illustrative). 1. enp3s0 carries ordinary lookups, sent to the home router at 192.168.1.1. 2. The tilde in ~corp.example marks a route-only domain: names under corp.example go to 10.20.0.53 on the VPN link, but the domain is not used as a search suffix. 3. A lookup of intranet.corp.example therefore goes to 10.20.0.53. 4. A lookup of www.example.org goes to 192.168.1.1. Check: resolvectl query intranet.corp.example would name link wg0.

Localhost and the loopback interface

The lo interface carries traffic from the machine to itself: the kernel takes outgoing datagrams for a loopback address and delivers them straight back as incoming ones, so a server listening locally can answer. lo holds 127.0.0.1/8, and every address beginning 127 is loopback, which lets services listen on separate loopback addresses; systemd-resolved uses 127.0.0.53 so it does not clash with another local DNS server on 127.0.0.1. IPv6 has one loopback address, ::1. The name localhost maps to 127.0.0.1 and ::1 through /etc/hosts, and systemd-resolved also answers for it itself.

Common mistakes to avoid

First, hand-editing a generated /etc/resolv.conf; change the network manager or resolved.conf instead. Second, filling /etc/hosts with names that DNS already holds, so stale entries override real addresses later. Third, expecting dig or host to show what applications see; they skip /etc/hosts and nsswitch.conf, so test with getent hosts or resolvectl query. Fourth, reading nameserver 127.0.0.53 as a broken setting; it is systemd-resolved's stub listener. Fifth, forgetting resolvectl flush-caches after moving a record during testing. Sixth, thinking only 127.0.0.1 is loopback; the whole 127.0.0.0/8 block is.

Key takeaways

Programs resolve names through the C library, which follows the hosts line in /etc/nsswitch.conf, typically /etc/hosts first and then DNS. /etc/hosts holds a few manual entries, including localhost. /etc/resolv.conf lists up to three nameservers and search domains, and is usually generated. systemd-resolved caches answers, keeps per-link servers and domains, supports mDNS and LLMNR, and listens on 127.0.0.53. resolvectl status, query, dns, domain, flush-caches and statistics manage it. lo carries 127.0.0.0/8 and ::1, so localhost always means this machine.

Flashcards

Flashcard

getaddrinfo

C library function programs call to turn a name into addresses

Follows /etc/nsswitch.conf.

Test your understanding

Question 1 of 5

A host's nsswitch.conf has hosts: files dns. /etc/hosts maps nas.home.arpa to 192.168.1.50, but DNS says 192.168.1.60. What does ping nas.home.arpa use?

Question 2 of 5

/etc/resolv.conf contains only nameserver 127.0.0.53. What does this usually mean?

Question 3 of 5

resolv.conf has search home.arpa. A user looks up the short name printer. Which name is queried?

Question 4 of 5

After a DNS record changes, a Linux laptop running systemd-resolved still gets the old address. Which command clears its local cache?

Question 5 of 5

Which statement about loopback addresses on Linux is correct?

← Previous topicNext topic →