Sub topic 6
What happens on your machine first
This chapter assumes you know what an IP address is, that a client connects to a server on a port, and that you can run commands in a terminal. It covers what happens before any DNS packet leaves your computer.
The application asks the operating system
Applications rarely speak DNS themselves. A browser or a script calls a system function, usually getaddrinfo, and passes it a name and a port. The operating system decides how to answer. That decision happens inside the stub resolver, a small resolver built into the OS or its C library. It does not walk the DNS hierarchy. It only sends the question to a configured recursive resolver and waits for the answer.
import socket
results = socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM)
for family, socktype, proto, canonname, sockaddr in results:
print(family.name, sockaddr[0])If the host has both IPv4 and IPv6 connectivity and the name has both record types, you see AF_INET and AF_INET6 lines. The application then chooses which address to try first.
The lookup order
On Linux, the file /etc/nsswitch.conf sets the order of sources. A typical line is:
hosts: files dnsfiles means the local hosts file, /etc/hosts on Linux and macOS. dns means a network query. The first source that answers wins.
A hosts entry maps an address to one or more names:
127.0.0.1 localhost
192.0.2.80 devbox.internalBecause files comes first, an entry here overrides whatever public DNS says. Developers use this to point a name at a test server. Stale entries are a common cause of "it works on my colleague's machine".
Search domains and the dot count
If the name is not in the hosts file, the stub resolver reads its settings. On many Linux systems they are in /etc/resolv.conf:
nameserver 192.0.2.1
search corp.example.com
options ndots:1nameserveris the recursive resolver to ask. Several lines give fallbacks.searchlists suffixes to append to short names.ndotsis the minimum number of dots a name needs before it is tried as written first. The default is 1.
If you look up db, it has fewer dots than ndots. The resolver tries db.corp.example.com first and then plain db. If you look up www.example.com, it has enough dots and is tried as written first. The search list is used only if that fails. A name with a trailing dot, such as db., is fully qualified. The search list is never applied to it.
Container platforms often set a high ndots, such as 5. Then even api.example.com is first tried with every search suffix. Each failed attempt is an extra query and extra latency.
Local caching
Some systems add a caching layer. On systemd-based Linux, systemd-resolved often listens on 127.0.0.53, so resolv.conf points at your own machine. Browsers may also keep their own cache. A name can therefore be answered without any network traffic.
Exercise
A machine has search corp.example.com and ndots:1 in resolv.conf. An application looks up intranet. What names does the stub resolver try, and in what order? Does the answer change for intranet.?
Show answer
intranet has no dots, which is fewer than ndots, so it tries intranet.corp.example.com first and then intranet. For intranet. the trailing dot makes it fully qualified, so only intranet at the root is queried and the search list is skipped.