Menu
Configure client side DNS
“How Linux turns names into addresses: /etc/hosts, resolv.conf and nsswitch.conf, querying with host, dig and getent, and where systemd-resolved fits in.”
Almost everything you do on a network starts with a name: apt update contacts deb.debian.org, ssh goes to db01, a browser opens example.com. If name resolution breaks, the network looks dead even though every cable and route is fine. “It’s always DNS” is a joke among admins because it’s so often true.
This byte shows how a Linux client resolves names, which files control it, and how to query and debug DNS with host, dig and getent. For how DNS fits into a web request, see the request and response cycle.
How a lookup works#
When a program needs an address, it doesn’t talk to DNS itself. It calls the C library (glibc), and glibc follows a chain:
/etc/nsswitch.confsays which sources to try, in which order. Usually: files first, then DNS.- files means
/etc/hosts, a static list on this machine. - dns means: ask the name servers listed in
/etc/resolv.conf. - That name server (a recursive resolver: your router, your ISP,
1.1.1.1, or a local cache) finds the answer on the internet and sends it back.
Remember this chain. It explains almost every DNS mystery you’ll meet.
/etc/hosts: local names#
/etc/hosts maps addresses to names without asking anyone. One line per address: the IP, the canonical name, then optional aliases.
127.0.0.1 localhost
127.0.1.1 web01.example.lan web01
192.168.1.10 db01.example.lan db01 database
::1 localhost ip6-localhost ip6-loopbackIt’s great for a few fixed machines, for testing a site before its DNS is live, or for blocking a name by pointing it at 0.0.0.0. It doesn’t scale: every machine needs its own copy. Changes work immediately, no restart needed.
/etc/resolv.conf: which DNS servers#
/etc/resolv.conf tells the resolver where to send DNS queries:
nameserver 192.168.1.1
nameserver 9.9.9.9
search example.lan corp.example.lan
options timeout:2 attempts:3| Keyword | Meaning |
|---|---|
nameserver | IP address of a DNS server. Up to three are used, tried in order. |
search | domains to append to short names. ping db01 tries db01.example.lan, then db01.corp.example.lan. |
domain | the local domain, an older single-domain form of search. If both appear, the last one wins. |
options | timeout:n seconds per try (default 5), attempts:n tries per server (default 2), rotate to spread load over the servers, ndots:n (default 1). |
ndots:1 means a name with at least one dot is tried as-is first, before the search list.
Important
Before editing, check who owns the file: ls -l /etc/resolv.conf and read its first lines. If it’s a symlink into /run/ or says “Generated by NetworkManager”, your edits will be overwritten. Change DNS in that program instead (see below).
With NetworkManager, set DNS servers on the connection profile. ipv4.ignore-auto-dns yes stops the DHCP server’s DNS from being added:
sudo nmcli connection modify office ipv4.dns "1.1.1.1 9.9.9.9" ipv4.ignore-auto-dns yes
sudo nmcli connection up office/etc/nsswitch.conf: the order#
The Name Service Switch decides where glibc looks things up: users, groups, hosts, services and more. Each line is a database followed by its sources, tried left to right:
passwd: files systemd
group: files systemd
hosts: files dns
networks: files
services: files
protocols: filesFor names, only the hosts: line matters. files dns means /etc/hosts first, DNS second. To prefer DNS, swap them:
hosts: dns filesNo reboot or restart is needed. You’ll also meet other sources on that line:
myhostname: always resolves the machine’s own name (and_gatewayto the default gateway), from systemd.resolve: ask systemd-resolved directly.mdns4_minimal: multicast DNS for.localnames on the LAN (Avahi).[NOTFOUND=return]: an action. “If the previous source answered ’no such name’, stop here and don’t try the next one.”
A typical line on a system with systemd-resolved:
hosts: files myhostname resolve [!UNAVAIL=return] dns/etc/host.conf. It is older than nsswitch.conf and once set the lookup order with order hosts,bind. glibc no longer takes the lookup order from it (that is the job of nsswitch.conf) and only reads a few options, like multi on.getent: what programs really see#
getent queries the NSS databases exactly the way any program would. It follows nsswitch.conf, so it sees /etc/hosts and DNS alike. When an application can’t resolve a name, start here:
getent hosts db01192.168.1.10 db01.example.lan db01 databasegetent ahostsv4 example.com203.0.113.10 STREAM example.com
203.0.113.10 DGRAM
203.0.113.10 RAWgetent hosts returns one address, getent ahosts lists every address (IPv4 and IPv6) the way getaddrinfo() returns them, and ahostsv4 / ahostsv6 limit it to one family. If the name isn’t found, getent prints nothing and exits with status 2.
getent works for the other databases too: getent passwd amin, getent services ssh.
Querying DNS servers: host and dig#
host and dig are DNS tools. They read the nameserver lines in /etc/resolv.conf, but they ignore /etc/hosts and nsswitch.conf. They ask DNS and nothing else. Install them with sudo apt install bind9-dnsutils bind9-host on Debian or Ubuntu (older releases call it dnsutils), or sudo dnf install bind-utils on RHEL or Fedora.
The real addresses for example.com change over time, so your output will show different numbers than the samples here.
host: short answers#
host example.comexample.com has address 203.0.113.10
example.com has IPv6 address 2001:db8::10
example.com mail is handled by 0 .| Command | What it does |
|---|---|
host -t MX gmail.com | ask for one record type (A, AAAA, MX, NS, TXT, …) |
host 1.1.1.1 | reverse lookup: address to name |
host example.com 9.9.9.9 | ask a specific server instead of the ones in resolv.conf |
host -v example.com | verbose, dig-like output |
host 1.1.1.1
host no-such-name.example.com1.1.1.1.in-addr.arpa domain name pointer one.one.one.one.
Host no-such-name.example.com not found: 3(NXDOMAIN)dig: the full picture#
dig shows the whole DNS response. It’s the tool for real debugging:
dig example.com; <<>> DiG 9.18.28-1~deb12u2-Debian <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41623
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 3600 IN A 203.0.113.10
;; Query time: 24 msec
;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)
;; WHEN: Tue Dec 01 10:15:42 CET 2026
;; MSG SIZE rcvd: 56What matters:
status:NOERRORworked.NXDOMAINmeans the name doesn’t exist.SERVFAILmeans the server failed to get an answer (broken upstream or a DNSSEC problem).REFUSEDmeans that server won’t answer you.- ANSWER SECTION: the name, the TTL in seconds (how long it may be cached), the class
IN, the record type and the value. - SERVER: which name server actually answered. Very useful when you’re not sure which one you’re using.
The options you’ll use most:
dig +short example.com # only the answer
dig @1.1.1.1 example.com # ask a specific server
dig example.com MX # a record type (or: dig -t MX example.com)
dig -x 1.1.1.1 # reverse lookup (PTR)
dig +noall +answer example.com # just the answer section
dig +trace example.com # follow the chain from the root servers downUnlike the system resolver, dig doesn’t apply the search list: dig db01 asks for exactly db01.. Add +search if you want it.
The record types you should know:
| Type | Holds |
|---|---|
A | an IPv4 address |
AAAA | an IPv6 address |
CNAME | an alias: “this name is really that name” |
MX | the mail servers for a domain, with priorities (lower is preferred) |
NS | the name servers responsible for a domain |
PTR | reverse mapping, address to name |
TXT | free text, used for SPF, domain verification and more |
SOA | start of authority: serial number and timers of a zone |
nslookup is a third query tool from the same package. It’s older and still common, especially on Windows, but dig gives you more detail.systemd-resolved#
systemd-resolved is a local DNS service from systemd. It caches answers, keeps separate DNS servers per interface (handy with VPNs), and can do DNSSEC and DNS over TLS. Ubuntu and Fedora use it by default. On Debian 12 it’s a separate package (systemd-resolved), and RHEL 9 doesn’t use it by default: NetworkManager writes /etc/resolv.conf directly.
When it runs, programs still read /etc/resolv.conf, but that file is a symlink pointing at a stub resolver on 127.0.0.53:
ls -l /etc/resolv.conf
cat /etc/resolv.conflrwxrwxrwx 1 root root 39 Mar 4 2025 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search example.lanSo 127.0.0.53 is not your real DNS server. To see the real ones, use resolvectl:
resolvectl statusGlobal
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
Link 2 (enp0s3)
Current Scopes: DNS
Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.1.1
DNS Servers: 192.168.1.1
DNS Domain: example.lan| Command | What it does |
|---|---|
resolvectl query example.com | resolve a name through systemd-resolved |
resolvectl flush-caches | empty the cache (after a DNS change) |
resolvectl statistics | cache hits and misses |
resolvectl dns enp0s3 1.1.1.1 | set a DNS server on a link until the next reconnect |
Older systemd versions call the tool systemd-resolve (for example systemd-resolve --status). Permanent global settings go in /etc/systemd/resolved.conf, followed by sudo systemctl restart systemd-resolved:
1[Resolve]
2DNS=1.1.1.1 9.9.9.9
3FallbackDNS=8.8.8.8
4Domains=example.lanDebugging name resolution#
Work through it step by step:
- Is it really DNS?
ping 1.1.1.1works butping example.comfails: yes. If the address doesn’t work either, go back to network troubleshooting. - What do programs see?
getent hosts example.com. A wrong answer here but a right one fromdigpoints at/etc/hostsor thehosts:line in/etc/nsswitch.conf. - Which servers are used?
cat /etc/resolv.conf, andresolvectl statusif it says127.0.0.53. - What does DNS say?
dig example.comand read thestatus:.NXDOMAINis a typo or a missing record.SERVFAILis a server-side problem. - Is it the server?
dig @1.1.1.1 example.com. If another server answers fine, your configured resolver is the problem. - Can you reach the server at all?
;; connection timed out; no servers could be reachedmeans no answer on port 53. Check the route to it and the firewall (DNS uses UDP 53, and TCP 53 for large answers). - Is it an old answer? A changed record can live on in caches until its TTL runs out. Flush with
resolvectl flush-cachesand compare withdig @<authoritative server>.
Tip
Try the difference yourself: add 127.0.0.1 example.com to /etc/hosts, then compare getent hosts example.com with dig +short example.com. The first shows 127.0.0.1, the second the real address. Remove the line afterwards.
Exam checklist#
- Lookup chain: program → glibc →
/etc/nsswitch.conf(hosts:line) →/etc/hosts(files) and/or the servers in/etc/resolv.conf(dns). /etc/hosts:IP canonical-name aliases, used before DNS with the defaulthosts: files dns. Swap todns filesto change the order./etc/resolv.conf:nameserver(max 3),search(domains appended to short names),domain,options(timeout,attempts,rotate,ndots).getent hosts NAMEresolves like a normal program, through NSS.hostanddigquery DNS only and ignore/etc/hosts.dig @server name type,dig +short,dig -x IPfor reverse. Readstatus:(NOERROR,NXDOMAIN,SERVFAIL), the ANSWER section with TTL, and theSERVER:line.host name,host -t MX domain,host IP(reverse),host name server.- systemd-resolved: stub on
127.0.0.53,/etc/resolv.confis a symlink to/run/systemd/resolve/stub-resolv.conf, managed withresolvectl, configured in/etc/systemd/resolved.conf. - If
/etc/resolv.confis generated (NetworkManager, systemd-resolved, netplan), set DNS in that tool, not in the file.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-109): Configure client side DNS"
[main 99e8940] learn(lpic-109): Configure client side DNS
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: