Name resolution on Linux

Intermediate · 4 min

Follow a hostname through nsswitch.conf, /etc/hosts, resolv.conf and systemd-resolved, and see why getent and dig can disagree.

Before thisUsers, groups and the account filesRouting and the default gateway

Slap Ya Administrator · 4 min

Name resolution on Linux

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Slap Ya AdministratorReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Programs do not ask DNS directly. They call the C library's `getaddrinfo()`, which follows the `hosts:` line in `/etc/nsswitch.conf` to decide which sources to ask and in what order.
  • `/etc/hosts` is a local, static source. `/etc/resolv.conf` tells the classic DNS client which nameservers and search domains to use.
  • On systemd-resolved systems `/etc/resolv.conf` is a symlink listing `nameserver 127.0.0.53`. That is a local stub that forwards to the real servers. `resolvectl status` shows which servers those are, per interface.
  • `getent hosts <name>` asks through NSS, the way applications do. `dig` talks DNS straight to a server and skips `/etc/hosts` and NSS entirely.
  • When a name resolves in `dig` but not in your app, or the other way round, the difference is almost always NSS order, `/etc/hosts`, or a search domain.

Names are not only DNS

You type ssh db1, and somehow it connects to 10.0.0.5. Most people would say “DNS did that”. Often it did. But the program never spoke DNS itself. It called a C library function, getaddrinfo(), and the library decided where to look. DNS is one of several places it can look, and not always the first.

That gap is behind a whole family of confusing tickets: a name that works in the browser but not in a script, a staging hostname that points at production on one server only, a dig that says the record exists while the app swears it does not. Once you know the path a lookup takes, those stop being mysteries. What DNS records are and how TTLs behave is covered in DNS records and TTLs in practice. This lesson is the client side, on the Linux machine.

Step one: nsswitch.conf

The Name Service Switch, which you met for users and groups in the account files lesson, also handles host names. The hosts: line in /etc/nsswitch.conf lists sources in order:

hosts: files dns

files means /etc/hosts. dns means the classic resolver configured by /etc/resolv.conf. The nsswitch.conf man page (man-pages 6.19, checked 2026-11-28) sets the default behaviour: on success the lookup returns; on notfound, unavail or tryagain it continues to the next source. You can override that with action items such as [NOTFOUND=return].

On a system running systemd-resolved, the nss-resolve man page recommends a longer line:

hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns

Read [!UNAVAIL=return] as “unless resolved is unavailable, its answer is final”. The later files and dns entries are a fallback for when the service is down. That does not mean /etc/hosts is ignored: the resolved man page says “the mappings defined in /etc/hosts are resolved to their configured addresses and back” by resolved itself.

Step two: /etc/hosts and resolv.conf

/etc/hosts is the oldest source: lines of address and names. It is the right tool for a handful of fixed mappings on one machine and the wrong tool for anything a second machine also needs to know.

/etc/resolv.conf configures the DNS client: nameserver lines, a search list of domains to try for short names, and options such as ndots. The search list deserves respect. With search corp.example, a lookup of db1 also tries db1.corp.example, which is convenient and also why a short name can resolve to something you did not expect.

Step three: the 127.0.0.53 stub

Open /etc/resolv.conf on a recent Ubuntu or Fedora and you find one server, 127.0.0.53. That is systemd-resolved, a local caching resolver. The resolved man page (systemd 262) describes four ways it can relate to the file. The recommended one is a symlink to /run/systemd/resolve/stub-resolv.conf, which lists only the stub. Another file, /run/systemd/resolve/resolv.conf, lists the real upstream servers, for programs that must bypass the stub. A second listener on 127.0.0.54 acts as a plain proxy without resolved’s extras.

The point of the stub is that DNS servers can differ per interface. A VPN can say “names under corp.example go to the VPN’s server” while everything else goes to the normal one. A single flat resolv.conf cannot express that. To see what is really configured:

resolvectl status            # upstream servers and search domains, per link
resolvectl query example.com # resolve through resolved and say which link answered

resolved also synthesises a few names itself, including localhost and _gateway, which resolves to the current default gateway.

Commands written from the systemd and glibc man pages, not run, since there is no Linux machine here.

getent versus dig

To see what an application sees, use getent hosts <name> (or getent ahosts). It goes through NSS, exactly as getaddrinfo() does. dig and nslookup are DNS tools. They build a DNS query, send it to a server and print the reply. They never read nsswitch.conf or /etc/hosts.

I can show the difference on macOS, which has the same split. This was run on the Mac I wrote this on. macOS has no getent. dscacheutil is its equivalent that asks through the system resolver:

$ grep broadcasthost /etc/hosts
255.255.255.255	broadcasthost
$ dig +short broadcasthost
$ dig broadcasthost | grep status
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 11762
$ dscacheutil -q host -a name broadcasthost
name: broadcasthost
ip_address: 255.255.255.255

The name exists in /etc/hosts, so the system resolver finds it. dig asked the home router, which has never heard of it. Neither tool is wrong. They answer different questions. When debugging, run both and compare.

About the video

The embed is all of Slap Ya Administrator’s “Why does /etc/resolv.conf point at 127.0.0.53 in Linux?”, October 2023, about four minutes. Its description explains the two files resolved generates, the stub one and the full one, which is the most confusing part of modern Linux name resolution. It is narrow: it does not cover nsswitch.conf, /etc/hosts or getent, which is why most of this lesson is spent on them, and the ComputerNetworkingNotes video in deeper covers nsswitch.conf. I judged the video from its title and description, and English from the English auto-caption track. Facts were checked against the nsswitch.conf, systemd-resolved and nss-resolve man pages on 2026-11-28. The dig and dscacheutil output is my only run.

Where this fits

Everything so far was set with commands that disappear at reboot, or files generated by something else. Next, what generates them: persistent network configuration.

Check yourself

  1. 01An entry in `/etc/hosts` maps `db.internal` to 10.0.0.5. `dig db.internal` says NXDOMAIN, but `curl http://db.internal` works. Is something broken?

    No. `dig` queries a DNS server and never reads `/etc/hosts`. `curl` resolves through the C library and NSS, which checks `files` first. Use `getent hosts db.internal` to see what applications see.

  2. 02`/etc/resolv.conf` says `nameserver 127.0.0.53`. Where are the real DNS servers configured?

    In systemd-resolved, which listens on that address and forwards queries. `resolvectl status` lists the upstream servers, usually learned per interface from DHCP or the network configuration.

  3. 03What does `[NOTFOUND=return]` after `dns` on a `hosts:` line change?

    If DNS answers authoritatively that the name does not exist, the lookup stops there instead of continuing to the next source. The default action for NOTFOUND is to continue.

  4. 04Why might editing `/etc/resolv.conf` by hand stop working after a reboot or a DHCP renewal?

    On many systems the file is generated, by systemd-resolved, NetworkManager or another tool. Change the configuration of whatever owns it instead of the file.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark