Installing and Configuring a DHCP Server on Linux
Configuring the DHCP Server on Debian
In this article, you will learn how to configure the isc-dhcp-server
. Additionally, we will configure an IP reservation and set it up to operate on two different scopes.
Here is my DHCP server’s network configuration:
# Router external network
auto ens4
iface ens4 inet dhcp
# Internal network 1
auto ens5
iface ens5 inet static
address 192.168.10.1
netmask 255.255.255.0
# SNAT for ens5 network , ensure that bit of forwarding is activated.
post-up iptables -t nat -A POSTROUTING -o ens4 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -o ens4 -j MASQUERADE
# Internal network 2
auto ens6
iface ens6 inet static
address 192.168.20.1
netmask 255.255.255.0
Installing the isc-dhcp-server
To install the DHCP server on Debian, run the following command:
sudo apt update && sudo apt install isc-dhcp-server
When APT finishes, it will return an error indicating that the service is misconfigured. You can ignore this for now as we will configure it next.
Configuring the isc-dhcp-server service
First, we need to configure the network interface that the DHCP server will operate on. Edit the interface configuration file:
sudo nano /etc/default/isc-dhcp-server
Add the interface that will distribute IP addresses, for example for network1:
INTERFACESv4="ens5"
Then we configure the DHCP server with the following characteristics:
- IP address range:
192.168.10.100 - 192.168.0.110
- Subnet mask:
255.255.255.0
- Lease duration: 1 hour
- Gateway:
192.168.10.1
- DNS servers:
8.8.8.8
Edit the isc-dhcp-server configuration file:
sudo nano /etc/dhcp/dhcpd.conf
The most important parameters inside this file are:
- max-lease-time: IP address lease time
- default-lease-time: Lease renewal time
- option routers: Gateway IP address
- option domain-name-server: IP addresses of the DNS servers the client will use
- option domain-name: Domain name sent to the client
- option subnet-mask: Subnet sent to the clients
- option broadcast-address: Network broadcast address
Here is the configuration I will add for network1:
subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.100 192.168.10.110;
option routers 192.168.10.1;
option domain-name-servers 8.8.8.8;
default-lease-time 3600;
max-lease-time 3600;
}
With this configuration, the DHCP server will provide addresses within the specified range and assign the specified gateway and DNS server.
To apply the changes, restart the DHCP service:
sudo systemctl restart isc-dhcp-server
Configuring clients to obtain dynamic addresses
To allow clients to receive IP addresses dynamically, ensure their network settings are set to use DHCP.
On a Debian client
Edit the network configuration to use DHCP. Open the /etc/network/interfaces
file:
sudo nano /etc/network/interfaces
Ensure the interface is configured like this:
auto ens4
iface ens4 inet dhcp
Restart the network interface to apply changes:
debian@client1:~$ sudo ifdown ens4 && sudo ifup ens4
You should see the DHCP client’s output as it obtains its configuration:
ifdown: interface ens4 not configured
Internet Systems Consortium DHCP Client 4.4.3-P1
...
bound to 192.168.10.100 -- renewal in 1479 seconds.
You can also verify the configuration using:
debian@client1:~$ ip -4 a
...
2: ens4: ...
inet 192.168.10.100/24 brd 192.168.10.255 scope global dynamic ens4
💡 Tip:
If you need to modify the DHCP client configuration or simply request a new one, these commands are helpful.
On Windows:ipconfig /release
and thenipconfig /renew
On Linux:dhclient -r
to release anddhclient
to renew.
Checking address leases
To view the IP leases on the server, open the DHCP lease file, typically located at:
sudo cat /var/lib/dhcp/dhcpd.leases
Do not edit this file manually, as it may cause issues.
Reserving an IP address
To reserve an IP address for a specific client, edit the DHCP server configuration:
sudo nano /etc/dhcp/dhcpd.conf
Add the following to reserve based on the MAC address:
host client_name {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.10.120;
}
Restart the DHCP service:
sudo systemctl restart isc-dhcp-server
Verify on the client (Windows or Linux) that the reserved IP was correctly assigned using ipconfig
or ip a
.
Configuring two scopes
If you want the DHCP server to handle two networks, such as 192.168.10.0
and 192.168.20.0
, add another network interface.
Edit /etc/default/isc-dhcp-server
:
INTERFACESv4="ens5 ens6"
Add a second address range to the DHCP configuration file:
sudo nano /etc/dhcp/dhcpd.conf
Add:
subnet 192.168.20.0 netmask 255.255.255.0 {
range 192.168.20.100 192.168.2.110;
option routers 192.168.20.1;
option domain-name-servers 8.8.8.8;
default-lease-time 3600;
max-lease-time 3600;
}
Restart the DHCP service:
sudo systemctl restart isc-dhcp-server
Now, clients on the second network will receive the configured DHCP settings.