To set up a DHCP server on Ubuntu, first install the isc-dhcp-server package, which provides the ISC DHCP server daemon. Run the following commands to update the package repository and install the software:
sudo apt update && sudo apt install isc-dhcp-server -y
This installs the necessary binaries, configuration files, and dependencies for the DHCP server.
The main configuration file for the ISC DHCP server is /etc/dhcp/dhcpd.conf. You need to define global options (applied to all subnets) and subnet-specific settings (IP ranges, gateways, DNS servers).
Add these lines at the top of the file to set default values for all subnets:
default-lease-time 600; # Default lease time (10 minutes)
max-lease-time 7200; # Maximum lease time (2 hours)
option domain-name "example.com"; # Your domain name
option domain-name-servers 8.8.8.8, 8.8.4.4; # Public DNS servers
authoritative; # Declare this server as authoritative for the network
default-lease-time: Time (in seconds) a client can use an IP before renewal.max-lease-time: Maximum time a client can hold an IP (even if it requests a longer lease).authoritative: Ensures the server responds to all DHCP requests in its network (prevents rogue servers).Define a subnet block for your local network. Replace the example values with your actual network details (e.g., 192.168.1.0/24):
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200; # Available IP range for dynamic allocation
option routers 192.168.1.1; # Default gateway (router) IP
option subnet-mask 255.255.255.0; # Subnet mask for the network
}
range: Defines the pool of IPs the server can assign to clients.option routers: The gateway IP clients use to access other networks (e.g., the internet).option subnet-mask: Identifies the network portion of IP addresses.The DHCP server must be configured to listen on the correct network interface (e.g., eth0, enp0s3). Edit the /etc/default/isc-dhcp-server file and modify the INTERFACESv4 line:
INTERFACESv4="eth0" # Replace 'eth0' with your actual interface name
To find your interface name, run ip a or ifconfig—look for the interface connected to your local network.
After configuring the files, start the DHCP server and enable it to launch at boot:
sudo systemctl start isc-dhcp-server # Start the service
sudo systemctl enable isc-dhcp-server # Enable auto-start on boot
Check the service status to ensure it’s running correctly:
sudo systemctl status isc-dhcp-server
A status of active (running) indicates the server is operational.
To confirm the server is assigning IPs, connect a client device (e.g., a laptop or another Ubuntu machine) to the same network and configure it to use DHCP:
/etc/netplan/01-netcfg.yaml (or the appropriate Netplan file) and set the interface to use DHCP:network:
version: 2
renderer: networkd
ethernets:
eth0: # Replace with your client interface name
dhcp4: yes
Apply the changes with sudo netplan apply.ip addr show eth0 (replace eth0 with your interface). Look for an IP address within the range specified in the server’s subnet block (e.g., 192.168.1.100–192.168.1.200).To assign a fixed IP to a specific device (e.g., a printer or server), add a host block to the /etc/dhcp/dhcpd.conf file. Use the device’s MAC address (find it with ip link show on the client) and desired static IP:
host printer1 {
hardware ethernet 00:1a:2b:3c:4d:5e; # Replace with the client's MAC address
fixed-address 192.168.1.50; # Static IP to assign
}
Save the file and restart the DHCP server to apply changes:
sudo systemctl restart isc-dhcp-server
If clients aren’t receiving IPs, check the following:
sudo ufw allow 67/udp
sudo ufw allow 68/udp
dhcpd.conf file with sudo dhcpd -t (this checks for errors without starting the server).journalctl -u isc-dhcp-server.service -f
INTERFACESv4 value in /etc/default/isc-dhcp-server matches the interface connected to the network.By following these steps, you can set up a functional DHCP server on Ubuntu to automate IP address allocation for your local network.