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

Free lesson

Handing out addresses and names (DHCP, DNS)

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

55 min lesson6 commandsDHCP and DNS lab: configure dnsmasq for a small network in your browser

Plug in and everything just works

A new starter plugs a laptop into the wall socket and opens the company web page. Nobody typed an address. Nobody typed a server.

Two small services did that in under a second. One lends the laptop an address. The other turns a name into a number.

This lesson opens both.

By the end of this lesson

Note

I can explain how a host gets an address and a name.

Key words

WordWhat it meansExample
DHCPthe service that lends addresses to devices that ask
Leasean address lent for a set time12 hours
Scopethe range of addresses a DHCP server may lend10.0.10.100 to 10.0.10.150
Reservationa fixed address always given to one device, found by its hardware address
DNSthe service that turns a name into an address
Recordone fact that a DNS server holds about a name

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

A device with no address asks for one

A new device has no address yet, and it does not know where the server is. So it sends its first message to everybody on the link. This is a broadcast.

A DHCP server answers. The two talk in four steps, which people remember as Discover, Offer, Request and Acknowledge.

Worked example: the four steps

StepSent byWhat it says
DiscoverThe deviceIs there a DHCP server here?
OfferThe serverYou may have this address
RequestThe deviceI would like that address, please
AcknowledgeThe serverIt is yours until this time

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.

What the answers say

The first Run shows that server has an address and that pc1 and pc2 have none.

The second Run waits about three seconds. It then prints pc1 was given nothing: no DHCPOFFER came back. The Discover went out, and no Offer returned.

An address is lent, not given

A lease lasts a set time. The device must ask to renew it before it ends. If it vanishes, the address returns to the pool.

The scope is the pool the server may lend from. A reservation ties one fixed address to one hardware address, so a printer always gets the same one.

Worked example: a lease file line

FieldExampleMeaning
Expiry1761300000When the lease ends, as seconds since 1970. Zero means never
Hardware address02:00:00:00:00:0aWho holds the lease
Address10.0.10.101What was lent
Nametill1The name the device gave, or a star
Client id01:02:00:00:00:00:0aA second label, or a star

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

Read a client'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.

Read the server's configuration 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.

Why the server lends nothing

The first Run prints a line that includes link/ether 02:00:00:00:00:01. That hardware address is how the server tells pc1 from pc2.

The second Run prints three lines of comments and nothing else. With no scope in it, dnsmasq has no addresses to lend.

DNS turns a name into an address

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

A device asks its configured DNS server. The server answers from its records, or asks another server and remembers the reply for a while.

DHCP tells the device which DNS server to use.

Worked example: common record types

TypeIt answersExample
AWhich IPv4 address has this name?files.lab is 10.0.10.20
AAAAWhich IPv6 address has this name?files.lab is a long hex number
CNAMEIs this name another name?www is the same as shop
MXWhich server takes mail for this domain?mail.example.org
PTRWhich name has this address?10.0.10.20 is files.lab
TXTWhat note is attached?A short piece of text

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.

Read pc1's list of DNS servers

Command
cat /etc/netns/pc1/resolv.conf

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

A common mistake

Warning

Many people think DHCP and DNS are one service. They do different jobs. DHCP lends addresses to devices. DNS answers questions about names. One program can do both, as dnsmasq does here, and they stay two jobs.

Going further: watch a lease being renewed

Once your configuration works in the lab, run sudo dnslab renew pc1 twice, a minute apart.

Each time, read the server's lease file with sudo cat /run/dnslab/dnsmasq.leases. Does pc1 get the same address? What changed in its line, and why? Feed the file to your active_leases to check it.

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.