Skip to content

Lab 02-01 · DHCP server: the router's reception desk

MTCNA

Module 2 · DHCP of the MikroTik MTCNA course, official slides p106–120 (what DHCP is, the DHCP client, DNS on the router, the DHCP server and the LAB "Servidor DHCP" on p113–119).

This morning you walked into school, opened your laptop or phone, and the Wi-Fi just worked. You did not type an address, a mask or a gateway. Nobody from IT came to configure you. Somebody handed your device everything it needed in less than a second. Who?

That somebody is a DHCP server, and on a small network it usually lives inside the router. Every MikroTik home router ships with one already running. In this lab you will delete it on purpose, move your network to a new address range, and rebuild the server from scratch with RouterOS's DHCP Setup wizard. Then your laptop asks again, and the router answers with the new settings.

What you will learn

  • Explain what DHCP does and name the four messages of a DORA exchange.
  • Tell apart the parts of a DHCP server on RouterOS: server, address pool, network and lease.
  • Remove the factory DHCP server and the old LAN address in the right order.
  • Put the router's address and the DHCP server on the bridge, not on a bridge port (the classic trap).
  • Build a new DHCP server with the DHCP Setup wizard and check that a PC gets a lease and reaches the Internet.
  • Understand why, on a real router, removing the LAN address kicks you out (and how MAC-WinBox saves you).

The analogy: a hotel reception desk

Keep this picture in mind for the whole lab. Your LAN is a hotel, and the DHCP server is the reception desk.

Hotel Network What it means
A guest walks in A device joins the network It has no address yet
The room key The IP address Where the guest "lives" while staying
The rooms reception may give out The address pool The range of addresses the server may hand out
The check-out time The lease time How long the address is yours before you must ask again
"The exit is that way" The default gateway Where to send anything that leaves the hotel (the Internet)
"Ask the information desk" The DNS server Who turns names like www.mikrotik.com into addresses
The guest book The lease table Who has which room right now

What is DHCP?

DHCP (Dynamic Host Configuration Protocol) is the protocol a device uses to ask the network for its IP settings automatically: address, subnet mask, default gateway and DNS server.

It is defined in RFC 2131 (1997). Before DHCP, somebody had to type those four values by hand on every single computer, and two people choosing the same address broke both of them.

Three facts from the course slides that matter in this lab:

  • It only works inside one broadcast domain. A brand-new device has no address, so it cannot send a normal "letter" to the server. It shouts to everybody on its segment instead (a broadcast). Routers do not forward broadcasts, so the server must be on the same LAN (or a relay must pass the message on).
  • Use DHCP only on networks you trust. Any device can pretend to be a DHCP server. A fake one could hand out a fake gateway and see all your traffic.
  • RouterOS is both. It can be a DHCP client (asking for an address, like on its Internet port) and a DHCP server (giving addresses out, like on its LAN).

DORA: four messages, one address

sequenceDiagram
    participant PC as pc1 (no address yet)
    participant R as r1 (DHCP server)
    PC->>R: DISCOVER (broadcast) "Is there a DHCP server here?"
    R->>PC: OFFER "You can have 192.168.199.254"
    PC->>R: REQUEST "Yes please, I take 192.168.199.254"
    R->>PC: ACK "It is yours for 30 minutes"

Discover, Offer, Request, Ack: the guest asks, reception offers a room, the guest accepts, reception hands over the key. You will see these four words printed by the PC in Step 6.

Leases expire, so devices renew

A lease is a loan, not a gift. Halfway through the lease time the client quietly asks to renew. If a device leaves and never renews, its address goes back to the pool when the lease expires. That is how a café with 50 addresses can serve hundreds of customers a day.

The router is also a DHCP client and a DNS server

The course reminds you of two things your router already does on its WAN (Internet) side:

  • A DHCP client on the Internet port (IP → DHCP Client) asks the provider for an address. Here the "provider" is the classroom router class-ap, which gives r1 an address in 10.0.0.0/24.
  • The router keeps a list of DNS servers (IP → DNS), learned from that DHCP client, and with Allow Remote Requests it answers DNS questions from the LAN too. It also has static DNS entries: the factory one called router.lan points at the router itself, so you can type http://router.lan instead of an IP.

The four pieces of a RouterOS DHCP server

Piece Menu Hotel version
DHCP server IP → DHCP Server → DHCP The desk itself, and which door (interface) it serves
Address pool IP → Pool The list of free rooms
DHCP network IP → DHCP Server → Networks What reception tells every guest: gateway, DNS, mask
Leases IP → DHCP Server → Leases The guest book

Also needed, but not part of the server: the router's own IP address on that interface. The server only serves a network the router itself belongs to.

