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

Free lesson

Giving devices addresses and names (DHCP, DNS)

After this lesson: I can give devices addresses and names.

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

Your phone joins the Wi-Fi

You walk into a café and join its Wi-Fi. Nobody types an address into your phone, yet it works in seconds.

Then you type a website's name, not a number, and the page opens.

Two small jobs happened without you seeing them. This lesson shows both.

By the end of this lesson

Note

I can give devices addresses and names.

Key words

WordWhat it meansExample
DHCPthe service that lends addresses to devices as they join
Leasean address lent to one device for a set time
Poolthe range of addresses a DHCP server may lend.100 to .150
Hardware addressthe fixed number built into a network card, also called a MAC address02:00:00:00:00:01
Reservationa rule that always lends one device the same address
DNSthe service that turns a name into an address

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

A DHCP server lends addresses

A new device has no address, so it cannot ask anybody directly. It shouts to the whole network instead: is there a DHCP server here?

The server offers an address from its pool. The device accepts it, and the address becomes a lease. Before the lease runs out, the device asks to keep it. A device that leaves just stops asking, and the address goes back in the pool.

Worked example: four short messages

MessageSent byWhat it means
DISCOVERThe new device, to everyoneIs there a DHCP server here?
OFFERThe serverYou can have 10.0.10.100
REQUESTThe deviceYes please, I will take it
ACKThe serverIt is yours for 12 hours

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

Meet the server and two computers

Command
dnslab status

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

Ask for an address for pc1

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.

Why pc1 gets nothing

The status shows the server on 10.0.10.1/24, and two computers, pc1 and pc2, with no address at all. The last line says dnsmasq: running.

The renew prints pc1 was given nothing: no DHCPOFFER came back. pc1 shouted DISCOVER, but no OFFER came. The server program is running, yet nobody has told it what it may lend.

A reservation keeps one address

A phone can have a different address every day and nobody minds. A printer is different: every laptop has to find it in the same place.

Every network card has a hardware address built in. A reservation tells the server: when this hardware address asks, always lend this one address. Pick reserved addresses outside the pool, so the server never lends them to somebody else.

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.

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.

An empty file lends nothing

The long line ends with link/ether 02:00:00:00:00:01. That is pc1's hardware address, the number a reservation would use.

The settings file holds three lines starting with #. Those are notes for people, and the server ignores them. So there is no pool, no reservation and no names. Filling this file in is your job in the lab.

A common mistake

Warning

Many people think DHCP and DNS are one service. DHCP lends an address and tells the device which DNS server to use. DNS answers names. One machine can run both, as in this lab, but they are two separate jobs.

DNS turns a name into an address

People remember names. Computers need numbers. A DNS server keeps a list of records, each one a name and its address.

When you type a name, your device asks the DNS server it was told about. The server answers with the address, and the device connects to that.

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 where pc1 sends its questions

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.

No record, no answer

The answer says status: REFUSED and ANSWER: 0. The server has no record for files.lab, and it has nobody else it can ask.

pc1's file of DNS servers is empty, so the second Run prints nothing. pc1 does not even know where to ask. DHCP will tell it once the server has something to lend.

Going further: your own leases

Once your lab works, run sudo dnslab renew pc1 and then cat /run/dnslab/dnsmasq.leases. Each line is one lease.

Copy the text into your assignment and call ip_for_mac with pc1's hardware address. Does it give the address the status shows? Which part of the line 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.