Sub topic 1
Why names exist: IP addresses and the need for DNS
Prerequisites
This chapter assumes you know three things:
- What a website and a web browser are.
- How to run commands in a terminal.
- That every device on a network has some kind of address.
Nothing else is needed. The chapter sets up the problem that the rest of the tutorial solves.
IP addresses: how machines find each other
Computers on a network do not locate each other by name. They use an IP address, a number that identifies a network interface so packets can be routed to it. Two versions are in use:
| Version | Size | Example | Notes |
|---|---|---|---|
| IPv4 | 32 bits | `192.0.2.10` | Four decimal numbers from 0 to 255. About 4.3 billion possible values. |
| IPv6 | 128 bits | `2001:db8::10` | Eight groups of hex digits, with a run of zeros shortened to `::`. |
The examples above come from ranges reserved for documentation, so they are safe to use in tutorials. They are not real servers.
IPv4 addresses ran short, so IPv6 was created. Most sites and networks now run both. That is called dual stack. A single name can therefore map to several addresses of both kinds.
Why addresses are a poor interface for people
Imagine typing 203.0.113.77 to reach your bank. There are three problems:
- Numbers are hard to remember and easy to mistype.
- Addresses change. If you move a site to a new server, you get a new address, and every visitor would need to learn it.
- One service may run on many servers. A large site spreads traffic across machines in different countries. A single typed number cannot express that.
Names fix all three. A name stays the same while the address behind it changes. Someone who owns the name can repoint it whenever they like.
The early solution and why it stopped working
Early networks used one shared file, HOSTS.TXT. It listed every name and its address. One organization edited it, and everyone downloaded copies. This broke as the network grew. The file got large, copies went stale, and two people could not both claim the same name.
You can still see the local descendant of that file on your own machine. On Linux and macOS it is /etc/hosts. The system checks it before asking the network.
What DNS is
The Domain Name System (DNS) replaced the single file. It is a distributed, hierarchical database that maps names to data, mostly IP addresses. Three ideas make it scale:
- Distributed. No single server holds everything. Each organization runs servers for its own names.
- Hierarchical. Names are organized as a tree, so responsibility can be handed down level by level. Later chapters cover this in detail.
- Cached. Answers are remembered for a limited time, so most lookups never leave your local network.
DNS does a job before almost every connection. Before your browser opens a connection to example.com, something must turn that name into an address.
Seeing a lookup yourself
The dig tool asks DNS a question and prints the answer. The +short option prints only the answer values:
dig example.com A +short
dig example.com AAAA +shortA asks for IPv4 addresses and AAAA asks for IPv6 addresses. The values you get can differ from what you expect, because the operator of a name can change them.
Programs do the same thing before they connect. This Python script asks the operating system's resolver for the addresses of a name:
import socket
for family, _, _, _, sockaddr in socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM):
print(family.name, sockaddr[0])Each line shows an address family, AF_INET for IPv4 or AF_INET6 for IPv6, and one address. The script needs network access and may print several lines.
Common misunderstandings
- A name is not a website. A name only maps to data, usually an address. The web server at that address is a separate thing.
- One name can have many addresses. Clients may try them in order or pick one.
- Names can exist without a website. Many names serve only email or other services.
What comes next
The next chapter breaks a domain name into its parts: labels, top-level domains, and subdomains. That structure is what makes the hierarchy possible.
Exercise
A company moves its website to a new server with a different IP address. Explain why visitors who type the site's name do not need to change anything, and name the system that makes this possible.
Show answer
Visitors type a name, and the name stays the same. The company updates the DNS data so the name maps to the new IP address. Browsers ask DNS for the address before connecting, so they learn the new address automatically. The Domain Name System makes this possible.
Exercise
Which dig command would you run to list only the IPv6 addresses for example.com, and what do you expect to see?
Show answer
Run dig example.com AAAA +short. The output is a list of IPv6 addresses, one per line, such as 2001:db8::10 in the documentation range. It is empty if the name has no IPv6 address.