The scenario

flowchart LR
    INET["<b>internet</b><br>198.51.100.10<br>web server"]
    CAP["<b>class-ap</b><br>class gateway<br>10.0.0.1 · 198.51.100.1"]
    subgraph R1["r1 · your router"]
        WAN["ether2 (WAN)<br>DHCP client: 10.0.0.x"]
        BR["<b>bridge1</b> (LAN)<br>before: 192.168.88.1/24<br>after: 192.168.199.1/24"]
        E3["ether3<br>bridge port"]
    end
    PC1["<b>pc1</b><br>your laptop<br>dhclient eth1"]
    INET ---|"198.51.100.0/24"| CAP
    CAP ---|"10.0.0.0/24"| WAN
    E3 --- BR
    E3 ---|"eth1"| PC1
Node Role Address
internet "The Internet": answers ping, serves a web page 198.51.100.10/24
class-ap The trainer's router, gateway of the classroom, DNS, NAT 10.0.0.1/24 (class) and 198.51.100.1/24
r1 Your router. ether2 = WAN, ether3 = LAN port in bridge1 WAN: by DHCP from class-ap · LAN: 192.168.88.1/24 → 192.168.199.1/24
pc1 Your laptop (Linux), MAC 02:00:00:00:01:01 By DHCP

Why ether2 and not ether1?

On a real hAP the Internet port is ether1. In the virtual lab ether1 is reserved for the lab itself (containerlab uses it to manage the router), so the Internet port is ether2 and the LAN ports start at ether3. Never touch ether1 in these labs.

The course asks you to use your student number XY for the new subnet, 192.168.XY.0/24. This guide uses the course's own example, XY = 199, so the new LAN is 192.168.199.0/24 and the router is 192.168.199.1. The automatic check expects 199.

Start the lab

Download the lab files and unzip them: 02-01-dhcp-server.zip. Open a terminal inside the 02-01-dhcp-server folder.

sudo containerlab deploy -t topology.clab.yml

Give RouterOS about two minutes to boot. Then open the router and the PC in two terminals:

ssh admin@clab-mtcna-02-01-r1            # password: admin
docker exec -it clab-mtcna-02-01-pc1 bash

At any moment you can run ./check.sh (with CLAB_EXEC="docker exec" on your PC) to see which parts are already solved.

Step 1 · Meet the factory DHCP server

Before you destroy something, look at it. First let the laptop ask for an address the normal way.

On pc1:

dhclient -v eth1
ip -br address show eth1

You should see the DORA words and an address like 192.168.88.254/24. RouterOS hands out addresses from the top of the pool down, so the first guest gets .254.

On r1 (WinBox: IP → DHCP Server, tabs DHCP, Networks, Leases; and IP → Pool):

/ip dhcp-server print
/ip dhcp-server network print
/ip pool print
/ip dhcp-server lease print

Expected: a server called defconf on bridge1 using pool default-dhcp (192.168.88.10-192.168.88.254), a network 192.168.88.0/24 with gateway and DNS 192.168.88.1, and a lease for 02:00:00:00:01:01 (pc1) with a D flag (dynamic: created on the fly).

Check yourself

  1. Which four values did pc1 receive (address, mask, gateway, DNS)? Where in the router does each one come from?
  2. Why is the server attached to bridge1 and not to ether3?

Done when you can point at the server, the pool, the network and pc1's lease.

Step 2 · Remove the old server, network, pool and address

The course does it in this order: server → network → pool → address. Removing the "users" first and the things they depend on last avoids leaving a server pointing at a pool that no longer exists.

WinBox: IP → DHCP Server → DHCP (select defconf, −), then the Networks tab (select 192.168.88.0/24, −), then IP → Pool (default-dhcp, −), then IP → Addresses (192.168.88.1/24, −).

/ip dhcp-server remove [find name=defconf]
/ip dhcp-server network remove [find address="192.168.88.0/24"]
/ip pool remove [find name=default-dhcp]
/ip address remove [find address="192.168.88.1/24"]

Put the quotes around addresses with a slash

In RouterOS 7, [find address=192.168.88.1/24] without quotes silently matches nothing: nothing is removed and no error is shown. With quotes, address="192.168.88.1/24", it works. If in doubt, run print and remove by number instead (remove 1).

Check with /ip address print and /ip dhcp-server print: no 192.168.88.1, no server.

On a real router this step kicks you out

In the classroom you are connected to the router through 192.168.88.1. The moment you remove that address, WinBox loses the connection. That is why the course says: disconnect, and reconnect using the router's MAC address. MAC-WinBox talks to the router at Layer 2, without any IP address, so it keeps working (you did it in Lab 01-01). In this virtual lab you manage the router through ether1, so your SSH session survives.

