Setting Up dhcpd in Linux
For the example in this article, you should have an interface already setup with a static IP address of 192.168.1.1/24
Installation
For RPM-based systems (e.g., CentOS, Fedora, RHEL):
Code:
sudo yum install dhcp
For DEB-based systems (e.g., Debian, Ubuntu):
Code:
sudo apt-get install isc-dhcp-server
Opening Needed Firewall Ports
To allow DHCP traffic through the firewall, you need to open ports 67 (DHCP server) and 68 (DHCP client).For firewalld (common in CentOS, Fedora, RHEL):
Code:
sudo firewall-cmd --permanent --add-port=67/udp
sudo firewall-cmd --permanent --add-port=68/udp
sudo firewall-cmd --reload
For ufw (common in Debian, Ubuntu):
Code:
sudo ufw allow 67/udp
sudo ufw allow 68/udp
sudo ufw reload
Enabling and Starting the DHCP Service
For systemd-based systems:
Code:
sudo systemctl enable dhcpd
sudo systemctl start dhcpd
For SysVinit-based systems:
Code:
sudo service dhcpd start
sudo chkconfig dhcpd on
Example /etc/dhcpd/dhcpd.conf
Here's a basic example of a dhcpd.conf file:
Code:
# Sample /etc/dhcpd/dhcpd.conf
default-lease-time 600; # 10 minutes
max-lease-time 7200; # 2 hours
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.3 192.168.1.250;
option routers 192.168.1.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 8.8.8.8, 8.8.4.4;
option domain-name "example.com";
}
Lease Time and Max Lease Time
- Lease Time (default-lease-time): This is the default duration (in seconds) for which a DHCP lease is granted to a client if the client does not request a specific lease duration. For example, if default-lease-time is set to 600, the lease will last for 600 seconds (10 minutes) unless the client requests a different duration.
- Max Lease Time (max-lease-time): This is the maximum duration (in seconds) for which a DHCP lease can be granted, regardless of the client's request. For example, if max-lease-time is set to 7200, the lease cannot exceed 7200 seconds (2 hours).
Lease Expiration
When a DHCP lease is granted, it has a specific expiration time based on the lease duration. The lease expiration process works as follows:- Lease Acquisition: When a client first acquires a lease, it is valid for the duration specified by the DHCP server (either the default lease time or the requested lease time, up to the max lease time).
- Lease Renewal: Before the lease expires, the client will attempt to renew it. Typically, the client starts the renewal process when 50% of the lease time has elapsed. If the renewal is successful, the lease duration is reset, and the client continues to use the same IP address.
- Lease Rebinding: If the client cannot renew the lease with the original DHCP server, it will attempt to rebind the lease with any available DHCP server. This usually occurs when 87.5% of the lease time has elapsed.
- Lease Expiration: If the client cannot renew or rebind the lease, the lease will expire at the end of the lease duration. The client must then stop using the IP address and request a new lease from the DHCP server.
Lease File Location
The DHCP server stores lease information in the /var/lib/dhcpd/dhcpd.leases file. This file is automatically managed by the DHCP server and contains information about current leases.Example IPv6 /etc/dhcp/dhcpd6.conf
Here's an example configuration for IPv6:
Code:
# Sample IPv6 /etc/dhcp/dhcpd6.conf
default-lease-time 600;
max-lease-time 7200;
subnet6 2001:db8::/64 {
range6 2001:db8::1000 2001:db8::2000;
option dhcp6.name-servers 2001:db8::1;
option dhcp6.domain-search "example.com";
}
Multiple Interfaces
If your system has multiple network interfaces with IP addresses on different subnets, dhcpd will automatically determine which interfaces to use based on the subnet configurations specified in the dhcpd.conf or dhcpd6.conf files. This ensures that the DHCP server assigns IP addresses appropriately for each subnet.I hope this article helps you set up dhcpd on your Linux system!