Everyone Can Learn Program
中文 Start learning
Core · NetworksLesson 2 of 6Free to read

DNS · Computer Networks

How does a name like shop.example.com turn into an address?

Course contents

Why 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:

TypeMaps a name to…
Aan IPv4 address
AAAAan IPv6 address
CNAMEanother name (an alias)
MXthe mail server for the domain
TXTfree 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 lesson

dig finds the address; curl fetches the page.

dig +short shop.example.com
dig shop.example.com
curl https://shop.example.com
Step by step
  1. L1+short prints just the answer.203.0.113.10
  2. L2The full answer: name, TTL (300 s), type A, address.
  3. L3With the address known, the HTTP request returns the page.<h1>Shop</h1>

A name that doesn't exist

Run it in the lesson

A typo in the name: DNS has no record, so curl can't even start.

dig +short shopp.example.com
curl https://shopp.example.com

This example stops with an error on purpose: reading the message is the point.

Step by step
  1. L1No answer at all.
  2. 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?

  1. ACNAME
  2. BMX
  3. CA
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.com
  1. A
    203.0.113.20
    203.0.113.30
  2. B
    203.0.113.30
    203.0.113.20
  3. C
    203.0.113.10
    203.0.113.20
Show 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.

  1. PredictRead the answers

    Type exactly what these commands print.

  2. 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.

  3. WriteCheck before you fetch

    Print the address of shop.example.com (just the address), then fetch only the headers of https://shop.example.com with curl -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

TermChineseDefinition
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.