Check yourself

  1. Run ping -c 2 192.168.88.1 on pc1. What happens now, and why?
  2. Why would removing the pool before the server be a bad idea?

Done when the router has no LAN address and no DHCP server.

Step 3 · Give the bridge its new address

The DHCP server can only serve a network the router belongs to, so first the router needs an address in 192.168.199.0/24. It goes on bridge1, the virtual switch that joins the LAN ports.

WinBox: IP → Addresses → +, Address 192.168.199.1/24, Interface bridge1.

/ip address add address=192.168.199.1/24 interface=bridge1
/ip address print

The bridge-port trap

ether3 is a bridge port (RouterOS calls it a slave interface): it has handed its traffic over to bridge1. Anything Layer 3 placed on it is useless. An IP address on ether3 shows the flag S (slave), and a DHCP server on ether3 shows the flag I (invalid) with the comment DHCP server can not run on slave interface!. Always configure the bridge, never its ports.

Check yourself

  1. Why .1 and a /24? How many addresses does 192.168.199.0/24 contain, and how many can be given to devices?

Done when /ip address print shows 192.168.199.1/24 on bridge1.

Step 4 · Rebuild the server with the DHCP Setup wizard

Instead of creating the server, the pool and the network one by one, RouterOS has a wizard that asks you six questions and creates all three for you.

WinBox: IP → DHCP Server → DHCP Setup. In the terminal the same wizard is:

/ip dhcp-server setup

Answer the questions like this (press Enter to accept a proposed value):

