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
| Word | What it means | Example |
|---|---|---|
| DHCP | the service that lends addresses to devices that ask | |
| Lease | an address lent for a set time | 12 hours |
| Scope | the range of addresses a DHCP server may lend | 10.0.10.100 to 10.0.10.150 |
| Reservation | a fixed address always given to one device, found by its hardware address | |
| DNS | the service that turns a name into an address | |
| Record | one 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
| Step | Sent by | What it says |
|---|---|---|
| Discover | The device | Is there a DHCP server here? |
| Offer | The server | You may have this address |
| Request | The device | I would like that address, please |
| Acknowledge | The server | It is yours until this time |
The cells students fill in are left blank in this preview.
Meet the server and two clients
dnslab statusShown 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
sudo dnslab renew pc1Shown 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
| Field | Example | Meaning |
|---|---|---|
| Expiry | 1761300000 | When the lease ends, as seconds since 1970. Zero means never |
| Hardware address | 02:00:00:00:00:0a | Who holds the lease |
| Address | 10.0.10.101 | What was lent |
| Name | till1 | The name the device gave, or a star |
| Client id | 01:02:00:00:00:00:0a | A second label, or a star |
The cells students fill in are left blank in this preview.
Read a client's hardware address
sudo ip netns exec pc1 ip -o link show eth0Shown 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
cat /etc/dnsmasq.d/lab.confShown 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
| Type | It answers | Example |
|---|---|---|
| A | Which IPv4 address has this name? | files.lab is 10.0.10.20 |
| AAAA | Which IPv6 address has this name? | files.lab is a long hex number |
| CNAME | Is this name another name? | www is the same as shop |
| MX | Which server takes mail for this domain? | mail.example.org |
| PTR | Which name has this address? | 10.0.10.20 is files.lab |
| TXT | What 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
sudo ip netns exec server dig +noall +comments @10.0.10.1 files.labShown 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
cat /etc/netns/pc1/resolv.confShown 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.