Home Blog Certs Knowledge Base About

LPIC-1 109.4 โ€” Configure Client Side DNS

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:

NotationMeaning
[!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
DirectiveDescription
nameserver IPDNS server to use (up to 3 entries)
search DOMAINAppend domain for short name lookups (up to 6)
domain DOMAINLocal domain name (mutually exclusive with search)
options KEY:VALUEResolver 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
OptionDescription
-t TYPERecord type: A, AAAA, MX, NS, SOA, PTR, etc.
Last argumentIf 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

SectionContents
HeaderQuery metadata (opcode, status, flags, counts)
QuestionThe query that was sent
AnswerResource records returned
AuthorityNS records for the authoritative zone
AdditionalA/AAAA records for servers in authority section

dig Options

OptionDescription
-t TYPERecord type to query
+shortSuppress all output except the result
+noallDisable all output sections
+answerRe-enable the answer section
+questionRe-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

  1. What file controls the order in which name databases are consulted? โ†’ /etc/nsswitch.conf
  2. 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.
  3. How many nameserver entries can appear in /etc/resolv.conf? โ†’ Up to 3.
  4. How many search domains can be configured in /etc/resolv.conf? โ†’ Up to 6.
  5. What happens if both search and domain appear in /etc/resolv.conf? โ†’ They are mutually exclusive; the last one in the file takes effect.
  6. What is the correct format of an /etc/hosts entry? โ†’ IP address in the first column, followed by hostname and optional aliases: IP hostname [alias]
  7. On what address does systemd-resolved listen? โ†’ 127.0.0.53
  8. What nsswitch.conf source keyword enables systemd-resolved? โ†’ resolve
  9. What is the key difference between getent and host/dig? โ†’ getent uses the full NSS resolver stack (respects nsswitch.conf); host and dig query DNS directly.
  10. What getent option forces a lookup using /etc/hosts? โ†’ -s files
  11. What does host wikipedia.org return by default? โ†’ A, AAAA, and MX records.
  12. What host option specifies the DNS record type to query? โ†’ -t TYPE
  13. How do you use host to query a specific name server? โ†’ Pass the server IP or hostname as the last argument: host NAME server.
  14. What dig option shows only the IP address result? โ†’ +short
  15. What file stores default dig options? โ†’ ~/.digrc
  16. What dig combination shows only the question and answer sections? โ†’ dig +noall +answer +question NAME
  17. What does dig -t MX lpi.org do? โ†’ Queries for the MX (Mail Exchanger) records for lpi.org.
  18. Why might manual changes to /etc/resolv.conf not 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