Sub topic 9
Configuring DNS servers and search domains
This chapter covers how a Linux server learns which DNS servers to ask and which domain suffixes to try. It assumes you can read a Netplan or nmcli configuration and know that a name lookup ends at a DNS server. It also assumes you have read the earlier chapters on identifying your network tool and on how a name passes through nsswitch.conf and /etc/hosts. Here the focus is the last step, the resolver.
What the resolver configuration contains
Two settings matter for almost every server:
- DNS servers (also called nameservers) are the IP addresses the system sends queries to. You usually list two or more so a lookup survives one server failing.
- Search domains are suffixes the resolver appends to short names. With the search domain
example.com, a lookup fordb01is tried asdb01.example.com.
Neither setting belongs to DNS itself. They are client settings, stored per network interface by whichever tool manages that interface, then combined by a resolver service on the host.
Where the settings end up
On most current distributions, a service called systemd-resolved receives the DNS settings from Netplan, systemd-networkd, or NetworkManager. Applications do not talk to it directly. They call the C library, which reads /etc/resolv.conf. On a typical Ubuntu server, that file points at a local stub listener:
nameserver 127.0.0.53
options edns0 trust-ad
search example.comThe address 127.0.0.53 is not your real DNS server. It is the stub resolver inside systemd-resolved, which forwards queries to the servers configured for each interface.
The practical consequence is that you should not edit /etc/resolv.conf by hand on such a system. It is a symlink, usually to /run/systemd/resolve/stub-resolv.conf, and the service regenerates it. Set DNS in the tool that owns the interface.
Not every distribution works this way. Check what you have:
ls -l /etc/resolv.conf
systemctl is-active systemd-resolved
resolvectl statusIf /etc/resolv.conf is a regular file and systemd-resolved is inactive, your network tool writes the file directly. This is common on RHEL-family systems with NetworkManager's default dns=default mode. The same nmcli settings still apply, but the result appears directly in /etc/resolv.conf.
Setting DNS with Netplan
Netplan puts DNS settings under nameservers for each interface. This is the same file you built in the static IP chapter:
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: false
addresses:
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [192.168.1.1, 9.9.9.9]
search: [example.com]addresses lists the servers in the order they are preferred. search lists the suffixes. Both are lists even when they hold one item.
An interface that still uses DHCP can also have fixed DNS. DHCP usually hands out DNS servers too, and by default they are merged with yours. To ignore the DHCP-supplied servers, tell Netplan not to use them:
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: true
dhcp4-overrides:
use-dns: false
use-domains: false
nameservers:
addresses: [10.0.0.2, 10.0.0.3]
search: [corp.example.com]use-dns: false drops the DHCP servers and use-domains: false drops the DHCP search domains. Without them, you may find extra servers in resolvectl status that you never configured.
Apply the file with sudo netplan try, as described in the earlier chapter on safe remote changes. A DNS typo does not cut your SSH session, but the try rollback is still worth using.
Setting DNS with NetworkManager
NetworkManager stores DNS settings in the connection profile. The commands below use the profile name from the earlier chapter:
sudo nmcli connection modify "Wired connection 1" \
ipv4.dns "192.168.1.1 9.9.9.9" \
ipv4.dns-search "example.com"
sudo nmcli connection up "Wired connection 1"Two details cause most confusion here.
First, ipv4.dns replaces the whole list. To add one server without retyping the rest, use the plus sign: nmcli connection modify "Wired connection 1" +ipv4.dns 1.1.1.1. A leading minus removes an entry.
Second, a profile that uses DHCP merges the DHCP-supplied DNS with yours. To use only your servers, set:
sudo nmcli connection modify "Wired connection 1" ipv4.ignore-auto-dns yes
sudo nmcli connection up "Wired connection 1"Changes take effect only when the connection is reactivated. nmcli connection modify alone changes the stored profile, not the running state.
Global settings with systemd-resolved
Per-interface settings are the right default. Some servers need a setting that applies regardless of interface, such as a fallback server. For this, use a drop-in file for systemd-resolved rather than editing the main file:
# /etc/systemd/resolved.conf.d/dns.conf
[Resolve]
DNS=192.168.1.1 9.9.9.9
FallbackDNS=1.1.1.1
Domains=example.comAfter creating it, restart the service:
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo systemctl restart systemd-resolvedDNS servers are used for all interfaces in addition to per-link servers. FallbackDNS is used only when no other server is known at all. Domains sets global search domains. Prefer the drop-in directory over /etc/systemd/resolved.conf because package updates do not overwrite your file.
Keep one source of truth for each server. When the same address appears in Netplan and in a global drop-in, it still works, but the next person to change it will probably update only one place.
Search domains in practice
A search domain saves typing, and it also changes behavior in ways that surprise people.
With search example.com, running ssh db01 makes the resolver try db01.example.com. If that fails, it tries db01 on its own. The rule that decides the order is ndots, an option in /etc/resolv.conf with a default of 1:
- A name with fewer dots than
ndotsis tried with the search suffixes first. - A name with at least that many dots is tried as written first, then with suffixes.
So db01 (zero dots) gets the suffix first. db01.internal (one dot) is tried as a fully qualified name first, and only falls back to the suffix when that fails.
This has two consequences you should expect.
- Extra queries. A mistyped or nonexistent name can produce several failed queries, one per search domain, before the final failure. A long search list slows down every failed lookup.
- Unintended matches. If you list several search domains, a short name resolves to whichever domain answers first. Two domains that both contain
db01give an ambiguous result that depends on list order.
Keep the list short, usually one domain, and use fully qualified names in configuration files and scripts. A trailing dot, as in db01.example.com., marks a name as absolute and skips the search list entirely.
Routing domains
systemd-resolved has one feature that plain resolv.conf does not: routing domains. A domain written with a leading tilde, such as ~corp.example.com, is not added as a search suffix. It only tells the resolver that queries for names under that domain go to the servers on that interface.
Use this when a server has two interfaces and each network has its own DNS. In Netplan, add the routing domain to the interface that serves it:
nameservers:
addresses: [10.20.0.2]
search: [~corp.example.com]Queries for *.corp.example.com go to 10.20.0.2. Everything else goes to the servers on the other interface. The special value ~. routes all queries to an interface, which makes it the preferred default route for DNS.
Checking what the resolver is using
Always read back the running state instead of trusting the file you edited:
resolvectl status
resolvectl dns
resolvectl domainresolvectl status prints a section for each interface plus a global section. Look for Current DNS Server and DNS Servers under your interface, and for DNS Domain to see search and routing domains. If a server or domain appears that you did not configure, DHCP or another profile supplied it.
To test a lookup through the full resolver path:
resolvectl query example.com
getent hosts example.com
dig @192.168.1.1 example.com +shortThese three commands test different things:
| Command | What it exercises |
|---|---|
| `resolvectl query` | The `systemd-resolved` service, including per-link routing and its cache |
| `getent hosts` | The full system path, including `nsswitch.conf` and `/etc/hosts`, as most programs use it |
| `dig @server` | One specific DNS server, bypassing local configuration entirely |
If dig @192.168.1.1 works but getent hosts fails, the server is fine and the local configuration is wrong. If dig fails too, the problem is the server or the path to it. Remember that dig and nslookup never read /etc/hosts, so a name that exists only there will fail in those tools and succeed in getent.
After changing DNS, clear the cache so you do not read an old answer:
sudo resolvectl flush-cachesCommon errors
- Edited
/etc/resolv.confand the change vanished. The file is generated. Change the Netplan or NetworkManager profile instead. resolvectl statusshows servers you did not set. DHCP supplied them. Useuse-dns: falsein Netplan oripv4.ignore-auto-dns yesin NetworkManager.- Short names fail but full names work. The search domain is missing or on a different interface. Check
resolvectl domain. - Internal names fail only for some queries. Another interface's DNS answers first. Add a routing domain for the internal zone.
- More than three servers configured, only some in effect. The C library reads at most three
nameserverlines. With the stub this is hidden, but on systems that writeresolv.confdirectly, extra entries are ignored. - A name resolves in
digbut not in an application. Compare withgetent hosts. The difference points atnsswitch.conf,/etc/hosts, or the search list.
Exercise
After applying a Netplan file with nameservers: addresses: [192.168.1.1], resolvectl status also shows 8.8.8.8 under your interface. Name the likely cause and give the Netplan setting that removes it.
Show answer
The interface uses DHCP, and the DHCP server supplied 8.8.8.8, which is merged with your configured server. Add dhcp4-overrides: use-dns: false under the interface (and use-domains: false for DHCP search domains), then run sudo netplan try and confirm with resolvectl status.
Exercise
The search domain is example.com, and ndots is 1. In what order are the names app01 and app01.staging tried, and how can you make the resolver skip the search list entirely?
Show answer
app01 has zero dots, so it is tried as app01.example.com first, then as app01. app01.staging has one dot, which meets ndots, so it is tried as app01.staging first, then as app01.staging.example.com. Ending a name with a dot, as in app01.staging.example.com., makes it absolute and skips the search list.
Exercise
dig @192.168.1.1 internal.example.com +short returns an address, but curl http://internal.example.com fails with a name resolution error. List two checks that narrow down the cause.
Show answer
Run getent hosts internal.example.com to see whether the system path fails, which would indicate a problem in nsswitch.conf, /etc/hosts, or the local resolver configuration. Run resolvectl status and resolvectl query internal.example.com to see which servers and domains systemd-resolved uses for that interface, and check whether another interface or a routing domain sends the query to a different server.