Exam weight: 2 โ LPIC-1 v5, Exam 102
What You Need to Know
From the official LPIC-1 objectives:
- Query remote DNS servers.
- Configure local name resolution and use remote DNS servers.
- Modify the order in which name resolution is done.
- Debug errors related to name resolution.
- Awareness of
systemd-resolved.
Key files and commands: /etc/hosts, /etc/resolv.conf, /etc/nsswitch.conf, host, dig, getent.
Name Resolution Process
When a program resolves a hostname, it calls the GNU C library (glibc), which reads /etc/nsswitch.conf to determine where and in what order to look up the name.
DNS has three record classes:
- IN โ Internet (TCP/IP addresses; used in all standard lookups).
- CH โ ChaosNet (legacy; rarely encountered).
- HS โ Hesiod (used to store passwd/group data in DNS).
/etc/nsswitch.conf
Controls the sources and order used for each type of name database.
passwd: compat
group: compat
hosts: dns [!UNAVAIL=return] files
networks: nis [NOTFOUND=return] files
services: nis [NOTFOUND=return] files
The hosts line determines how hostnames are resolved. Each column is a source. Columns in brackets provide conditional logic:
| Notation | Meaning |
|---|---|
[!UNAVAIL=return] | If the service IS available, stop here (even if not found). |
[NOTFOUND=return] | If the lookup succeeded but entry was not found, stop. |
[SUCCESS=continue] | If found, continue to the next source anyway. |
Common hosts line examples:
hosts: files dns # check /etc/hosts first, then DNS
hosts: dns files # check DNS first, then /etc/hosts
/etc/resolv.conf
Configures DNS resolution. May be overwritten by NetworkManager or other tools (look for a # Generated by NetworkManager comment at the top).
search lpi.org
nameserver 10.0.0.53
nameserver fd00:ffff::2:53
| Directive | Description |
|---|---|
nameserver IP | DNS server to use (up to 3 entries) |
search DOMAIN | Append domain for short name lookups (up to 6) |
domain DOMAIN | Local domain name (mutually exclusive with search) |
options KEY:VALUE | Resolver options, e.g., options timeout:3 |
search and domain are mutually exclusive โ if both appear, the last one takes effect.
/etc/hosts
Maps IP addresses to hostnames locally. Checked before DNS when files appears first in /etc/nsswitch.conf.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
10.0.0.1 gateway.lpi.org gateway gw
10.0.1.53 dns1.lpi.org
Format: IP_ADDRESS primary-hostname [alias ...]
The IP address is always the first column.
systemd-resolved
systemd-resolved is a systemd service that provides DNS resolution on 127.0.0.53. It proxies queries to the servers configured in /etc/resolv.conf or /etc/systemd/resolv.conf.
To use it, set resolve as a source in /etc/nsswitch.conf:
hosts: resolve [!UNAVAIL=return] files
Note: systemd-resolved does not replace a full DNS server; it only forwards requests.
getent
getent retrieves entries from NSS databases using the same resolver libraries that applications use, respecting /etc/nsswitch.conf.
getent hosts # list all entries from the hosts database
getent hosts dns1.lpi.org # look up a specific hostname
getent -s files hosts learning.lpi.org # force lookup from /etc/hosts
getent -s dns hosts learning.lpi.org # force lookup via DNS
The -s option (glibc 2.2.5+) specifies which data source to use.
Key difference: getent uses the full NSS stack (respects nsswitch.conf); host and dig query DNS directly.
host
host is a simple DNS lookup tool. By default it returns A, AAAA, and MX records.
host wikipedia.org # A, AAAA, MX records
host 208.80.154.224 # reverse lookup (PTR record)
host -t NS lpi.org # query specific record type
host -t MX lpi.org dns1.easydns.com # use specific nameserver
| Option | Description |
|---|---|
-t TYPE | Record type: A, AAAA, MX, NS, SOA, PTR, etc. |
| Last argument | If it is an IP or hostname of a name server, use that server instead of /etc/resolv.conf |
dig
dig is a verbose DNS diagnostic tool. By default it queries for A records.
dig learning.lpi.org # A record lookup
dig -t SOA lpi.org # query SOA record
dig +short lpi.org # terse output (IP only)
dig +noall +answer +question lpi.org # show only question and answer sections
dig +nocookie -t MX lpi.org # disable EDNS cookie extension
dig Output Sections
| Section | Contents |
|---|---|
| Header | Query metadata (opcode, status, flags, counts) |
| Question | The query that was sent |
| Answer | Resource records returned |
| Authority | NS records for the authoritative zone |
| Additional | A/AAAA records for servers in authority section |
dig Options
| Option | Description |
|---|---|
-t TYPE | Record type to query |
+short | Suppress all output except the result |
+noall | Disable all output sections |
+answer | Re-enable the answer section |
+question | Re-enable the question section |
~/.digrc
Default dig options can be set in ~/.digrc. For example, to always use +short:
+short
Quick Reference
/etc/nsswitch.conf:
hosts: files dns check /etc/hosts first, then DNS
[!UNAVAIL=return] stop if service is available (even if not found)
[NOTFOUND=return] stop if found but entry absent
[SUCCESS=continue] continue even if found
/etc/resolv.conf:
nameserver IP DNS server (up to 3)
search DOMAIN short-name search domain (up to 6)
domain DOMAIN local domain (mutex with search)
options timeout:3
/etc/hosts:
IP hostname [alias] left column is always IP
systemd-resolved:
listens on 127.0.0.53; proxies to configured DNS
use "resolve" in nsswitch.conf hosts line
getent hosts [NAME] NSS lookup (respects nsswitch.conf)
getent -s files hosts NAME force /etc/hosts
getent -s dns hosts NAME force DNS
host NAME returns A, AAAA, MX
host -t TYPE NAME specific record type
host NAME server use specific nameserver
dig NAME verbose A lookup
dig -t TYPE NAME specific record type
dig +short NAME terse (IP only)
dig +noall +answer NAME answer section only
~/.digrc default dig options
Exam Questions
- What file controls the order in which name databases are consulted? โ
/etc/nsswitch.conf - What does
[!UNAVAIL=return]mean in/etc/nsswitch.conf? โ If the service is available (not unavailable), stop here and do not try the next source. - How many
nameserverentries can appear in/etc/resolv.conf? โ Up to 3. - How many
searchdomains can be configured in/etc/resolv.conf? โ Up to 6. - What happens if both
searchanddomainappear in/etc/resolv.conf? โ They are mutually exclusive; the last one in the file takes effect. - What is the correct format of an
/etc/hostsentry? โ IP address in the first column, followed by hostname and optional aliases:IP hostname [alias] - On what address does
systemd-resolvedlisten? โ127.0.0.53 - What
nsswitch.confsource keyword enablessystemd-resolved? โresolve - What is the key difference between
getentandhost/dig? โgetentuses the full NSS resolver stack (respects nsswitch.conf);hostanddigquery DNS directly. - What
getentoption forces a lookup using/etc/hosts? โ-s files - What does
host wikipedia.orgreturn by default? โ A, AAAA, and MX records. - What
hostoption specifies the DNS record type to query? โ-t TYPE - How do you use
hostto query a specific name server? โ Pass the server IP or hostname as the last argument:host NAME server. - What
digoption shows only the IP address result? โ+short - What file stores default
digoptions? โ~/.digrc - What
digcombination shows only the question and answer sections? โdig +noall +answer +question NAME - What does
dig -t MX lpi.orgdo? โ Queries for the MX (Mail Exchanger) records forlpi.org. - Why might manual changes to
/etc/resolv.confnot persist? โ Tools like NetworkManager regenerate this file; changes are overwritten when the network is reconfigured.
Exercises
Exercise 1 โ nsswitch.conf Order
Change the resolution order so that DNS is consulted before /etc/hosts for host lookups.
Answer
Edit /etc/nsswitch.conf and set:
hosts: dns files
Exercise 2 โ resolv.conf Configuration
Configure /etc/resolv.conf to use DNS servers 1.1.1.1 and 8.8.8.8, with search domain example.com.
Answer
nameserver 1.1.1.1
nameserver 8.8.8.8
search example.com
Exercise 3 โ getent vs dig
Look up the address of myserver.local using the full NSS stack, then look it up using DNS only.
Answer
getent hosts myserver.local # uses nsswitch.conf order
getent -s dns hosts myserver.local # DNS only
Or using dig for the DNS-only lookup:
dig myserver.local
Exercise 4 โ host Record Types
Use host to look up the NS records for lpi.org, then the SOA record.
Answer
host -t NS lpi.org
host -t SOA lpi.org
Exercise 5 โ dig Terse Output
Use dig to display only the IP address of wikipedia.org.
Answer
dig +short wikipedia.org
LPIC-1 Study Notes | Topic 109: Networking Fundamentals