What is DHCP?
Dynamic Host Configuration Protocol (DHCP) is a network protocol used to automatically assign IP addresses and other network settings to devices. It eliminates the need for manually configuring IP addresses on each device. DHCP works on a client-server model, where the server provides network details to clients when they join the network. The protocol uses a lease system, where IP addresses are assigned for a limited time and can be reused. It operates over UDP using ports 67 (server) and 68 (client). DHCP greatly simplifies network management, especially in environments with many devices.
Why Use DHCP?
DHCP reduces administrative workload by automating IP allocation and avoiding IP conflicts. It allows devices to connect to the network without manual setup, making it ideal for dynamic and frequently changing networks. With DHCP, reconfiguring networks or adding new devices becomes faster. It also ensures that IP addresses are reused efficiently. Additionally, DHCP can distribute other settings like DNS and gateway addresses automatically. This leads to a more reliable and scalable network setup.
Where to Use DHCP?
DHCP is used in home networks, corporate offices, data centers, and public Wi-Fi hotspots. It is essential in large organizations where hundreds or thousands of devices connect and disconnect regularly. DHCP is also useful in lab environments where network configurations change frequently. Internet Service Providers (ISPs) use DHCP to assign IP addresses to customer routers. In cloud environments, DHCP helps in managing virtual machine networking. Essentially, DHCP is applicable anywhere IP address management needs to be efficient and automatic.
A DHCP server automatically assigns IP addresses to devices in your network. Follow these steps to set up a DHCP server on Linux.
1. Install DHCP Server
- Arch Linux:
sudo pacman -S dhcp
2. Create a Network Interface File
First, you need to check your network interface name. Using:
ip addr show
You will see something like this:
1. lo: ....
2. enp2s0: ...
Check which interface is showing after "2. ". Here in the example my interface is enp2s0
. Remember your interface name.
Now create a directory:
sudo mkdir -p /etc/systemd/network
Edit the network configuration file with the name of your network interface:
sudo vim /etc/systemd/network/enp2s0.network
Example configuration:
[Match]
Name=enp2s0
[Network]
Address=192.168.10.10/24
3. Enable and Start systemd-networkd
sudo systemctl enable systemd-networkd
sudo systemctl start systemd-networkd
Check your IP address:
ip addr show
4. Configure DHCP
Edit the DHCP configuration file:
sudo vim /etc/dhcpd.conf
Example configuration:
subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.100 192.168.10.200;
option routers 192.168.10.10;
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
}
This configuration will make your device IP 192.168.10.10
in that interface. And provide the connected devices an IP address within the range 192.168.10.100
to 192.168.10.200
.
Optional:
To make any computer IP address fixed, add these lines in /etc/dhcpd.conf
file:
host node1 {
hardware ethernet B0:83:FE:A3:80:18; #MAC address of the client computer network interface
fixed-address 192.168.10.160;
}
5. Start the DHCP Service
Start DHCP manually:
sudo dhcpd -4 -cf /etc/dhcpd.conf enp2s0
Congratulations! You have successfully configured a DHCP server on your own. Connect one or two computers to test the server. Best of luck.