Skip to content
LF Cert Trainer

Lesson 104 · Section 9: Networking

Lesson 104: DNS: how Linux turns a name into an address

Name resolution on a plain Linux machine, before Kubernetes adds its own names: /etc/hosts, /etc/resolv.conf, the lookup order in nsswitch.conf, search domains, record types, and the difference between what nslookup, dig and your application see. · 14 min read · 3 min video

1/9DNS
0:00 / 2:36

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why names

In the last lesson, machines found each other by IP address. That works for computers and fails for people. Nobody wants to remember 192.168.1.11, and an address changes when a machine is replaced, while the name db can stay the same for years.

Name resolution is the step that turns a name into an address before any connection is made. It is worth being exact about that order: an application first resolves db to 192.168.1.11, and only then opens a connection to that address. If resolution fails, no packet is ever sent to the target, and no route or firewall is to blame.

This lesson stays on a plain Linux machine. That is on purpose. A container has the same three files as a server, and a Pod resolves a Service name with exactly the mechanism described here. The lesson DNS in Kubernetes then only has to add which names exist.

The hosts file

The oldest and simplest form of name resolution is a text file on each machine.

# /etc/hosts
127.0.0.1      localhost
192.168.1.11   db
192.168.1.20   web web.example.com

Each line is an address followed by one or more names. After adding the second line on host A, ping db and ssh db reach 192.168.1.11.

Two properties of this file are useful to know:

  • The machine believes it without checking. If you write 192.168.1.11 www.google.com, this machine will send traffic for Google to your database server. The file says what the address of a name is, as far as this host is concerned. That makes it a handy tool for tests.
  • It is local. The entry exists on this machine only. With three hosts you can keep the files in step by hand. With three hundred, every new server means editing three hundred files.

A DNS server

The fix for the second problem is to keep the list in one place and let everyone ask. That place is a DNS server: a program that listens on port 53 (UDP, and TCP for large answers) and answers the question "what is the address of this name?".

A machine learns where its DNS server is from /etc/resolv.conf:

# /etc/resolv.conf
nameserver 192.168.1.100

Now a new host is added once, on the server, and every machine can resolve it. The hosts file does not go away. It stays useful for a private name on one machine, and it is still read first.

Line in resolv.confMeaning
nameserver 192.168.1.100A DNS server to ask. Up to three lines, tried in order: the second is used only when the first does not answer
search example.com prod.example.comDomains to append to short names, tried in order
options ndots:1How many dots a name needs before it is tried as it is, ahead of the search list (default 1)
options timeout:2 attempts:2How long to wait for a server and how often to retry

The lookup order

Where a Linux machine looks for a namefilesnot foundasks the nameserversets the orderApplicationcurl http://db/etc/hostslocal list of namesDNS server192.168.1.100:53nsswitch.confhosts: files dns/etc/resolv.confnameserver, search
Where a Linux machine looks for a name. An application asks the system resolver. With the usual order 'files dns', the local hosts file is checked first, and only a miss goes to the DNS server named in resolv.conf.

