Sub topic 1
Why names exist: IP addresses and the need for DNS
Prerequisites
This chapter assumes you can do the following:
- Explain what a website and a web browser are, at the level of "the browser requests a page from a server".
- Open a terminal and run a command.
- Recognize an IP address such as
192.0.2.10as a number that identifies a machine on a network.
You don't need to know how DNS works internally. This chapter explains the problem DNS solves. The rest of the tutorial covers how it solves it.
What an IP address does
Computers on the Internet find each other with IP addresses. An IP address is a numeric identifier that routers use to deliver packets to the right network interface. When your browser fetches a page, it opens a connection to an address and port. It does not connect to a name.
There are two versions in use.
| Property | IPv4 | IPv6 |
|---|---|---|
| Size | 32 bits | 128 bits |
| Written as | Four decimal numbers, `192.0.2.10` | Eight groups of hex digits, `2001:db8::10` |
| Total addresses | About 4.3 billion | About 3.4 × 10^38 |
| DNS record that holds it | A | AAAA |
IPv6 notation lets you shorten a run of zero groups to ::, but only once per address. That is why 2001:db8::10 is the same as 2001:0db8:0000:0000:0000:0000:0000:0010.
The addresses in this tutorial come from ranges reserved for documentation. 192.0.2.0/24 and 2001:db8::/32 are never assigned to real hosts, so examples that use them can't point at someone's real server.
Why raw addresses are a poor interface
You could browse the Web entirely by typing addresses. Four problems make that impractical.
- People remember words better than numbers.
example.comis easier to recall, type, and print on a business card than a 32-bit or 128-bit number. - Addresses change. A site moves to a new hosting provider, a company renumbers its network, or a service is rebuilt in another region. If users had stored the old address, every bookmark and link would break. A name stays stable while the address behind it changes.
- One service can have many addresses. A large site may run from dozens of servers. It may also have both IPv4 and IPv6 addresses. A name can map to all of them, and the answer can differ by location or load.
- One address can host many sites. A single server often serves hundreds of websites. The address alone doesn't say which site you want. The name travels with the request (in the HTTP
Hostheader and in the TLS handshake) so the server can pick the right site. Later chapters return to this.
The common thread is indirection. A name is a level of indirection between what people use and where the service currently runs.
The first solution: a shared file
In the early ARPANET, the mapping from names to addresses was a single text file called HOSTS.TXT. One organization, the Stanford Research Institute's Network Information Center, maintained it. Every site downloaded a fresh copy periodically.
That worked while the network had a few hundred machines. It failed as the network grew:
- Traffic. Every host fetched the whole file from one place.
- Delay. A new machine was unreachable by name until the next release of the file and the next download.
- Name collisions. With one flat list, two organizations could not use the same name, and one party had to arbitrate.
- Consistency. Different sites held different versions of the file at the same time.
The idea of a central file is still alive on every machine you use. Your operating system has a local hosts file that is checked before any network lookup.
| Operating system | Location |
|---|---|
| Linux and macOS | `/etc/hosts` |
| Windows | `C:\Windows\System32\drivers\etc\hosts` |
A typical entry maps an address to one or more names:
127.0.0.1 localhost
192.0.2.10 staging.example.comDevelopers use this to test a site on a new server before changing public DNS. The entry applies only to that one machine. It is a useful trick and a classic source of confusion later. If a site works on your laptop but not for anyone else, a forgotten hosts entry is a likely cause.
DNS: a distributed, hierarchical database
The Domain Name System (DNS) replaced HOSTS.TXT in the 1980s. It is defined mainly in RFC 1034 and RFC 1035. Its design answers each failure of the shared file:
- Distributed. No single machine holds all the data. Each organization runs servers that answer for its own names.
- Hierarchical. Names are organized as a tree.
www.example.comsits underexample.com, which sits undercom. Whoever controls a branch decides everything below it, so no central authority has to approve each new name. - Cached. Answers are reused for a limited time, which keeps load low and responses fast.
- Delegated. A parent hands responsibility for a branch to another set of servers.
DNS maps names to data. The most common data is an IP address, but it can also be a mail server, a text string, or an alias to another name. Those record types get their own chapter. For now, treat DNS as a lookup service: you give it a name and a record type, and it returns the matching data.
The lookup happens before the connection. If DNS fails, the web server is never contacted, even if it is running fine. This ordering matters for troubleshooting. "The site is down" and "the name doesn't resolve" are different problems.
Seeing the lookup yourself
You can watch name-to-address mapping from a terminal. The dig tool queries DNS directly. Each of these asks for one record type of example.com, and +short prints only the answer values.
dig example.com A +short
dig example.com AAAA +short
dig example.com MX +short
dig example.com NS +shortThe first command returns IPv4 addresses, the second returns IPv6 addresses, the third returns mail servers, and the fourth returns the name servers responsible for the domain. The actual values you see depend on the live data for the domain when you run them. Some record types may return nothing for some names.
Programs do not usually call dig. They ask the operating system to resolve a name, and the OS decides how: it can check the hosts file, a local cache, and then the configured DNS servers. In Python, that call is socket.getaddrinfo:
import socket
for family, _, _, _, sockaddr in socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM):
print(family.name, sockaddr[0])Each line of output pairs an address family (AF_INET for IPv4, AF_INET6 for IPv6) with an address. A name with both kinds of records gives you both. Real programs try these addresses in turn, or race them, until one connects.
The two tools can disagree. dig talks to DNS only and ignores your hosts file. getaddrinfo follows the OS rules, so it sees the hosts entry. If the two give different answers for the same name, something local is overriding DNS.
What DNS does not do
DNS is often blamed for problems that belong elsewhere. These limits prevent confusion.
- It does not carry web traffic. DNS returns an address. The browser then connects to that address using HTTP over TCP or QUIC, which is a separate exchange.
- It does not check that a server is up. A name can resolve to an address where nothing is listening. Resolution succeeds, then the connection fails or times out.
- It does not choose ports. The browser assumes 443 for HTTPS and 80 for HTTP unless the URL says otherwise. Some newer record types can carry service hints, but the plain A and AAAA records only hold addresses.
- It is not private by default. Classic DNS queries travel in plain text, usually over UDP port 53. Anyone on the path can read which names you look up. Encrypted transports exist and are covered later.
- It is not instant everywhere. Answers are cached at several levels, so a change you make may take time to be seen. The chapter on TTL explains why.
How this chapter connects to the rest
The remaining chapters build on the idea that a name is an indirection to an address. The table shows where each question is answered.
| Question | Chapter topic |
|---|---|
| How is a name like `www.example.com` built from parts? | Anatomy of a domain name |
| Who answers for which part of the tree? | The DNS hierarchy, then name servers and zones |
| How does a program get from a name to an answer? | Resolution step by step |
| What kinds of data can a name hold? | Core record types |
| Why does a change take time to appear? | TTL and caching |
| Where do the addresses come from in the first place? | Hosting types and connecting a domain to a host |
Summary
IP addresses are what the network uses to deliver packets, but they are hard to remember, change over time, and don't say which site on a shared server you want. Names add a stable layer of indirection. The old shared HOSTS.TXT file broke down as the network grew, and DNS replaced it with a distributed, hierarchical, cached database. A lookup always comes first, then the connection. When something fails, keep the two steps separate: did the name resolve, and did the connection to the resulting address work?
Exercise
A site loads for you but not for a colleague. dig staging.example.com A +short on your machine returns nothing, yet your browser still reaches the site. Name one likely cause, and the command or file that would confirm it.
Show answer
Your machine probably has a hosts file entry mapping staging.example.com to an address. dig queries DNS only and ignores that file, while the browser uses the operating system resolver, which checks hosts first. Open /etc/hosts (or the Windows hosts file) to confirm. Running ping staging.example.com or the getaddrinfo script would also show the address that the OS resolver returns.
Exercise
Why can a single IP address serve many websites, and what tells the server which site the client wants?
Show answer
A server can host many sites on one address because the client sends the name it wants along with the request. In HTTP this is the Host header, and in TLS it is the server name indication in the handshake. The IP address only gets the packets to the machine. The name picks the site.
Exercise
What are two reasons the shared HOSTS.TXT file stopped working as the network grew?
Show answer
Any two of the following. Every host had to download the whole file from one place, which created heavy traffic. New machines were unreachable by name until the next release. Names lived in one flat list, so collisions needed central arbitration. Different sites held different versions at the same time.