Where to start First steps track · 15 stages
Linux Labs

DNS and name resolution

5 min Ubuntu 24.04 · systemd-resolved

How a name becomes an IP — resolv.conf, systemd-resolved, /etc/hosts, dig.

Cheat sheet

7 commands Printable version
  • getent hosts example.com how the system actually resolves it (for apps)
  • resolvectl status DNS servers and search domains
  • dig +short example.com a quick answer (IP only)
  • dig example.com MX a specific record type
  • dig @8.8.8.8 example.com check via a public DNS server
  • dig +trace example.com where delegation breaks
  • resolvectl flush-caches clear the cache
  • local overrides go in /etc/hosts; permanent ones in netplan's nameservers

Overview

To reach example.com, the system turns the name into an IP. The order of sources is set by NSS (/etc/nsswitch.conf): usually /etc/hosts first, then DNS.

On Ubuntu, DNS resolution is handled by systemd-resolved, and /etc/resolv.conf is a symlink to its stub (127.0.0.53).

bash
getent hosts example.com          # how the system ACTUALLY resolves it (hosts+DNS+NSS)
resolvectl status                 # current DNS servers and search domains per interface
resolvectl query example.com      # a query through systemd-resolved (as an app would see it)

Important: dig/nslookup go directly to DNS, while applications resolve through NSS. So for "what the program sees", use getent hosts/resolvectl query; use dig to check the DNS server itself.

Options

/etc/hosts — local overrides (usually take priority over DNS):

127.0.0.1     localhost
192.168.1.10  myserver myserver.local
bash
getent hosts myserver             # check the system sees the override
resolvectl flush-caches           # clear systemd-resolved's cache
resolvectl statistics             # cache hits/misses

Recipes

1

dig / host / nslookup

bash
dig example.com                   # a detailed DNS query (package dnsutils/bind9-dnsutils)
dig +short example.com            # answer only (IP) — handy in scripts
dig +short example.com AAAA       # the IPv6 address
dig example.com MX                # a record type (A, AAAA, MX, TXT, NS, SOA, CNAME…)
dig @8.8.8.8 example.com          # ask a SPECIFIC server (bypassing the local one)
dig +trace example.com           # walk delegation from the root (where it breaks)
dig -x 1.2.3.4                    # a reverse lookup (PTR)
dig +noall +answer example.com   # only the answer section
host example.com                  # short and simple
host -t MX example.com           # a record type
nslookup example.com             # the classic (also interactive)

Troubleshooting

5

IPs ping, but names don't

DNS isn't working. Work down the ladder: 1. resolvectl status — does the interface have a DNS server (Current DNS Server)? 2. getent hosts example.com — does it resolve through the system at all? 3. dig @8.8.8.8 example.com — does a public DNS server answer (if yes, the problem is local/resolved)?

A name resolves to the wrong IP

  • An entry in /etc/hosts overrides DNS — check it (getent hosts name).
  • A stale cache: resolvectl flush-caches.
  • Multiple A records/round-robin — that's normal (load balancing).

resolv.conf gets overwritten after a reboot

Don't edit /etc/resolv.conf directly (it's a symlink to resolved). Configure DNS in netplan (nameservers:) or /etc/systemd/resolved.conf, then run sudo netplan apply / systemctl restart systemd-resolved.

Internal names don't resolve, external ones do

There's no search domain or the right DNS for the internal zone. Check resolvectl status (Search Domains) and add the domain/server in netplan.

dig works, but the application "can't find" the host

dig goes to DNS bypassing NSS. Check with getent hosts/resolvectl query instead — the cause is in /etc/hosts, nsswitch.conf, or resolved's cache.

Terms

5
DNS
A distributed system of names and records. Also appears in: curl, hostname, Network diagnostics, ping and 19 more
resolver
A mechanism obtaining name data from configured sources. Also appears in: hostname, BIND9
A record
A DNS record carrying an IPv4 address for a name.
fully qualified domain name
A name fully identifying its place in DNS; a trailing dot denotes the root. Also appears in: hostname
DNS record TTL
The permitted record cache lifetime in seconds.

See also

  • ip — Managing interfaces, addresses, and routes (iproute2) — the replacement for ifconfig/route.
  • curl — HTTP/HTTPS requests from the CLI — debugging APIs and web services, headers, methods.
Loading…