Skip to content

Linux Exercise 7 - Ubuntu DHCP Server

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:

  1. In VirtualBox Manager → File → Tools → Network Manager.
  2. Go to Host-only Networks tab.
  3. Create a new one (e.g. vboxnet0).
  4. 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

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 and ens4 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.


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:

Terminal window
ip link show
ip 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.


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:

Terminal window
sudo netplan generate
sudo netplan apply
ip 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


Update packages and install:

sudo apt update
sudo 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:

Terminal window
sudo mkdir -p /var/lib/dhcp
sudo touch /var/lib/dhcp/dhcpd.leases
sudo chown root:dhcpd /var/lib/dhcp /var/lib/dhcp/dhcpd.leases
sudo 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:

Terminal window
# Minimal example for a lab network
authoritative;
ddns-update-style none;
# global settings
default-lease-time 600; # seconds
max-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 resides
subnet 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:

Terminal window
sudo sed -i 's/^INTERFACESv4=.*/INTERFACESv4="ens3"/' /etc/default/isc-dhcp-server
sudo 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”
Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now isc-dhcp-server
sudo systemctl status isc-dhcp-server --no-pager

Watch the logs (live):

Terminal window
sudo journalctl -u isc-dhcp-server -f
# or
sudo 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:

Terminal window
sudo netplan apply
ip addr show dev ens3
ip 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


On the client:

Terminal window
ip addr show dev ens3 # IP must be in the range you configured
ip 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:

Terminal window
sudo grep -i <client-mac-or-ip> /var/lib/dhcp/dhcpd.leases
cat /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


  • dhclient - DHCP Client for managing DHCP requests
Terminal window
# release the current DHCP lease
sudo dhclient -r ens3
# request again from scratch using verbose output
sudo dhclient -v ens3
  • dhcping — simulate client DHCP request to a server (useful for scripted tests):

    sudo apt install dhcping
    dhcping -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.)


  1. Server won’t startsudo systemctl status isc-dhcp-server -l and sudo 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
  2. “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
  3. 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.
  4. Static IP conflict → ensure static/fixed addresses are outside the dynamic range to avoid two hosts using same IP.
  5. AppArmor or permission errorsjournalctl will show apparmor="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

  1. Change default-lease-time to 30 seconds and observe lease renewal behavior.
  2. Add a host static mapping and verify that the client with that MAC always gets the fixed IP.
  3. 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.)
  4. 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)”
Terminal window
# server: identify interface
ip link show
ip addr show
# server: create lease file & set perms
sudo mkdir -p /var/lib/dhcp
sudo touch /var/lib/dhcp/dhcpd.leases
sudo chown root:dhcpd /var/lib/dhcp /var/lib/dhcp/dhcpd.leases
# server: install dhcp server
sudo apt update
sudo apt install isc-dhcp-server -y
# server: start/enable service
sudo systemctl enable --now isc-dhcp-server
sudo systemctl status isc-dhcp-server -l
sudo journalctl -u isc-dhcp-server -f
# client: quick request with dhclient (install if missing)
sudo apt install isc-dhcp-client -y
sudo dhclient -v ens3
# capture DHCP packets (tcpdump)
sudo tcpdump -n -i ens3 udp and \(port 67 or port 68\) -vv