Skip to content

ElementaryLesson 5 of 9

Handing out addresses: DHCP ​

A device that joins a network needs four things before it can talk to anything. DHCP hands them out automatically: the device asks, and a DHCP server answers with a free address from its pool. The server lends the address for a set time, called the lease.

What a device needs ​

WhatExampleWhat it is for
IP address192.168.20.100Who the device is on the network
Mask255.255.255.0, or /24Which addresses are on its own network, to send to directly
Gateway192.168.20.1Where to send everything else: the router's address in this VLAN
DNS server192.168.20.1Who turns names into addresses

You could type these into every laptop, phone, and printer by hand, and some devices, such as servers, are still set up that way. But nobody types them into a phone that visits ten networks a week. And two people who type the same address by hand break each other's connection. So almost every network uses Dynamic Host Configuration Protocol: a device joining a network asks a server for an address, a gateway, and DNS servers, and the server gives them. instead.

Your home router has done this all along, which is why Opening a website could start with all four in place.

Four messages ​

Getting an address takes four messages between the device and the DHCP server. People remember them as DORA: discover, offer, request, acknowledge. They travel over UDP, from port 68 on the device to port 67 on the server.

Try itStep through a laptop getting its address
Laptopno address yetSwitchVLAN 20Router, DHCP server192.168.20.1Printer192.168.20.20

Step 1 of 6

A laptop joins, with nothing

The laptop is plugged into a port in VLAN 20. It has its hardware address, but no IP address, no gateway, and no DNS server.

The discover and the request are broadcasts, and a broadcast stays inside its VLAN. So each VLAN needs a DHCP server of its own that can hear it.

It also means anyone in the VLAN can answer. A spare home router plugged in by mistake answers too. Devices take whichever offer arrives first, so some of them get the wrong gateway and lose the internet. When some devices on a network work and others do not, look for a second DHCP server.

CheckWhy does the laptop send its discover message to everyone in the VLAN?

The pool and the lease ​

The server does not hand out just any address. It lends from its The range of addresses a DHCP server may lend to devices, from a first to a last address., a range such as 192.168.20.100 to 192.168.20.200. The rest of the network, such as .1 to .99, is kept for the router and for devices whose address is set by hand.

Each address is lent for a lease time. A device that stays asks to renew its lease when half of it has passed, so it keeps its address. A device that leaves usually does not say so, and the server keeps its address for it until the lease runs out.

Try itFill a pool, and let leases end
router.ts, the guest VLAN's DHCP server
dhcp_server: {
    guests: {
        interface: "guests",
        network: "192.168.30.0/24",
        gateway: "192.168.30.1",
        dns: ["192.168.30.1"],
        pool: ["192.168.30.100", "192.168.30.103"],
        lease_time: 86400,
    },
},

day 1, 00:004 of 4 addresses free

  • 192.168.30.100free
  • 192.168.30.101free
  • 192.168.30.102free
  • 192.168.30.103free

Let a few guests arrive, let some leave, and keep more arriving.

A short lease frees an address soon after its guest leaves. It costs nothing for the guests who stay, because they renew. So guest networks use short leases of about an hour. An office where the same laptops come every day can use long ones.

CheckA café’s guest Wi-Fi has 50 addresses and day-long leases. About 200 guests pass through each day, each staying an hour. What goes wrong?

The gateway, on a VLAN interface ​

In Trunks, the router reached every VLAN over one trunk port. It still needs an address in each VLAN, to be that VLAN's gateway. Since one port carries them all, the address goes on a An interface on a router or switch that sits on a VLAN instead of on a port. It gives the device an address in that VLAN. instead: one for each VLAN, each with its own address.

This is part of the router in Circuit's single-site example, which you build in the last project of this level:

ts
interfaces: {
    ethernet: {
        "25g-1": {
            description: "switch 10GE1/0/1",
            speed: "10g",
            trunk_vlans: ["mgmt", "home", "guests"],
            link: { device: "switch", port: "10g-1" },
        },
    },
    // The gateway of each VLAN.
    vlan_interface: {
        mgmt: { vlan: "mgmt", addresses: ["192.168.10.1/24"] },
        home: { vlan: "home", addresses: ["192.168.20.1/24"] },
        guests: { vlan: "guests", addresses: ["192.168.30.1/24"] },
    },
},

services: {
    dhcp_server: {
        home: {
            interface: "home",
            network: "192.168.20.0/24",
            gateway: "192.168.20.1",
            dns: ["192.168.20.1"],
            pool: ["192.168.20.100", "192.168.20.200"],
        },
        guests: {
            interface: "guests",
            network: "192.168.30.0/24",
            gateway: "192.168.30.1",
            dns: ["192.168.30.1"],
            pool: ["192.168.30.100", "192.168.30.200"],
            lease_time: 3600,
        },
    },
},

Each entry in interfaces.vlan_interface sits on the VLAN in its vlan field, and its addresses give the router its address in that VLAN. Each DHCP server in services.dhcp_server then says:

FieldWhat it says
interfaceWhere it answers: the VLAN interface of its VLAN.
networkThe network it serves.
gatewayThe gateway it hands out: the router's own address on that VLAN interface.
dnsThe DNS servers it hands out. Here the router answers DNS questions itself.
poolThe first and last address it lends.
lease_timeHow long a lease lasts, in seconds. 3600 is one hour, short, for guests.

The mgmt VLAN has no DHCP server: the devices on it have addresses written in their own files. Circuit checks that the gateway and the pool are inside the network. A pool that ends at 192.168.21.5 stops circuit validate:

text
  error  router: services.dhcp_server.home: 192.168.21.5 is outside 192.168.20.0/24

1 error(s).

Some devices, such as a printer, are easier to find when their address never changes. A reservation ties an address to one hardware address, and the server always lends that device the same one:

ts
reservations: [{ address: "192.168.20.20", mac: "00:00:5e:00:53:20", description: "Printer" }],

A DHCP server elsewhere

A bigger network may run one DHCP server for every VLAN, on a server of its own. A broadcast cannot reach it across a router, so the router passes the requests on, with services.dhcp_relay. Hover it in your editor to see its fields.

Discussion

Stuck, or found a mistake? Ask here. You sign in with GitHub.

Released under the Business Source License 1.1.