# 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.

Source: https://aminbytes.dev/bytes/networking-fundamentals/client-side-dns/


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](../../http/basics/request-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:

1. **`/etc/nsswitch.conf`** says which sources to try, in which order. Usually: files first, then DNS.
2. **files** means **`/etc/hosts`**, a static list on this machine.
3. **dns** means: ask the name servers listed in **`/etc/resolv.conf`**.
4. 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.

```text {title="/etc/hosts"}
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-loopback
```

It'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:

```text {title="/etc/resolv.conf"}
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:

```bash
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:

```text {title="/etc/nsswitch.conf"}
passwd:         files systemd
group:          files systemd
hosts:          files dns
networks:       files
services:       files
protocols:      files
```

For names, only the `hosts:` line matters. `files dns` means `/etc/hosts` first, DNS second. To prefer DNS, swap them:

```text
hosts:          dns files
```

No reboot or restart is needed. You'll also meet other sources on that line:

- `myhostname`: always resolves the machine's own name (and `_gateway` to the default gateway), from systemd.
- `resolve`: ask systemd-resolved directly.
- `mdns4_minimal`: multicast DNS for `.local` names 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:

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

 (Side note: You may also find `/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:

```bash
getent hosts db01
```

```text
192.168.1.10    db01.example.lan db01 database
```

```bash
getent ahostsv4 example.com
```

```text
203.0.113.10    STREAM example.com
203.0.113.10    DGRAM
203.0.113.10    RAW
```

`getent 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

```bash
host example.com
```

```text
example.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 |

```bash
host 1.1.1.1
host no-such-name.example.com
```

```text
1.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:

```bash
dig example.com
```

```text
; <<>> 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: 56
```

What matters:

- **`status:`** `NOERROR` worked. `NXDOMAIN` means the name doesn't exist. `SERVFAIL` means the server failed to get an answer (broken upstream or a DNSSEC problem). `REFUSED` means 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:

```bash
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 down
```

Unlike 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 |

 (Side note: `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`:

```bash
ls -l /etc/resolv.conf
cat /etc/resolv.conf
```

```text
lrwxrwxrwx 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.lan
```

So `127.0.0.53` is not your real DNS server. To see the real ones, use `resolvectl`:

```bash
resolvectl status
```

```text
Global
       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`:

```ini {title="/etc/systemd/resolved.conf"}
[Resolve]
DNS=1.1.1.1 9.9.9.9
FallbackDNS=8.8.8.8
Domains=example.lan
```

## Debugging name resolution

Work through it step by step:

1. **Is it really DNS?** `ping 1.1.1.1` works but `ping example.com` fails: yes. If the address doesn't work either, go back to [network troubleshooting](../network-troubleshooting/).
2. **What do programs see?** `getent hosts example.com`. A wrong answer here but a right one from `dig` points at `/etc/hosts` or the `hosts:` line in `/etc/nsswitch.conf`.
3. **Which servers are used?** `cat /etc/resolv.conf`, and `resolvectl status` if it says `127.0.0.53`.
4. **What does DNS say?** `dig example.com` and read the `status:`. `NXDOMAIN` is a typo or a missing record. `SERVFAIL` is a server-side problem.
5. **Is it the server?** `dig @1.1.1.1 example.com`. If another server answers fine, your configured resolver is the problem.
6. **Can you reach the server at all?** `;; connection timed out; no servers could be reached` means no answer on port 53. Check the route to it and the firewall (DNS uses UDP 53, and TCP 53 for large answers).
7. **Is it an old answer?** A changed record can live on in caches until its TTL runs out. Flush with `resolvectl flush-caches` and compare with `dig @<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 default `hosts: files dns`. Swap to `dns files` to change the order.
- `/etc/resolv.conf`: `nameserver` (max 3), `search` (domains appended to short names), `domain`, `options` (`timeout`, `attempts`, `rotate`, `ndots`).
- `getent hosts NAME` resolves like a normal program, through NSS. `host` and `dig` query DNS only and ignore `/etc/hosts`.
- `dig @server name type`, `dig +short`, `dig -x IP` for reverse. Read `status:` (`NOERROR`, `NXDOMAIN`, `SERVFAIL`), the ANSWER section with TTL, and the `SERVER:` line.
- `host name`, `host -t MX domain`, `host IP` (reverse), `host name server`.
- systemd-resolved: stub on `127.0.0.53`, `/etc/resolv.conf` is a symlink to `/run/systemd/resolve/stub-resolv.conf`, managed with `resolvectl`, configured in `/etc/systemd/resolved.conf`.
- If `/etc/resolv.conf` is generated (NetworkManager, systemd-resolved, netplan), set DNS in that tool, not in the file.


## Byte check

1. You add `10.0.0.5 intranet` to `/etc/hosts`. `getent hosts intranet` finds it, but `dig intranet` doesn't. Why?
   - **`dig` asks DNS servers directly and ignores `/etc/hosts` and `nsswitch.conf`** (correct)
   - `/etc/hosts` needs a reboot first
   - `dig` only works for IPv6
   `getent` goes through the system resolver (NSS), like normal programs do. `dig` and `host` are DNS tools that only talk to name servers.

2. Which line makes the system ask DNS before looking in `/etc/hosts`?
   - `order dns,hosts` in `/etc/resolv.conf`
   - **`hosts: dns files` in `/etc/nsswitch.conf`** (correct)
   - `nameserver files` in `/etc/hosts`
   The `hosts:` line in `/etc/nsswitch.conf` lists the sources in the order they are tried.

3. `/etc/resolv.conf` contains only `nameserver 127.0.0.53`. What does that tell you?
   - DNS is broken: that's a loopback address
   - **systemd-resolved runs a local stub resolver that forwards queries** (correct)
   - The machine is a public DNS server
   `127.0.0.53` is systemd-resolved's stub listener. Use `resolvectl status` to see the real upstream servers.


