How to Manually Configure a DHCP Server on Ubuntu (With Script Example)

How to Manually Configure a DHCP Server on Ubuntu (With Script Example)

Whether you're setting up a home lab, provisioning virtual machines, or simulating enterprise networks, having your own DHCP server can make things a lot easier. In this guide, I’ll show you how to manually install and configure an ISC DHCP server on Ubuntu, step-by-step.

We'll also walk through a Bash script you can use to automate the process—ideal for reproducible labs or automated testing environments.

What is DHCP?

DHCP stands for Dynamic Host Configuration Protocol. It automatically assigns IP addresses and network parameters (like subnet mask, default gateway, and DNS servers) to devices in a network—saving you from manually configuring each one.

Prerequisites

To follow along, you'll need:

  • A machine running Ubuntu (any recent LTS version should work fine)
  • sudo access or root privileges
  • A network interface available for DHCP service (e.g. ens4, eth0, etc.)
  • Internet access to install packages

Step-by-Step Manual Setup

1. Update Your System

sudo apt update && sudo apt upgrade -y

2. Install the ISC DHCP Server

sudo apt install -y isc-dhcp-server

This package provides a robust DHCP server used widely in Linux environments.

3. Configure the DHCP Server

Edit the main configuration file:

sudo nano /etc/dhcp/dhcpd.conf

Paste in a basic configuration like this:

option domain-name "localdomain";
option domain-name-servers 8.8.8.8;

default-lease-time 600;
max-lease-time 7200;

authoritative;

subnet 192.168.18.0 netmask 255.255.255.0 {
  range 192.168.18.10 192.168.18.50;
  option routers 192.168.18.1;
  option subnet-mask 255.255.255.0;
  option broadcast-address 192.168.18.255;
}
Adjust subnet, range, and routers to suit your network layout.

4. Specify the Network Interface

Open the DHCP server defaults file:

sudo nano /etc/default/isc-dhcp-server

Set the interface name:

INTERFACESv4="ens4"

Replace ens4 with your actual interface name.

To find it:

ip addr

5. Enable and Start the Service

sudo systemctl enable isc-dhcp-server
sudo systemctl restart isc-dhcp-server

6. Check the Status

sudo systemctl status isc-dhcp-server --no-pager

If everything is configured correctly, you should see the service active and running.

Test the DHCP Server

Try connecting a client device to your network segment, or configure a VM to obtain an IP automatically via DHCP.

You can monitor leases in two ways:

1. Traditional: View Raw Lease File

cat /var/lib/dhcp/dhcpd.leases

This shows all current and past DHCP lease entries, including timestamps, MAC addresses, and client hostnames.

But it's a bit raw and not very readable.

2. Modern: Use dhcp-lease-list for a Cleaner View

sudo dhcp-lease-list

This gives you a clean tabular output of the current leases, including MAC, IP, hostname, and expiry date. Example output:

MAC                IP              hostname    valid until         manufacturer
00:50:79:66:68:00  192.168.18.10  PC1         2025-06-29 07:23:17 -NA-
00:50:79:66:68:01  192.168.18.11  PC2         2025-06-29 07:23:22 -NA-
00:50:79:66:68:02  192.168.18.12  PC3         2025-06-29 07:23:26 -NA-
⚠️ If you don’t see manufacturer names, download the OUI file:

This is easier than parsing the lease file manually and helps when troubleshooting IP allocation or device detection.

Bonus: Automate Everything with a Bash Script

Here’s a complete script that installs and configures the DHCP server automatically:

#!/bin/bash

set -e

INTERFACE="ens4"
SUBNET="192.168.18.0"
NETMASK="255.255.255.0"
RANGE_START="192.168.18.10"
RANGE_END="192.168.18.50"
ROUTER="192.168.18.1"
DNS="8.8.8.8"
LEASE_TIME="600"
MAX_LEASE_TIME="7200"
DOMAIN_NAME="localdomain"
HOSTNAME="$(hostname)"

echo "[+] Updating packages"
sudo apt update && sudo apt upgrade -y

echo "[+] Installing ISC DHCP server"
sudo apt install -y isc-dhcp-server

echo "[+] Configuring DHCP server"
sudo tee /etc/dhcp/dhcpd.conf > /dev/null <<EOF
option domain-name "$DOMAIN_NAME";
option domain-name-servers $DNS;

default-lease-time $LEASE_TIME;
max-lease-time $MAX_LEASE_TIME;

authoritative;

subnet $SUBNET netmask $NETMASK {
  range $RANGE_START $RANGE_END;
  option routers $ROUTER;
  option subnet-mask $NETMASK;
  option broadcast-address 192.168.18.255;
}
EOF

echo "[+] Specifying DHCP interface"
echo "INTERFACESv4=\"$INTERFACE\"" | sudo tee /etc/default/isc-dhcp-server > /dev/null

echo "[+] Enabling and starting DHCP server"
sudo systemctl enable isc-dhcp-server
sudo systemctl restart isc-dhcp-server

echo "[+] Checking DHCP server status"
sudo systemctl status isc-dhcp-server --no-pager

echo "[+] DHCP setup complete on \$HOSTNAME"

Make it executable and run it:

chmod +x setup_dhcp.sh
./setup_dhcp.sh

Troubleshooting Tips

  • Make sure the interface you're using isn't managed by NetworkManager or cloud-init if you're on a cloud VM.
  • Check /var/log/syslog or journal logs for DHCP errors:
sudo journalctl -u isc-dhcp-server
  • If you're running this inside a VM, ensure promiscuous mode is enabled for the interface in your hypervisor.

Conclusion

And that's it! You've now set up a functional DHCP server on your Ubuntu machine. This is incredibly useful for labs, PXE booting, or custom provisioning systems.

If you’re managing multiple servers or testbeds, consider integrating this setup into an Ansible playbook or cloud-init template for even more automation.

💬 Got questions or tips? Drop them in the comments or connect with me on LinkedIn or Twitter—let’s nerd out on networking! 🔌

Subscribe to LevelUp I.T. newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!