Weight: 2
Goal: Configure how the local system resolves hostnames into IP addresses.
This is a small, focused objective — only a handful of files and commands. Some of this overlaps with 109.2 and 109.3; the focus here is purely on DNS resolution from the client side.
You are not setting up a DNS server. You are
configuring how this machine asks name servers to
translate names like www.example.com into IP addresses.
There are three pieces:
/etc/hosts — Static
MappingsA plain text file that maps IP addresses to hostnames. The resolver
consults it before going to DNS on virtually every
Linux system (because of nsswitch.conf).
# IP address hostname(s)
127.0.0.1 localhost
127.0.1.1 myhost.localdomain myhost
192.168.1.50 fileserver fileserver.lan
::1 localhost ip6-localhost ip6-loopback
# are comments.www.example.com at a test server).Edits take effect immediately — there is no service to reload.
/etc/resolv.conf — DNS Servers and Search DomainsTells the resolver which DNS servers to query for
any name not found in /etc/hosts.
nameserver 192.168.1.1
nameserver 8.8.8.8
nameserver 1.1.1.1
search example.com lan
domain example.com
options timeout:2 attempts:1
| Keyword | Meaning |
|---|---|
nameserver IP |
A DNS server. List up to three; tried in order. |
search dom1 dom2 ... |
Domain suffixes appended to single-label names.
ping web will try web.dom1, then
web.dom2. |
domain NAME |
A single default domain. Mutually exclusive with
search — the last one in the file wins. |
options ... |
Tuning. Common: timeout:N, attempts:N,
rotate, ndots:N. |
search vs
domainYou use one or the other, not both.
search is more flexible (it accepts multiple domains). On
most modern systems you’ll only see search.
search in
actionWith search example.com lan in
/etc/resolv.conf:
ping web → tries web.example.com then web.lan
ping web. → trailing dot = fully qualified, no search applied
ping web.foo → contains dots, treated as fully qualified
The ndots option controls when search expansion happens:
ndots:1 (default) means “if the name has fewer than 1 dot,
apply the search list.”
/etc/resolv.conf is often auto-generatedOn modern systems, this file may be managed by another service:
| Manager | How it manages the file |
|---|---|
| NetworkManager | Rewrites /etc/resolv.conf based on connection
settings. |
systemd-resolved |
/etc/resolv.conf is a symlink to
/run/systemd/resolve/resolv.conf or
/run/systemd/resolve/stub-resolv.conf (pointing at the
local stub on 127.0.0.53). |
resolvconf (package,
Debian/Ubuntu) |
Generates the file from snippets supplied by clients (DHCP, openvpn, etc.). |
| DHCP client | Some clients overwrite the file directly with DHCP-provided servers. |
If your edits disappear, one of these is the cause. The correct fix depends on the manager:
nmcli connection modify name ipv4.dns "8.8.8.8 1.1.1.1")./etc/systemd/resolved.conf./etc/resolvconf/resolv.conf.d/base (or
head)./etc/dhcp/dhclient.conf./etc/nsswitch.conf — The Name Service SwitchThis file controls the order in which sources are consulted for each kind of lookup. It’s not just for hostnames — it also covers users, groups, and other databases.
passwd: files
group: files
shadow: files
hosts: files dns
networks: files
services: files
protocols: files
Each line has the form:
database: source1 source2 ...
hosts:hosts: files dns
means: “look in /etc/hosts first (files),
and if not found, query DNS.” This is why edits to
/etc/hosts work without any service restart.
Other sources you might see on the hosts: line:
| Source | Meaning |
|---|---|
files |
/etc/hosts. |
dns |
DNS (/etc/resolv.conf). |
mdns4 / mdns4_minimal |
Multicast DNS (e.g. .local names, Avahi/Bonjour). |
resolve |
systemd-resolved lookups. |
myhostname |
Built-in module that always resolves the local hostname. |
nis |
NIS server. |
A common modern line on systemd systems:
hosts: files mdns4_minimal [NOTFOUND=return] resolve [!UNAVAIL=return] dns
Reads as: try local files, then mDNS for .local names,
then systemd-resolved, finally falling back to plain DNS.
The exam doesn’t quiz on the bracketed action codes — recognizing
files and dns is enough.
Three tools, three different scopes.
host —
quick lookup, talks directly to DNShost example.com
host example.com 8.8.8.8 # query a specific server
host -t MX example.com # specific record type
host 93.184.216.34 # reverse lookupdig —
detailed lookup, talks directly to DNSdig example.com
dig +short example.com
dig @8.8.8.8 example.com
dig example.com MX
dig -x 93.184.216.34 # reverse lookupgetent hosts
— what the system actually returnsgetent hosts example.com
getent ahosts example.comgetent goes through /etc/nsswitch.conf, so
it sees /etc/hosts and any other configured source. If
host and dig say one thing but
getent says another, the answer is coming from
/etc/hosts (or another non-DNS source).
This is the key debugging insight from this objective:
host and dig test DNS;
getent tests the system’s full resolution
chain.
nslookup — legacy
alternativenslookup example.com
nslookup example.com 8.8.8.8Still works; host and dig are preferred
today but nslookup is on the exam.
systemd-resolvedsystemd-resolved is the DNS client service shipped with
systemd. Where it is enabled:
/etc/resolv.conf is a symlink to a file under
/run/systemd/resolve/.127.0.0.53 — applications
query it, and it forwards to the configured upstream servers./etc/systemd/resolved.conf.resolvectl status:resolvectl status # show DNS servers, search domains, per-link config
resolvectl query example.com
resolvectl flush-caches # clear the local DNS cacheYou only need to recognize that this service exists for the exam, not configure it deeply.
Files:
/etc/hosts — static mappings./etc/resolv.conf — DNS servers and search domains./etc/nsswitch.conf — order of resolution sources./etc/hostname — local hostname (touched here in case of
confusion with hosts).Commands:
hostdignslookup (legacy)getent hostsConcepts:
/etc/hosts (via files) before
DNS, controlled by nsswitch.conf.search vs domain in
resolv.conf.nameserver entries.host/dig test DNS; getent
tests the system resolver./etc/resolv.conf is often auto-generated by
NetworkManager, systemd-resolved, resolvconf, or a DHCP client.Which file holds static hostname-to-IP mappings on a
Linux client? /etc/hosts.
Which file lists the DNS servers used by the
resolver? /etc/resolv.conf.
What is the maximum number of nameserver
entries that /etc/resolv.conf will use?
Three.
What is the difference between the domain
and search directives in
/etc/resolv.conf? domain sets a
single default domain. search lists multiple suffixes to
try. They are mutually exclusive — the last one in the file
wins.
What is the role of /etc/nsswitch.conf for
hostname resolution? It defines the order in which the resolver
consults different sources. The hosts: line typically reads
files dns, meaning /etc/hosts is checked
before DNS.
You edit /etc/hosts to add a new mapping. Do
you need to restart anything? No. The file is read on every
lookup; changes take effect immediately.
You edit /etc/resolv.conf and the changes
vanish after a few minutes. Why? Something is auto-generating
the file — typically NetworkManager, systemd-resolved,
resolvconf, or a DHCP client. Configure DNS through that
tool instead.
Which tool tells you whether the answer is coming from
/etc/hosts or from real DNS?
getent hosts uses the full system resolver (and thus
/etc/hosts), whereas host and dig
go straight to DNS. Comparing the two reveals where the answer comes
from.
How do you query a specific DNS server for an A record
using host?
host example.com 8.8.8.8.
How do you query a specific DNS server for an A record
using dig?
dig @8.8.8.8 example.com.
What does the line hosts: files dns in
/etc/nsswitch.conf mean? For host lookups, consult
/etc/hosts first (files), then fall back to
DNS.
What does the search example.com lan
directive cause when you run ping web? The
resolver tries web.example.com first, then
web.lan.
What is the loopback address typically listed for
localhost in /etc/hosts?
127.0.0.1 for IPv4 and ::1 for IPv6.
What systemd service may make
/etc/resolv.conf a symlink to a file under
/run/systemd/resolve/?
systemd-resolved.
In /etc/hosts, a line is
192.168.1.50 fileserver fileserver.lan. Which name is the
canonical name? fileserver — the first hostname on
the line. fileserver.lan is an alias.