Wizard question (RouterOS 7.23) Answer Hotel version
dhcp server interface bridge1 Which door the desk serves
dhcp address space 192.168.199.0/24 (proposed) The whole building
gateway for dhcp network 192.168.199.1 (proposed) Where the exit is
addresses to give out 192.168.199.2-192.168.199.254 (proposed) The free rooms (.1 is the router's own)
send dns yes Tell guests about the information desk
dns servers accept the proposal (10.0.0.1) Which information desk
lease time accept 1800s (30 min) Check-out time

The wizard proposes good values because it reads them from the address you put on bridge1 in Step 3. That is why the course says the address must exist before you run it.

Now look at what it built:

/ip dhcp-server print
/ip dhcp-server network print
/ip pool print

Expected: a server dhcp1 on bridge1 with pool dhcp_pool1 (192.168.199.2-192.168.199.254), and a network 192.168.199.0/24 with gateway 192.168.199.1. Compare with slide p120: the wizard created a new pool and a new server.

The course slides show RouterOS 6

The screenshots on p118 show six windows and a 10-minute lease. RouterOS 7 adds the send dns question and proposes 30 minutes. The idea is identical.

Check yourself

  1. Which three objects did the wizard create? Write their names.
  2. The network shows an empty dns-server. Yet in Step 6 pc1 will receive DNS servers. Where do they come from?

Done when dhcp1 exists on bridge1 and is not disabled.

Step 5 · Fix the router's own name

The factory static DNS entry router.lan still points at the old address 192.168.88.1. Nobody will ever answer there. Point it at the new one.

WinBox: IP → DNS → Static, double-click router.lan, Address 192.168.199.1.

/ip dns static set [find name=router.lan] address=192.168.199.1
/ip dns static print

Done when router.lan points at 192.168.199.1.

Step 6 · Renew the laptop and test the Internet

pc1 still believes it lives at 192.168.88.254: it has a lease and nobody told it things changed. The course says: renew the IP address of your laptop. Release the old lease, then ask again.

On pc1:

dhclient -r eth1          # release: give the old key back
dhclient -v eth1          # ask again: watch DISCOVER, OFFER, REQUEST, ACK
ip -br address show eth1
ip route
cat /etc/resolv.conf

Expected: an address in 192.168.199.0/24 (probably .254), default via 192.168.199.1, and name servers 192.168.199.1 and 10.0.0.1. Now test the Internet, first by number and then by name:

ping -c 3 198.51.100.10
curl http://www.mikrotik.com/
dig +short router.lan

curl should print Welcome to the Internet, and router.lan should resolve to 192.168.199.1.

On r1, open the guest book (WinBox IP → DHCP Server → Leases):

/ip dhcp-server lease print

Check yourself

  1. Copy the four DORA lines that dhclient -v printed. Which ones are broadcasts, and why?
  2. pc1 got 10.0.0.1 as a second DNS server although you never typed it. Who told the router about 10.0.0.1?

Done when pc1 has a 192.168.199.x address and curl shows the welcome page. Run ./check.sh: it must say Lab 02-01 solved.

If something goes wrong

Symptom Likely cause Fix
remove [find address=...] does nothing Address written without quotes Use address="192.168.88.1/24" or remove by number
The wizard proposes a strange network You ran it before adding 192.168.199.1/24, or on the wrong interface Cancel, do Step 3, choose bridge1
Server shows I (invalid), "can not run on slave interface" It sits on ether3 (a bridge port) /ip dhcp-server set dhcp1 interface=bridge1
pc1 keeps 192.168.88.x It still holds the old lease dhclient -r eth1 then dhclient -v eth1
dhclient waits and gives up No server on the bridge, or the server is disabled /ip dhcp-server print; enable it, check the interface
Ping to 198.51.100.10 works but curl www.mikrotik.com fails DNS: send dns answered no, or DNS remote requests off /ip dns print must show allow-remote-requests: yes; rerun the wizard with send dns yes
Real router: WinBox disconnected after Step 2 You removed the address you were connected through Reconnect by MAC address in WinBox

Clean up

sudo containerlab destroy -t topology.clab.yml --cleanup

Key takeaways

  • DHCP hands out address, mask, gateway and DNS automatically, through four messages: DORA.
  • A RouterOS DHCP server is made of a server, an address pool, a DHCP network and the leases it creates.
  • Addresses and DHCP servers go on the bridge, never on a bridge port.
  • The DHCP Setup wizard builds the pool, the server and the network from the address already on the interface.
  • After the server changes, clients keep their old lease until they renew it.
  • On real hardware, removing the address you are connected through disconnects you: MAC-WinBox gets you back in.

Quick check

1. In which order do the four DHCP messages travel?
  • Discover, Offer, Request, Ack
  • Request, Offer, Discover, Ack
  • Offer, Discover, Ack, Request
  • Discover, Request, Offer, Ack

DORA: the client discovers a server, the server offers an address, the client requests it, and the server acknowledges. Like a hotel guest asking, getting offered a room, accepting it and receiving the key.

2. Which value does a DHCP server NOT usually give to a client?
  • The client's own MAC address
  • The IP address and mask
  • The default gateway
  • The DNS server

The MAC address is burned into the network card: the client already has it and uses it to identify itself. DHCP hands out the IP address, mask, gateway and DNS.

3. Where must the router's LAN address and its DHCP server be configured when ether3 is a member of bridge1?
  • On bridge1
  • On ether3
  • On ether1
  • On ether2

A bridge port hands its traffic to the bridge, so Layer 3 settings on the port are useless: RouterOS marks a DHCP server there as invalid ("can not run on slave interface"). The address and the DHCP server go on the bridge. ether1 is the lab's management port and ether2 is the WAN.

4. Why does the DHCP Setup wizard need the router's address on the interface before it runs?
  • It reads that address to propose the network, gateway and pool
  • It refuses to start on any interface that has no traffic
  • It copies the address into the DNS static table
  • It uses the address as the client's lease time

The wizard looks at the address on the chosen interface (192.168.199.1/24) and derives the address space, the gateway and the pool from it. No address, no sensible proposals.

5. You changed the router's LAN to 192.168.199.0/24, but your laptop still shows 192.168.88.254. What is the simplest explanation?
  • The laptop still holds its old lease and has not renewed it
  • The router hands out the old pool to devices it already knows
  • The laptop's network card can only use one subnet
  • The new DHCP server needs a router reboot to start

A lease is valid until it expires or is released. The client only learns the new settings when it renews: dhclient -r eth1 and dhclient -v eth1 on Linux, or unplug and plug back in.

6. On a real hAP you remove 192.168.88.1 from the bridge and WinBox freezes. What is the best way back in?
  • Reconnect with WinBox using the router's MAC address
  • Wait for the DHCP lease to expire and reconnect by IP
  • Ping 192.168.88.1 until it answers again
  • Reset the router to factory settings straight away

You removed the very address you were connected through. MAC-WinBox works at Layer 2 and needs no IP, so you can log in, add the new address and continue. A reset would throw away all your work.

7. A café router has a pool of 50 addresses, yet 300 customers connect during the day. How is that possible?
  • Leases expire, so addresses of customers who left return to the pool
  • The router gives the same address to several phones at once
  • Phones invent their own address when the pool is empty
  • Each customer is given a new address every second

A lease is a loan with a lease time. When a customer leaves and stops renewing, the lease expires and the address goes back to the pool for the next customer. Two devices with the same address would break each other.

Next, in Lab 02-02, you will stop treating every guest the same: your laptop gets a reserved room that never changes, and strangers get no room at all.