Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Getting an address and a name (DHCP, DNS)

After this lesson: I can explain how a device gets an address and finds a name.

45 min lesson5 commandsDHCP and DNS lab: configure dnsmasq for a small network in your browser

Joining the café Wi-Fi

You sit down in a café and join its Wi-Fi. Within a second your phone has an address. Then you type a website's name and the page opens.

You never typed a number. Two small services did it for you.

One lends your phone an address. The other turns a name into an address.

By the end of this lesson

Note

I can explain how a device gets an address and finds a name.

Key words

WordWhat it meansExample
DHCPthe service that lends addresses to devices that ask
Leasean address lent for a set time
Broadcasta message sent to every device on the network
Hardware addressthe fixed number of a network card, also called a MAC address02:00:00:00:00:01
DNSthe service that turns a name into an address
Recordone fact a DNS server holds about a name

The cells students fill in are left blank in this preview.

A new device has to ask

A device that has just joined has no address. It does not know where the DHCP server is either.

So it sends a broadcast to everybody on the network. The DHCP server hears it and offers an address.

It takes four messages, often called DORA: Discover, Offer, Request, Acknowledge.

Worked example: the four messages

MessageSent byWhat it means
DiscoverThe phoneIs there a DHCP server here?
OfferThe serverYou can have 192.168.5.101
RequestThe phoneYes please, I will take it
AcknowledgeThe serverIt is yours for 12 hours

The cells students fill in are left blank in this preview.

Meet the server and two clients

Command
dnslab status

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make pc1 ask for an address

Command
sudo dnslab renew pc1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read pc1's hardware address

Command
sudo ip netns exec pc1 ip -o link show eth0

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Nobody answered pc1

The status shows server with 10.0.10.1/24. Under pc1 and pc2 there is no address at all.

The renew waits about three seconds. Then it says pc1 was given nothing: no DHCPOFFER came back. The Discover went out, and no Offer came.

The last Run shows link/ether 02:00:00:00:00:01. That is how a server tells pc1 from pc2.

Read the server's settings file

Command
cat /etc/dnsmasq.d/lab.conf

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

DNS turns a name into an address

People remember names. Routers need numbers. A DNS server holds records that join the two.

Your phone asks its DNS server, "what is the address of this name?". The server answers from its records, or asks another server.

DHCP is what tells your phone which DNS server to ask.

Worked example: records in a café

Record typeIt answersExample
AWhich address has this name?till.cafe is 192.168.5.10
AAAAWhich IPv6 address has this name?A long number with colons
CNAMEIs this name another name?menu.cafe is the same as web.cafe
MXWhich server takes the mail?mail.cafe

The cells students fill in are left blank in this preview.

Ask the server for a name

Command
sudo ip netns exec server dig +noall +comments @10.0.10.1 files.lab

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The server refused

The answer has status: REFUSED and ANSWER: 0. The server has no record for that name yet, and it cannot reach another server to ask.

Writing the records is part of the lab. After it, the same question has an answer.

A common mistake

Warning

Many people think DHCP and DNS are one service. They do two different jobs. DHCP lends addresses. DNS answers questions about names. One program can do both, as dnsmasq does in this lab, and they are still two jobs.

Going further: what a lease holds

When your lab works, read the server's real lease file: sudo cat /run/dnslab/dnsmasq.leases.

Each line has more fields than the assignment's. Match each field to what it means. Which one is the time the lease ends?

About this lab and its source: DHCP and DNS lab: configure dnsmasq for a small network in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.