How names become addresses: DNS
Almost every connection starts with a question: which address belongs to this name? The Domain Name System answers it through resolvers, caches and the servers each domain's owner runs, and you can ask it yourself from the terminal.
Names and addresses
When you call a friend, you tap their name and your phone dials the
number saved under it. The internet works the same way. People
remember names such as example.com, but computers connect
to numeric addresses. The Domain Name System, or
DNS, is the shared directory that turns one into the
other.
Clients, servers, and addresses
introduced it and showed how a name's labels are read from the end.
Behind that directory is a chain of servers that each know one part
of the answer, and you can ask them yourself from the terminal.
The numbers are IP addresses, and they come in two
versions. An IPv4 address is four numbers from 0 to
255 separated by dots, such as 203.0.113.10. That allows
about four billion addresses, which the internet has outgrown.
An IPv6 address is much longer: eight groups of four
hexadecimal digits (0 to 9 and a to f) separated by colons. Leading
zeros in a group can be dropped, and one run of all-zero groups can be
written as ::, so
2001:0db8:0000:0000:0000:0000:0000:0010 is usually written
2001:db8::10. Many hosts have addresses of both kinds. The
addresses in this lesson come from ranges set aside for documentation,
so they belong to no real computer.
A name is not tied to a single address. A busy site can publish several addresses for one name, and the client picks one. Many names can also share one address, such as the sites a hosting company serves from the same machines. That is why a web request also states which site it wants, as Connections, ports and speaking HTTP by hand shows.
In www.example.com, com is a
top-level domain (TLD), example.com is a
domain registered under it, and www is a name inside that
domain. Above them all sits the root, written as a
final dot that is usually left off: www.example.com.
Different organizations look after each level, and a lookup follows
that same structure to find an answer.
Asking a resolver
No single computer holds every name. When a program needs an address, a small piece of the operating system called the stub resolver passes the question to a recursive resolver. That resolver is usually provided by the network you are on: your home router forwards to one at your internet provider, a workplace runs its own, and public resolvers exist that anyone can use.
The recursive resolver does the legwork. If it does not already know
the answer, it starts at the top. A root server does
not know the address of www.example.com, but it knows
which servers handle .com. A .com server
knows which servers handle example.com. Those are the
authoritative servers for the domain: they hold the
records the domain's owner published, and they give the final answer.
The final answer is not always an address. If the owner published no
such name, the authoritative server says so with
NXDOMAIN, short for "non-existent domain", and the
resolver passes that on. A program that receives it stops before it
tries to connect, because there is nowhere to connect to. If the
resolver cannot get any answer, for example because the domain's
servers do not reply, it reports SERVFAIL instead.
Walking the whole tree for every question would be slow, so resolvers
remember what they learn. Every answer carries a
TTL, short for time to live: the number of seconds it
may be kept. Within that time, the resolver answers the same question
from its cache without asking anyone, and the answer
arrives almost instantly. It also remembers the referrals, so even a
new name under .com does not need the root again. Your
operating system and your browser may keep caches of their own.
The TTL is a trade-off chosen by the domain's owner. A long TTL means fewer lookups, but when the owner changes a record, a resolver that cached the old one keeps handing it out until its TTL runs out. Nothing tells the resolver that the record changed; it asks again only when its copy expires. That is why a DNS change can work for you and not yet for a colleague whose resolver cached the old answer. Owners often lower the TTL a day before a planned move, so the change spreads quickly.
Record types
DNS holds more than addresses. Each record has a name, a type, a TTL and a value, and the type says what kind of value it is. These are the types you will meet most often:
| Type | Holds | Example value |
|---|---|---|
A | An IPv4 address | 203.0.113.10 |
AAAA | An IPv6 address | 2001:db8::10 |
CNAME | An alias: this name is another name, and the resolver continues with that one | photos-cdn.example.net. |
NS | The authoritative name servers for a domain | ns1.example.com. |
MX | The servers that receive email for a domain, with a preference number | 10 mail.example.com. |
TXT | Free text, often used to prove control of a domain or publish email policies | "site-verification=7c1e9a" |
SRV | Where a named service runs: a priority, a weight, a port and a host | 0 5 389 dc1.corp.example.com. |
CAA | Which certificate authorities may issue certificates for the domain | 0 issue "ca.example.net" |
A CNAME is common when a company hands part of its site
to another provider. photos.example.com might be an alias
for a name the provider controls, so the provider can change its
addresses without asking the company to update anything.
TXT records matter in identity work. Before a service
trusts a domain for something important, such as accepting sign-ins
for everyone with an email address at that domain, it often asks the
owner to publish a TXT record with a value it chose. Only
someone who controls the domain's DNS can do that, so the record proves
control.
An SRV record lets a program find a service by name
rather than by host: a computer joining a company network asks for
_ldap._tcp.corp.example.com and learns which hosts run
the company directory, and on which port. A CAA record
says which certificateA signed statement that a public key belongs to a name, such as a website's hostname, for stated purposes and a limited period. A certificate authority signs it, so software that trusts the authority can connect the key to the name. authorities may issue certificates for a domain.
These authorities are the organizations that issue the certificates
HTTPSHTTP protected by TLS, so traffic is encrypted and checked for changes while it travels. It protects the connection, but does not prove that the site at the other end is honest. relies on. They check the record before issuing one, so a domain
that lists a single authority makes a mistaken or fraudulent certificate
from any other harder to obtain.
Looking up names with dig
dig sends a DNS question and shows the whole answer. By
default it asks for an A record:
dig www.example.comExample output, shortened:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41207
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; QUESTION SECTION:
;www.example.com. IN A
;; ANSWER SECTION:
www.example.com. 300 IN A 203.0.113.10
;; Query time: 24 msec
;; SERVER: 192.0.2.53#53(192.0.2.53) (UDP)Read it from the top:
-
statusis the result:NOERRORwhen the question was answered,NXDOMAINorSERVFAILwhen it was not. -
flagsdescribe the reply.qrmarks it as a response,rdmeans you asked for a recursive lookup,rameans the server offers one, andaa, when present, means the answer came straight from an authoritative server rather than a cache. -
The
ANSWER SECTIONholds the records, in columns: name, TTL in seconds, class (IN, for internet, on nearly every record), type and value. -
Query timesays how long the answer took.SERVERnames the resolver that gave it, then#53, the port DNS servers listen on, and how the question travelled. UDP sends a single message to a port without first setting up a connection, which suits one short question and its answer. When an answer is too large for one message, dig asks again over TCP, the connection-based alternative.
Run the same command again and the query time usually drops close to zero and the TTL is lower, because the answer now comes from a cache that is counting down.
A few variations cover most needs. +short prints only the
values. A type after the name asks for that type instead of
A. And @ followed by a server sends the
question to that server instead of your usual resolver:
dig +short www.example.com
dig www.example.com AAAA
dig example.com MX
dig +short example.com NS
dig @1.1.1.1 www.example.com
@ also works with an authoritative server. Find the
domain's servers with the NS query, then ask one of them
by name. Its reply has the aa flag, because it is the
source of the record rather than a copy, and its TTL is the full value
the owner set.
The SERVER line shows where your questions normally go.
You can also look it up directly:
Ubuntu 24.04 (Windows with WSL, or Linux)
grep nameserver /etc/resolv.conf
On Ubuntu installed directly on a computer, this usually shows
127.0.0.53, a resolver running on the computer
itself that caches answers and passes questions on to the
network's resolver. In WSL it usually shows an address that WSL
set up to pass questions to Windows.
macOS 14 or later with Homebrew
scutil --dns | grep nameserver
scutil --dns lists the resolvers macOS uses, often
your router's address, such as 192.168.1.1. macOS
also has an /etc/resolv.conf file, but most programs
do not read it.
How programs find addresses
When curl or a browser needs an address, it does not run
dig. It asks the operating system, and the operating
system first checks a plain text file called the
hosts file, at /etc/hosts on both Linux
and macOS. Each line holds an address followed by one or more names.
Typical lines look like this:
127.0.0.1 localhost
::1 localhost
localhost always means your own computer, and the hosts
file is where that comes from. A line such as
203.0.113.10 photos.example would make that name lead to
that address on this computer only, without DNS being asked.
Developers use this to try a site before its DNS is ready. Editing the
file needs administrator rights, and a forgotten line is easy to miss.
That is also why one computer can reach the wrong server while
dig shows the right answer. dig never reads
the hosts file itself: it sends a DNS question to the resolver named in
your settings, while other programs ask the operating system, which
checks the hosts file first. On native Ubuntu that resolver is the
local stub at 127.0.0.53, which keeps its own cache and
can answer from the hosts file too, so there
dig @1.1.1.1 is the way to ask a DNS resolver directly.
To see the address your programs actually get, ask the system the same way they do:
Ubuntu 24.04 (Windows with WSL, or Linux)
getent hosts www.example.com
getent prints one address followed by the name, such
as 2001:db8::10 www.example.com. When a name has
both kinds of address, it shows the IPv6 one.
macOS 14 or later with Homebrew
dscacheutil -q host -a name www.example.comExample output:
name: www.example.com
ipv6_address: 2001:db8::10
name: www.example.com
ip_address: 203.0.113.10
When a name cannot be found at all, programs say so before they try
to connect. The top-level domain .invalid is reserved so
that it never exists, which makes it safe to try:
$ curl https://photos.invalid/
curl: (6) Could not resolve host: photos.invalid
When a name leads somewhere unexpected, compare the two views:
dig for what DNS says, and getent hosts or
dscacheutil for what your programs receive. If they
disagree, look at the hosts file and at caches. Browsers add one more
possibility: many can send their DNS questions over HTTPS to a
resolver of their own choosing, so a browser can get a different
answer from getent on the same computer.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Look up the practice service with dig and report its addressesFind the resolver your computer asks, read dig's answers for the practice service, loop over record types, ask the authoritative server, then report the addresses DNS gave you.