DNS · Computer Networks
How does a name like shop.example.com turn into an address?
Course contentsWhy you need it
People use names; networks use numbers. DNS is the internet's phone book that translates one into the other, and it's involved in almost every request you make. "It's always DNS" is a running joke among engineers because so many outages start there.
The intuition
When you visit shop.example.com, your computer asks a DNS resolver: "what's the A record for this name?" The answer is an IPv4 address, with a TTL saying how long it may be cached.
Common record types:
| Type | Maps a name to… |
|---|---|
| A | an IPv4 address |
| AAAA | an IPv6 address |
| CNAME | another name (an alias) |
| MX | the mail server for the domain |
| TXT | free text, often used to prove you own a domain |
In a terminal, dig NAME asks DNS directly, and curl URL makes an HTTP request.
Definition
DNS record
One entry in DNS: a name, a type (A, CNAME, MX…), a value and a TTL.
TTL
Time to live: how many seconds a resolver may cache an answer. After changing a record, old answers can linger until the TTL runs out.
Examples
Ask DNS, then ask the server
Run it in the lessondig finds the address; curl fetches the page.
dig +short shop.example.com
dig shop.example.com
curl https://shop.example.com- L1+short prints just the answer.
203.0.113.10 - L2The full answer: name, TTL (300 s), type A, address.
- L3With the address known, the HTTP request returns the page.
<h1>Shop</h1>
A name that doesn't exist
Run it in the lessonA typo in the name: DNS has no record, so curl can't even start.
dig +short shopp.example.com
curl https://shopp.example.comThis example stops with an error on purpose: reading the message is the point.
- L1No answer at all.
- L2Could not resolve host: the failure is in DNS, not the server.
Common mistakes
Expecting instant changes
After you change a record, resolvers keep the old answer until its TTL expires. Lower the TTL a day before a planned move.
Blaming the server first
"Could not resolve host" means DNS failed; the server was never contacted. Check the name with dig before restarting anything.
Quick quiz
1. Which record type points www.example.com at another name, example.com?
Show the answer
Answer: A
A CNAME is an alias: one name pointing at another.
2. What does this print?
dig +short api.example.com
dig +short mail.example.comShow the answer
Answer: A
Each name has its own A record.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictRead the answers
Type exactly what these commands print.
- ModifyFix the typo
This health check fails before it reaches the server. Find out why and fix the command so it prints the health check's JSON.
- WriteCheck before you fetch
Print the address of
shop.example.com(just the address), then fetch only the headers ofhttps://shop.example.comwithcurl -I.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| DNS | 域名系统 | The system that turns names like example.com into IP addresses. |
| Resolver | 解析器 | The DNS server that looks names up for your computer. |
| TTL | 生存时间 | How long an answer may be cached. |