TExcelTAD
Home › Linux Server Administration › DNS from the Server

DNS from the Server

DNS explained how resolution works across the internet. On a server the questions are narrower and more urgent: what does this machine resolve that name to, which resolver did it ask, and is the answer cached? Half of all "the app can't reach the database" incidents turn out to be a DNS answer nobody expected.

How a server resolves a name

An application calls the C library (getaddrinfo), which follows /etc/nsswitch.conf:

text
hosts: files dns          ← first /etc/hosts, then DNS

files means /etc/hosts, which wins over DNS. "DNS" means the servers in /etc/resolv.conf; on Ubuntu that is nameserver 127.0.0.53, the local systemd-resolved stub, which forwards to the real upstream resolvers (from DHCP or netplan) and caches answers.

Tool Asks Use it to
getent hosts name / getent ahosts name the C library, exactly like the app see what the app will get, including /etc/hosts
dig name a DNS server directly (by default the one in resolv.conf) inspect DNS answers, TTLs, record types
dig @10.0.0.2 name a specific server compare resolvers, bypass caches
resolvectl query name, resolvectl status systemd-resolved see which upstream server and interface was used
nslookup name a DNS server, older output format quick checks, also on Windows

The difference between getent and dig explains many puzzles: dig ignores /etc/hosts, so a stale /etc/hosts line makes the app connect to an old address while dig shows the right one.

bash · try it yourself
grep '^hosts:' /etc/nsswitch.conf
getent hosts localhost
getent ahosts localhost | awk '{print $1}' | sort -u
hosts:          files dns
::1             localhost ip6-localhost ip6-loopback
127.0.0.1
::1

getent hosts returns one answer (here the IPv6 ::1 line of this container's /etc/hosts), while getent ahosts lists every address an application may try, IPv6 and IPv4. An app that tries ::1 first and finds nothing listening there must fall back to 127.0.0.1; most do, some report the first failure.

A DNS server to practise on

The sandbox has no internet, so the session runs its own small DNS server: dnsmasq, answering for the zone example.test on port 5353 (the standard port 53 needs root). local=/example.test/ makes it authoritative for that zone, so unknown names get a proper NXDOMAIN:

bash · try it yourself
cat > dns.conf <<'EOF'
local=/example.test/
host-record=app.example.test,10.0.0.20
host-record=db.example.test,10.0.0.30
cname=www.example.test,app.example.test
mx-host=example.test,mail.example.test,10
txt-record=example.test,"v=spf1 mx -all"
srv-host=_postgresql._tcp.example.test,db.example.test,5432,0,100
local-ttl=300
EOF
dnsmasq --conf-file=dns.conf --port 5353 --listen-address=127.0.0.1 --bind-interfaces \
        --no-resolv --no-hosts --user=learner --pid-file="$PWD/dnsmasq.pid"
q() { dig @127.0.0.1 -p 5353 +short "$@"; }
echo "A:     $(q app.example.test)"
echo "CNAME: $(q www.example.test | tr '\n' ' ')"
echo "PTR:   $(q -x 10.0.0.30)"
echo "MX:    $(q MX example.test)"
echo "TXT:   $(q TXT example.test)"
echo "SRV:   $(q SRV _postgresql._tcp.example.test)"
echo "--- a full answer, with TTL:"
dig @127.0.0.1 -p 5353 www.example.test +noall +answer
echo "--- a name that doesn't exist:"
dig @127.0.0.1 -p 5353 nope.example.test +noall +comments | grep -o 'status: [A-Z]*'
kill "$(cat dnsmasq.pid)"
A:     10.0.0.20
CNAME: app.example.test. 10.0.0.20
PTR:   db.example.test.
MX:    10 mail.example.test.
TXT:   "v=spf1 mx -all"
SRV:   0 100 5432 db.example.test.
--- a full answer, with TTL:
www.example.test.	300	IN	CNAME	app.example.test.
app.example.test.	300	IN	A	10.0.0.20
--- a name that doesn't exist:
status: NXDOMAIN

+short prints only the data, +noall +answer the answer section with TTLs (the second column), +noall +comments the header, where status: lives. The statuses are the first thing to read:

Status Means Typical cause
NOERROR with answers fine –
NOERROR with no answer the name exists, but not with that record type asking for AAAA where only A exists
NXDOMAIN the name doesn't exist a typo, a missing record, a wrong search domain
SERVFAIL the resolver failed to get an answer broken DNSSEC, unreachable authoritative servers
REFUSED the server won't answer you asking a server that doesn't serve that zone or doesn't allow recursion for you

Caching and TTLs: why changes "don't work"

Every answer carries a TTL (300 seconds above), and every resolver on the path caches it for that long: systemd-resolved on the server, the company resolver, sometimes the application itself (the JVM caches DNS, some HTTP clients pool connections for hours). After changing a record:

bash
# Sample session: checking a change at its source and in the caches
$ dig +short @ns1.example.com app.example.com          # the authoritative server: the new address?
10.0.0.21
$ dig +noall +answer app.example.com                    # the local resolver: cached old answer, TTL counting down
app.example.com.        212     IN      A       10.0.0.20
$ resolvectl flush-caches                               # clear systemd-resolved's cache (root)
$ resolvectl query app.example.com
app.example.com: 10.0.0.21                             -- link: ens5

-- Information acquired via protocol DNS in 18.4ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network

Lower the TTL (to 60 s, say) a day before a planned migration, so the old value expires quickly when you switch.

Following the delegation

dig +trace resolves from the root servers down, ignoring caches, and shows which server gave which referral. It is the tool for "the new domain doesn't resolve" (sample session; needs the internet):

bash
$ dig +trace +nodnssec shop.example.org | grep -E 'IN\s+(NS|A)\s' | head -6
.                       518400  IN      NS      a.root-servers.net.
org.                    172800  IN      NS      a0.org.afilias-nst.info.
example.org.            86400   IN      NS      ns1.dnshost.example.
example.org.            86400   IN      NS      ns2.dnshost.example.
shop.example.org.       300     IN      A       203.0.113.40

If the trace stops at the org. servers with no NS for your domain, the delegation at the registrar is wrong, not your zone.

Test yourself

dig app.internal shows the new address, but the application still connects to the old one. What should you check first?

  1. The firewall
  2. getent hosts app.internal, which also reads /etc/hosts, and the application's own DNS caching
  3. The TTL on the root servers
  4. The MX record

What does NXDOMAIN mean?

  1. The server is down
  2. The queried name does not exist
  3. The name exists but has no record of this type
  4. The answer is not authenticated

Why lower a record's TTL a day before migrating a service to a new IP?

  1. To speed up DNS queries
  2. So caches expire the old address within the short TTL when you switch, instead of keeping it for the old, long TTL
  3. To make DNSSEC work
  4. TTLs don't matter