Parts:

  • Application (curl http://db)
  • /etc/hosts (local list of names)
  • DNS server (192.168.1.100:53)
  • nsswitch.conf (hosts: files dns)
  • /etc/resolv.conf (nameserver, search)

Connections:

  • Application to /etc/hosts: files
  • /etc/hosts to /etc/resolv.conf: not found
  • /etc/resolv.conf to DNS server: asks the nameserver
  • nsswitch.conf to Application: sets the order

What happens when a name is in both places with different addresses? A third file decides, /etc/nsswitch.conf:

# /etc/nsswitch.conf
hosts:          files dns

files means /etc/hosts, and dns means the servers in /etc/resolv.conf. With this usual order the hosts file wins, and DNS is asked only when the file has no entry. Swap the two words and DNS is asked first.

Under the hood · Why does my resolv.conf say 127.0.0.53?

On Ubuntu and other systemd distributions, /etc/resolv.conf often contains nameserver 127.0.0.53. That is not an error. It is systemd-resolved, a small local resolver that listens on the machine itself, caches answers and forwards to the real DNS servers. resolvectl status shows which servers it really uses, and the file /run/systemd/resolve/resolv.conf lists them in the classic format.

This matters for Kubernetes in one place: a Pod must never be given 127.0.0.53 as its upstream DNS server, because inside the Pod that address is the Pod itself. kubeadm detects systemd-resolved and points the kubelet at the real file with the resolvConf setting. If CoreDNS reports a forwarding loop, this is one of the first things to check.

Domain names and search domains

Names in DNS are built from right to left, with dots between the parts:

www.example.com.
 |     |     |  └── the root, usually not written
 |     |     └───── top-level domain
 |     └─────────── the domain
 └───────────────── a host or subdomain inside it

A name written out to the top is a fully qualified domain name (FQDN). A DNS server stores and answers full names only. Inside a company you still want to type web instead of web.example.com, and the search line makes that possible.

A search domain completes short nameswebfullanswersping weba short name/etc/resolv.confsearch example.comDNS serverknows web.example.com192.168.1.20the A record
A search domain completes short names. The application asks for 'web'. The resolver appends each search domain in turn and sends the full name to the DNS server, which only knows full names.

Parts:

  • ping web (a short name)
  • /etc/resolv.conf (search example.com)
  • DNS server (knows web.example.com)
  • 192.168.1.20 (the A record)

Connections:

  • ping web to /etc/resolv.conf: web
  • /etc/resolv.conf to DNS server: full
  • DNS server to 192.168.1.20: answers
# /etc/resolv.conf
nameserver 192.168.1.100
search example.com prod.example.com

With this file, a lookup of web tries web.example.com, then web.prod.example.com, and stops at the first that exists.

The ndots option decides what happens with names that already contain dots. The rule: if the name has fewer dots than ndots, the search domains are tried first and the name as typed is tried last. With the default ndots:1, a name with at least one dot, such as web.prod, is tried as it is first.

A name that ends in a dot is treated as complete, and the search list is skipped entirely: web.example.com. is looked up exactly once.

Record types

A DNS server does not only map names to IPv4 addresses. Each entry is a record with a type.

TypeMapsExampleWhere you meet it in Kubernetes
AA name to an IPv4 addressweb.example.com → 192.168.1.20Every Service name resolves to its ClusterIP
AAAAA name to an IPv6 addressweb.example.com → 2001:db8::20Services in IPv6 and dual-stack clusters
CNAMEA name to another name (an alias)shop.example.com → web.example.comA Service of type ExternalName
SRVA service name to a host and port_http._tcp.web… → 80 web…Named ports of Services
PTRAn address back to a name (reverse lookup)192.168.1.20 → web.example.comReverse lookups of Service and Pod IPs
NSA domain to the servers responsible for itexample.com → ns1.example.comRarely
TXTA name to free textverification stringsRarely

Every record also has a TTL (time to live), in seconds. It tells whoever received the answer how long it may be kept in a cache before asking again.

How a public name is found

How a public name is foundasks once1. who has .com?2.3. www.example.com?Your machinewww.example.com?Your DNS serverresolves and cachesRoot serversknow who runs .com.com serversknow example.comexample.com servershold the records
How a public name is found. Your DNS server does the walking for you: it asks a root server, then the servers of the top-level domain, then the servers of the domain itself, and keeps the answer in its cache.

Parts:

  • Your machine (www.example.com?)
  • Your DNS server (resolves and caches)
  • Root servers (know who runs .com)
  • .com servers (know example.com)
  • example.com servers (hold the records)

Connections:

  • Your machine to Your DNS server: asks once
  • Your DNS server to Root servers: 1. who has .com?
  • Your DNS server to .com servers: 2.
  • Your DNS server to example.com servers: 3. www.example.com?

No single server knows every name on the internet. Responsibility is split along the dots. When your DNS server is asked for www.example.com and has no cached answer, it walks down the tree: a root server says who runs .com, a .com server says who runs example.com, and one of those servers finally holds the record.

Your machine does none of that walking. It asks its configured server once and waits for the final answer. Then both keep it for as long as the TTL allows. Caching is why the second lookup is instant, and also why a changed record can take a while to be seen everywhere.

The same idea runs through a cluster. A Pod asks CoreDNS. CoreDNS answers cluster names itself, and forwards everything else to the DNS servers of the node. So a Pod can resolve both web.shop.svc.cluster.local and www.example.com through one nameserver.

The tools, and what each one really asks

This table is the most practical part of the lesson. The tools do not all resolve names in the same way.

ToolReads /etc/hosts?Uses search domains?Use it to
ping, curl, ssh, your applicationYesYesDo the real thing
getent hosts <name>YesYesSee what an application would get, without connecting
nslookup <name>NoYesAsk the DNS server directly
dig <name>NoNo, unless you add +searchRead the full DNS answer in detail
host <name>NoYesA short form of nslookup

So if ping db works and nslookup db says NXDOMAIN, nothing is broken: the name is in the hosts file and not on the DNS server.

Two answers from a DNS tool are worth telling apart:

  • NXDOMAIN: the server answered, and the name does not exist. The server is reachable. The name is wrong, or the search list did not complete it the way you expected.
  • connection timed out; no servers could be reached: nobody answered on port 53. This is a network problem or a wrong nameserver address, not a naming problem.

Using it

Example · Give a machine a private name for a test

You want curl http://shop.example.com on this host to reach a test server at 192.168.1.50, without touching the DNS server.

echo "192.168.1.50 shop.example.com" | sudo tee -a /etc/hosts
getent hosts shop.example.com
192.168.1.50    shop.example.com

getent confirms what applications will now see. nslookup shop.example.com would still show the public address, because it does not read the file. Remove the line when you are done: forgotten hosts entries are a classic source of "it only fails on this machine".

Example · Point a machine at a DNS server and add a search domain

cat /etc/resolv.conf
nameserver 192.168.1.100
search example.com
nslookup web
Server:         192.168.1.100
Address:        192.168.1.100#53

Name:   web.example.com
Address: 192.168.1.20

The Server line tells you who answered, and the Name line shows the full name that was found, with the search domain added. Read both every time: they tell you which server and which name were really used.

Example · Read a full answer with dig

dig web.example.com
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41873
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; QUESTION SECTION:
;web.example.com.               IN      A

;; ANSWER SECTION:
web.example.com.        3600    IN      A       192.168.1.20

;; Query time: 1 msec
;; SERVER: 192.168.1.100#53(192.168.1.100) (UDP)

Four things to read: status (NOERROR or NXDOMAIN), the question (the exact name and type that was asked), the answer (name, TTL, type, value), and the server that replied. Useful variations:

dig +short web.example.com          # only the address
dig @8.8.8.8 www.example.com        # ask a specific server
dig web.example.com AAAA            # ask for another record type
dig -x 192.168.1.20                 # reverse lookup (PTR)
dig +search web                     # apply the search list, like other tools

Example · Look at name resolution from inside a Pod

The same files exist in every container. Three commands show the whole setup of a Pod:

kubectl exec -it cart -- cat /etc/resolv.conf
kubectl exec -it cart -- cat /etc/hosts
kubectl exec -it cart -- nslookup web
search shop.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5

# Kubernetes-managed hosts file.
127.0.0.1       localhost
10.244.1.7      cart

Server:         10.96.0.10
Address:        10.96.0.10:53

Name:   web.shop.svc.cluster.local
Address: 10.96.14.20

The nameserver is the cluster's DNS Service, the search list starts with the Pod's own namespace, and the hosts file is written by the kubelet with the Pod's own name. Everything you learned on this page applies unchanged.

Tricks

Common mistakes

  • Expecting nslookup or dig to see /etc/hosts. They ask the DNS server only. Use getent hosts to test what applications see.
  • Listing two nameservers that know different names. The second is only used when the first does not answer at all. An NXDOMAIN from the first is final.
  • Reading NXDOMAIN as "DNS is down". It means the opposite: a server answered. The name, or the search list, is wrong.
  • Editing /etc/resolv.conf by hand on a machine that generates it. With systemd-resolved, NetworkManager or DHCP, the file is rewritten and your change disappears. Change the tool's configuration instead.
  • Forgetting old entries in /etc/hosts. They win over DNS, silently, long after the test they were added for.
  • Blaming the network when a name fails. Resolution happens before the connection. Test the IP address directly: if that works, the problem is naming and nothing else.

Recap

  • Resolution comes before the connection: name to address first, then packets.
  • /etc/hosts is a local list that is believed without checks. /etc/resolv.conf names the DNS nameserver and the search domains. /etc/nsswitch.conf sets the order, usually files dns.
  • Search domains complete short names. ndots decides when they are tried first. A trailing dot skips them.
  • Records have types: A and AAAA for addresses, CNAME for aliases, SRV for name and port, PTR for reverse lookups.
  • nslookup and dig ask the DNS server and ignore the hosts file. getent hosts resolves like an application.
  • A Pod resolves names the same way, with a resolv.conf written by the kubelet that points at CoreDNS.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.