Sub topic 3
Configuring DNS Servers and Search Domains
This chapter assumes you can edit files in a terminal editor, run commands with sudo, and read an IPv4 address with a prefix length such as 192.168.1.50/24. It also assumes the server has a static address, or that you are about to set one. You need console or out-of-band access in case SSH drops during the change.
Setting DNS servers and search domains
The resolver sends name lookups to the DNS servers you list. A search domain is a suffix the resolver appends to a short name. With example.com as a search domain, a lookup for db01 also tries db01.example.com.
On a Netplan system, add the servers and search domain under the interface:
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]Changing the network without locking yourself out
Netplan files can hold secrets, so restrict them to root:
sudo chmod 600 /etc/netplan/01-static.yaml
sudo netplan trynetplan try applies the file and waits for you to press Enter. If you do not confirm within about 120 seconds, it rolls back to the previous configuration. Keep your console open while you test.
> Note: Run netplan try from a session you can afford to lose. If the new address is wrong, the rollback restores the old one only when the timer expires and you do not confirm.
Applying the configuration and verifying it
After the change holds, make it permanent and check the result:
sudo netplan apply
resolvectl status enp1s0
resolvectl query example.com
getent hosts example.comresolvectl status shows the DNS servers and search domains in use. getent hosts checks the full lookup path, including /etc/nsswitch.conf and /etc/hosts.
Troubleshooting common failures
| Symptom | Likely cause | Check |
|---|---|---|
| Local hosts reachable, internet names fail | Wrong or missing DNS server | `resolvectl status enp1s0` |
| Gateway unreachable | Wrong prefix length or gateway | `ip -br address show dev enp1s0` |
| Outside IP unreachable | Missing default route | `ip route get 9.9.9.9` |
| Short names fail, full names work | Search domain missing | `resolvectl status enp1s0` |
Test the resolver directly with resolvectl query example.com. If a lookup fails, check whether the DNS server answers by pinging it.
flowchart LR
A["Application asks for a name"] --> B["nsswitch.conf order"]
B --> C["/etc/hosts"]
B --> D["systemd-resolved"]
D --> E["DNS server from Netplan"]caption: How a name lookup moves from the application to the DNS server. alt: A left-to-right flow. The application asks for a name, nsswitch decides the order, the lookup checks the hosts file, then systemd-resolved sends the query to the configured DNS server.
Exercise
Exercise
You set search: [example.com] and run ping db01. The lookup fails, but ping db01.example.com works. What is the most likely cause, and which command confirms it?
Show answer
The search domain is not active on the interface. Run resolvectl status enp1s0 and check that example.com appears under the search domains. If it is missing, check the Netplan file indentation and run sudo netplan apply again.