Linux Exercise 7 - Ubuntu DHCP Server
Lab setup
Section titled “Lab setup”Create VMs
Section titled “Create VMs”We will use Virtualbox for this exercise.
Create VM1:
- Type: Ubuntu Server 24.4 and above
- Machine Name: server
- username: serverusr
- password: sytb
Create VM2:
- Type: Ubuntu Server or Desktop 24.4 and above
- Machine Name: client
- username: clientusr
- password: sytb
In VirtualBox Manager create a Host-only Network:
- In VirtualBox Manager → File → Tools → Network Manager.
- Go to Host-only Networks tab.
- Create a new one (e.g.
vboxnet0
). - Select it → Properties:
- IPv4 Address: e.g.
192.168.56.1
(this is host’s address in the host-only LAN). - Subnet mask:
255.255.255.0
. - Disable DHCP Server checkbox (important, otherwise VirtualBox’s own DHCP service will conflict with your lab DHCP server).
- If DHCP server option is not available (depending on your host and version of Virtualbox):
- In your command line enter
VBoxManage list dhcpservers
- Locate the DHCP Server for your Host-Only Network
- Disable it with
VBoxManage dhcpserver modify --network "NetworkName" --disable
- In your command line enter
- IPv4 Address: e.g.
Configure each VM
Section titled “Configure each VM”Go to each VM → Settings → Network:
- Adapter 1: Attached to NAT. We will need this adapter to be able to reach the internet from within the VM to install packages.
- Adapter 2: Attached to Host-Only Adapter, choose
vboxnet0
. This is the adapter we will use for the internal network where we want to set up DHCP.- Note: even with the DHCP Server disabled as described above, your host OS might be able to act as a DHCP server. If at the end of your exercise you see the client getting an IP address from the wrong server, you should switch both VMs’ second adapter to Internal with the same name instead.
Inside VM:
- NAT adapter usually shows up as
ens3
(with DHCP from VirtualBox NAT). - Host-Only adapter shows up as
ens4
(used for your DHCP lab). - Make sure you note down the actual adapter names and use them instead of
ens3
andens4
whenever they appear in this guide. - In the instructions below you will learn how to achieve the following. Don’t do it yet:
- On the server, you’ll configure
ens4
with static IP (e.g.192.168.100.1
). - On the client, you’ll set
ens4
to DHCP.
- On the server, you’ll configure
Network structure
Section titled “Network structure”Short conceptual primer (read to students)
Section titled “Short conceptual primer (read to students)”-
What DHCP does: DHCP automatically assigns IPv4 configuration (IP address, subnet mask, gateway, DNS, etc.) to clients so admins don’t manually configure each device. The server maintains a pool of addresses and a lease database. See this resource for details: DHCP
-
DORA: DHCP uses a 4-step exchange: Discover → Offer → Request → Acknowledge (client broadcasts Discover; server(s) reply Offer; client picks one and sends Request; server replies ACK). Watching these packets (tcpdump) is a great way to teach the protocol. Wikipedia
-
Lease file: The server persistently stores lease records in
/var/lib/dhcp/dhcpd.leases
(dhcpd needs that file present to run). We’ll look at it. Ubuntu Manpages
STEP-BY-STEP: configure the DHCP server (Ubuntu 24.04)
Section titled “STEP-BY-STEP: configure the DHCP server (Ubuntu 24.04)”1) Identify the server’s network interface(s)
Section titled “1) Identify the server’s network interface(s)”On the server console run:
ip link showip addr show
Note the interface name you’ll use (example ens3
, ens4
, eth0
— modern Ubuntu uses en*
/eth*
names). You’ll use this name in Netplan and in the DHCP server config.
Note: Interface names are not always eth0
— DHCP daemon must bind to the correct physical interface to listen for client broadcasts.
2) Give the server a static IP (Netplan)
Section titled “2) Give the server a static IP (Netplan)”DHCP servers must have a stable address on the LAN so the router and clients can reach them and so the server’s pool is in the right subnet.
Create (or edit) a netplan file, for example /etc/netplan/99-static-dhcp-server.yaml
:
network: version: 2 ethernets: ens3: dhcp4: no addresses: [192.168.100.1/24] nameservers: addresses: [1.1.1.1, 8.8.8.8]
Apply it:
sudo netplan generatesudo netplan applyip addr show dev ens3 # verify address is set
Note: Netplan is the current way to configure networking on Ubuntu servers (and the YAML must be valid indentation). If you set the wrong interface name you will lose connectivity — check ip addr
first. Ubuntu Documentation
3) Install the DHCP server package
Section titled “3) Install the DHCP server package”Update packages and install:
sudo apt updatesudo apt install isc-dhcp-server -y
Note: isc-dhcp-server
is the traditional DHCP daemon (DHCPD) package that provides dhcpd
and the config files we’ll edit. Ubuntu documents this package and how to configure it. Ubuntu Documentation
4) Ensure the lease database exists & permissions are OK
Section titled “4) Ensure the lease database exists & permissions are OK”Create the lease file if it’s not already there and give proper ownership:
sudo mkdir -p /var/lib/dhcpsudo touch /var/lib/dhcp/dhcpd.leasessudo chown root:dhcpd /var/lib/dhcp /var/lib/dhcp/dhcpd.leasessudo chmod 0644 /var/lib/dhcp/dhcpd.leases
Note: dhcpd
requires a lease database file to exist before starting; it also needs to be writable. If permissions are wrong, the server will log “Can’t open /var/lib/dhcp/dhcpd.leases for append.” (you’ll learn this when troubleshooting). Ubuntu Manpages
5) Edit the main DHCP config: /etc/dhcp/dhcpd.conf
Section titled “5) Edit the main DHCP config: /etc/dhcp/dhcpd.conf”Open with sudo nano /etc/dhcp/dhcpd.conf
and replace (or add) a minimal, commented configuration:
# Minimal example for a lab networkauthoritative;ddns-update-style none;
# global settingsdefault-lease-time 600; # secondsmax-lease-time 7200; # seconds
option domain-name "lab.local";option domain-name-servers 1.1.1.1, 8.8.8.8;
# subnet declaration for the LAN where server interface residessubnet 192.168.100.0 netmask 255.255.255.0 { range 192.168.100.100 192.168.100.200; option routers 192.168.100.1; # gateway option broadcast-address 192.168.100.255; option subnet-mask 255.255.255.0; default-lease-time 600; max-lease-time 7200;}
# example: give a static IP to a host (outside the pool)# host mydevice {# hardware ethernet aa:bb:cc:dd:ee:ff;# fixed-address 192.168.100.50;# }
Explain main items:
authoritative;
— tells server it can assert control for the subnet (useful in lab to avoid confusion if another DHCP server is present).range
— the dynamic pool; clients will normally get addresses from here.option routers
— default gateway clients should use.domain-name-servers
— DNS servers clients will get.host { ... }
— static mapping: server will always try to give that MAC the fixed address (keep fixed addresses outside the dynamic range to avoid conflicts).
For full details on dhcpd.conf syntax consult the ISC dhcpd manual pages (we picked key fields above). kb.isc.org
6) Tell the DHCP daemon which interface(s) to listen on
Section titled “6) Tell the DHCP daemon which interface(s) to listen on”Edit /etc/default/isc-dhcp-server
and set INTERFACESv4
to the server interface:
sudo sed -i 's/^INTERFACESv4=.*/INTERFACESv4="ens3"/' /etc/default/isc-dhcp-serversudo sed -i 's/^INTERFACESv6=.*/INTERFACESv6=""/' /etc/default/isc-dhcp-server
(Or edit with sudo nano /etc/default/isc-dhcp-server
and set INTERFACESv4="ens3"
.)
Note: by default the package may not know which interface to bind to — this tells systemd/unit which interface(s) DHCPD should service. If it references the wrong interface the daemon will start but not answer on your LAN. (Ubuntu docs mention editing this file.) Ubuntu Documentation
7) Start the DHCP service and enable on boot
Section titled “7) Start the DHCP service and enable on boot”sudo systemctl daemon-reloadsudo systemctl enable --now isc-dhcp-serversudo systemctl status isc-dhcp-server --no-pager
Watch the logs (live):
sudo journalctl -u isc-dhcp-server -f# orsudo tail -f /var/log/syslog | grep dhcpd
Note: systemctl status
shows immediate errors (bad config, permission issues). Use the journal to see offers/leasing events. The server logs lease grants here.
STEP-BY-STEP: test from the client Ubuntu machine (console only)
Section titled “STEP-BY-STEP: test from the client Ubuntu machine (console only)”Configure the client to use DHCP (Netplan)
Section titled “Configure the client to use DHCP (Netplan)”Create /etc/netplan/99-dhcp-client.yaml
on the client:
network: version: 2 ethernets: ens3: dhcp4: true
Apply:
sudo netplan applyip addr show dev ens3ip route show
You should see the client obtain an IP from the DHCP range (e.g. 192.168.100.100
etc).
Why: this is how normal machines get DHCP addresses in production; it shows students how server and client interact through the normal OS configuration. Ubuntu Documentation
Verify the client got the lease
Section titled “Verify the client got the lease”On the client:
ip addr show dev ens3 # IP must be in the range you configuredip route show # default gateway set?ping -c 3 1.1.1.1 # ping a DNS (connectivity)ping -c 3 google.com # verify DNS resolution works
On the server, check lease file:
sudo grep -i <client-mac-or-ip> /var/lib/dhcp/dhcpd.leasescat /var/lib/dhcp/dhcpd.leases # full lease DB (text)# or (if installed)sudo dhcp-lease-list
Why: the server lease file shows which client got which IP and for how long; this reinforces how the server remembers leases. Ubuntu Manpages
Watching the DORA exchange (packet capture)
Section titled “Watching the DORA exchange (packet capture)”On the server (or client) run:
sudo tcpdump -n -i ens3 udp and \(port 67 or port 68\) -vv
Then on the client trigger a DHCP request (netplan apply or dhclient -v ens3
). You’ll see the Discover, Offer, Request, ACK packets — great visual of DORA.
Why: tcpdump lets students see DORA and the UDP ports (server listens on UDP 67; client on UDP 68). This is the best way to connect theory (DORA) with reality. (Troubleshooting guides often recommend tcpdump to confirm DHCP comms.) Red Hat
Extra testing tools (optional)
Section titled “Extra testing tools (optional)”dhclient
- DHCP Client for managing DHCP requests
# release the current DHCP leasesudo dhclient -r ens3# request again from scratch using verbose outputsudo dhclient -v ens3
-
dhcping
— simulate client DHCP request to a server (useful for scripted tests):sudo apt install dhcpingdhcping -s 192.168.100.1 -c 192.168.100.101(This tool tests server responsiveness without fully configuring the interface.) Baeldung on Kotlin
-
nmap
script broadcast discover:sudo nmap --script broadcast-dhcp-discover -e ens3(Useful to find DHCP servers on the segment.)
Common problems & how to debug
Section titled “Common problems & how to debug”- Server won’t start →
sudo systemctl status isc-dhcp-server -l
andsudo journalctl -u isc-dhcp-server -b
.- Check
/etc/dhcp/dhcpd.conf
for syntax errors (typos will stop start). - Ensure
/var/lib/dhcp/dhcpd.leases
exists & has correct permissions. Ubuntu Manpages
- Check
- “Not configured to listen on any interfaces” → check
/etc/default/isc-dhcp-server
INTERFACESv4
is set to correct interface (e.g.,INTERFACESv4="ens3"
). Ubuntu Documentation - Client gets no IP → check server and client are on same L2 domain (same VLAN/switch), firewall blocking UDP 67/68, or another DHCP server interfering. Use
tcpdump
to see if Discover reaches server. - Static IP conflict → ensure static/fixed addresses are outside the dynamic
range
to avoid two hosts using same IP. - AppArmor or permission errors →
journalctl
will showapparmor="DENIED"
or permission messages; the DHCP service unit normally handles ownership (chown root:dhcpd ...
) — see journal for details and adjust AppArmor profile or ownership. (If you see permission errors, check ownership of/var/lib/dhcp
and lease file.) Ask Ubuntu+1
Optional exercises
Section titled “Optional exercises”- Change
default-lease-time
to 30 seconds and observe lease renewal behavior. - Add a
host
static mapping and verify that the client with that MAC always gets the fixed IP. - Run two DHCP servers on the same segment (briefly) and watch what happens — then restore a single authoritative server. (Discuss why networks should have one authoritative DHCP server per broadcast domain.)
- Capture DORA via tcpdump and annotate each packet (which options are present, what is the YIADDR field, etc.).
Short reference of useful commands (copy/paste)
Section titled “Short reference of useful commands (copy/paste)”# server: identify interfaceip link showip addr show
# server: create lease file & set permssudo mkdir -p /var/lib/dhcpsudo touch /var/lib/dhcp/dhcpd.leasessudo chown root:dhcpd /var/lib/dhcp /var/lib/dhcp/dhcpd.leases
# server: install dhcp serversudo apt updatesudo apt install isc-dhcp-server -y
# server: start/enable servicesudo systemctl enable --now isc-dhcp-serversudo systemctl status isc-dhcp-server -lsudo journalctl -u isc-dhcp-server -f
# client: quick request with dhclient (install if missing)sudo apt install isc-dhcp-client -ysudo dhclient -v ens3
# capture DHCP packets (tcpdump)sudo tcpdump -n -i ens3 udp and \(port 67 or port 68\) -vv
References / further reading
Section titled “References / further reading”- Ubuntu Server how-to: install & configure
isc-dhcp-server
. Ubuntu Documentation - Netplan docs (static and DHCP config examples). Ubuntu Documentation
dhcpd.leases
manual (format & why the file is required). Ubuntu Manpagesdhcping
and client/discovery testing approaches. Baeldung on Kotlin- DHCP protocol & the DORA exchange (overview). Wikipedia