Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Networking and Firewalls

DNS and Name Resolution on Linux

Understand how Linux turns names into addresses: nsswitch, /etc/hosts, systemd-resolved, resolv.conf, and how to debug problems with dig, getent and resolvectl.

"The network is down" very often means "name resolution is broken". Pings to 1.1.1.1 work, but nothing that uses a hostname does. To fix that quickly you need a clear picture of what happens between an application calling getaddrinfo("example.org") and an IP address coming back, and which files and services take part.

This lesson follows that path on a modern Linux system, introduces the diagnostic tools (getent, dig, resolvectl) and shows the common failure patterns. Commands are read-only snippets, because the site cannot reach a real resolver; the runnable blocks are Python stand-ins that model the lookup order and the resolv.conf file.

The resolution path

Applications do not talk to DNS directly. They call the C library, which consults /etc/nsswitch.conf to decide which sources to ask and in which order:

TEXT
hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname

Read it left to right. files means /etc/hosts first. dns means ask the configured DNS servers. myhostname always resolves the machine's own name. So a line in /etc/hosts beats DNS, which is both a handy override and a classic source of "it works on my machine" confusion.

flowchart LR
    A["application<br/>getaddrinfo()"] --> N["nsswitch.conf<br/>hosts: files dns"]
    N --> H["/etc/hosts"]
    N --> S["stub resolver<br/>127.0.0.53"]
    S --> U["upstream DNS<br/>router or 1.1.1.1"]

The stub resolver at 127.0.0.53 is systemd-resolved; it caches answers and forwards the rest to upstream servers.

/etc/hosts

A static table of address, canonical name and aliases:

TEXT
127.0.0.1   localhost
127.0.1.1   web01.example.org web01
10.0.0.20   db01.internal db01

Use it for tiny setups, for pointing a name at a test server, or for blocking a hostname (0.0.0.0 ads.example). Do not use it as a substitute for real DNS on many machines; nobody remembers to update 40 copies.

/etc/resolv.conf

The classic file that lists DNS servers:

TEXT
nameserver 1.1.1.1
nameserver 9.9.9.9
search internal.example.org example.org
options timeout:2 attempts:2
  • nameserver lines are tried in order (at most three are used).
  • search is a list of domains appended to short names: ping db01 tries db01.internal.example.org first.
  • options tunes timeouts and retries.

On most modern distributions you do not edit this file. It is generated, and on Ubuntu, Fedora and many others it is a symlink to a file managed by systemd-resolved or NetworkManager. Check with ls -l /etc/resolv.conf. If it points to /run/systemd/resolve/stub-resolv.conf, the nameserver you see is the local stub 127.0.0.53, and the real upstream servers are shown by resolvectl status. Editing a generated file works until the next DHCP renewal overwrites it.

Python A tiny resolv.conf parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The ndots option (default 1) decides when the search list is used first: a name with fewer dots than ndots is tried with the search domains first, otherwise as an absolute name first. Container platforms often set ndots:5, which explains why short external lookups there can trigger several wasted queries.

systemd-resolved and resolvectl

systemd-resolved runs a local caching stub and supports per-interface DNS settings (useful with VPNs, where only corp.example should go to the VPN's server).

Terminal
resolvectl status                # servers and domains per link
resolvectl query example.org     # resolve through the stub, shows cache/protocol
resolvectl flush-caches
sudo resolvectl dns ens3 1.1.1.1 9.9.9.9
sudo resolvectl domain ens3 internal.example.org

The permanent setting normally lives in NetworkManager or netplan (nameservers:), or in /etc/systemd/resolved.conf with DNS=, FallbackDNS= and DNSSEC= keys, followed by sudo systemctl restart systemd-resolved.

Testing with getent, dig and host

Choose the tool that tests the layer you care about.

Tool What it tests
getent hosts name The full nsswitch path, exactly as applications see it
dig name DNS only: talks directly to a resolver, shows details
host name A short DNS answer
resolvectl query name Through systemd-resolved with its cache
Terminal
getent hosts example.org
dig example.org A +short
dig example.org AAAA
dig @1.1.1.1 example.org            # ask a specific server
dig +trace example.org              # walk from the root servers down
dig -x 203.0.113.9                  # reverse lookup (PTR)
dig example.org MX +short

If getent works but dig fails, or the other way around, the problem is in the layer that differs: a stale /etc/hosts entry, or a resolver that only the stub can reach. If dig @1.1.1.1 works while dig (default servers) fails, your configured upstream is broken or blocked.

Important record types: A (IPv4), AAAA (IPv6), CNAME (alias), MX (mail), TXT (SPF, verification), NS (delegation), PTR (reverse), SRV (service location). The number in the answer section is the TTL: how many seconds a cache may keep the result, which is why DNS changes take time to propagate.

Python Lookup order simulator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Typical failures

  • Cannot resolve anything, ping to IPs works. No reachable nameserver. Check resolvectl status, then dig @1.1.1.1 example.org to see whether the network allows DNS (UDP and TCP port 53) at all.
  • One name resolves wrongly. Look at /etc/hosts, then at the TTL: a recent DNS change may still be cached. resolvectl flush-caches clears the local cache only.
  • Slow logins or sudo hanging for 5 to 10 seconds. A reverse lookup or hostname lookup times out. Make sure the machine's own hostname is in /etc/hosts.
  • resolv.conf keeps reverting. Another service owns it; configure that service instead.
  • Works on the host, fails in a container. The container may use a different resolver or ndots setting.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file decides whether /etc/hosts or DNS is consulted first?

    The hosts: line in nsswitch.conf lists the sources in order, for example files then dns.
  2. 2. getent hosts example.org works, but dig example.org fails. What does this most likely indicate?

    getent follows nsswitch (files, stub, cache); dig asks DNS servers directly, so different layers disagree.
  3. 3. On Ubuntu, /etc/resolv.conf shows nameserver 127.0.0.53. Where do you find the real upstream servers?

    127.0.0.53 is the systemd-resolved stub; resolvectl status lists the upstream servers per interface.
  4. 4. What does the TTL in a DNS answer mean?

    TTL is in seconds and controls cache lifetime, which is why changes propagate slowly.

Summary

  • Applications resolve names through nsswitch.conf: usually files (/etc/hosts) first, then dns.
  • /etc/resolv.conf lists nameservers, search domains and options, but is usually generated by systemd-resolved or NetworkManager; configure the owning service instead of editing it.
  • getent hosts tests what applications see; dig tests DNS itself; resolvectl shows the systemd-resolved view and cache.
  • Record types and TTLs explain most "it resolves here but not there" puzzles.
  • Check the hosts file, the stub resolver, the upstream server and the TTL in that order when names misbehave.

Next: Firewalls with nftables, ufw and firewalld