Skip to content

Dhcp

DHCP automates IP address assignment. This comprehensive chapter covers everything about DHCP.


DHCP (Dynamic Host Configuration Protocol) automatically assigns network configuration to devices.

+------------------------------------------------------------------+
| DHCP Overview |
+------------------------------------------------------------------+
Why DHCP?
+------------------------------------------------------------------+
Manual Configuration Problems:
+------------------------------------------------------------------+
1. Time-consuming for many devices
2. IP conflicts possible
3. Difficult to change network settings
4. No centralized tracking
DHCP Solutions:
+------------------------------------------------------------------+
1. Centralized management
2. Automatic IP assignment
3. Easy to change network settings
4. Prevents IP conflicts
5. Efficient use of addresses
What DHCP Provides:
+------------------------------------------------------------------+
1. IP Address
2. Subnet Mask
3. Default Gateway (Router)
4. DNS Servers (Primary and Secondary)
5. Domain Name
6. Lease Time
7. Additional Options (NTP, WINS, etc.)
+------------------------------------------------------------------+

DHCP uses a four-step process called DORA.

+------------------------------------------------------------------+
| DORA Process Deep Dive |
+------------------------------------------------------------------+
DHCP Four-Way Handshake:
+------------------------------------------------------------------+
Step 1: DISCOVER (Client -> Server)
+------------------------------------------------------------------+
Client broadcasts: "Is there a DHCP server?"
Packet details:
+------------------------------------------------------------------+
| Field | Value |
|----------------|--------------------------------|
| Source IP | 0.0.0.0 |
| Source Port | 68 |
| Dest IP | 255.255.255.255 |
| Dest Port | 67 |
| Transaction ID | Random number |
| Message Type | DHCP DISCOVER |
+------------------------------------------------------------------+
Step 2: OFFER (Server -> Client)
+------------------------------------------------------------------+
Server responds with available IP
Packet details:
+------------------------------------------------------------------+
| Field | Value |
|----------------|--------------------------------|
| Your IP | 192.168.1.100 (offered) |
| Subnet Mask | 255.255.255.0 |
| Router | 192.168.1.1 |
| DNS | 8.8.8.8 |
| Lease Time | 86400 seconds |
| Message Type | DHCP OFFER |
+------------------------------------------------------------------+
Step 3: REQUEST (Client -> Server)
+------------------------------------------------------------------+
Client requests the offered IP
Packet details:
+------------------------------------------------------------------+
| Field | Value |
|----------------|--------------------------------|
| Requested IP | 192.168.1.100 |
| Message Type | DHCP REQUEST |
+------------------------------------------------------------------+
Step 4: ACK (Server -> Client)
+------------------------------------------------------------------+
Server confirms the lease
Packet details:
+------------------------------------------------------------------+
| Field | Value |
|----------------|--------------------------------|
| Your IP | 192.168.1.100 |
| Lease Time | 86400 seconds |
| Message Type | DHCP ACK |
+------------------------------------------------------------------+
Complete Flow Diagram:
+------------------------------------------------------------------+
Client Server
| |
|------- DHCP DISCOVER ------> |
| (broadcast) |
| |
|<------ DHCP OFFER ----------- |
| (offer IP) |
| |
|------- DHCP REQUEST --------> |
| (request IP) |
| |
|<------ DHCP ACK ------------- |
| (confirmation) |
| |
|====== CONFIGURED ========== |
DHCP Nak (Negative Acknowledgment):
+------------------------------------------------------------------+
If server cannot fulfill request:
+------------------------------------------------------------------+
Server -> Client: DHCP NAK
+------------------------------------------------------------------+
Example: Client requested IP that server no longer has
Client then starts over with DISCOVER
+------------------------------------------------------------------+

+------------------------------------------------------------------+
| DHCP Message Types |
+------------------------------------------------------------------+
DHCP Messages:
+------------------------------------------------------------------+
| Type | Value | Direction | Description |
|------|-------|-------------|--------------------------------|
| DHCPDISCOVER | 1 | Client->Server | Find available servers |
| DHCPOFFER | 2 | Server->Client | Offer available IP |
| DHCPREQUEST | 3 | Client->Server | Request offered IP |
| DHCPDECLINE | 4 | Client->Server | IP already in use |
| DHCPACK | 5 | Server->Client | Confirm lease |
| DHCPNAK | 6 | Server->Client | Request rejected |
| DHCPRELEASE | 7 | Client->Server | Release IP early |
| DHCPINFORM | 8 | Client->Server | Client has IP, need config|
DHCP Packet Format:
+------------------------------------------------------------------+
+------------------------------------------------------------------+
| Field | Size | Description |
|-------------------|-----------|-----------------------------------|
| Operation | 1 byte | 1=request, 2=reply |
| Hardware Type | 1 byte | 1=Ethernet |
| Hardware Length | 1 byte | 6 for Ethernet |
| Hops | 1 byte | Relay agent hops |
| Transaction ID | 4 bytes | Matches requests/responses |
| Seconds | 2 bytes | Time since request started |
| Flags | 2 bytes | Broadcast/unicast flags |
| Client IP | 4 bytes | Client IP (if known) |
| Your IP | 4 bytes | IP being offered |
| Server IP | 4 bytes | Server IP |
| Gateway IP | 4 bytes | Relay agent IP |
| Client Hardware | 16 bytes | MAC address |
| Server Hostname | 64 bytes | Server name |
| Boot Filename | 128 bytes| Boot file name |
| Options | Variable | DHCP options |
+------------------------------------------------------------------+
DHCP Options (Common):
+------------------------------------------------------------------+
| Option | Code | Description | Example |
|--------|------|--------------------------------|---------------------|
| Subnet Mask | 1 | Network mask | 255.255.255.0 |
| Router | 3 | Default gateway | 192.168.1.1 |
| DNS | 6 | Name servers | 8.8.8.8 |
| Host Name | 12 | Client hostname | client-pc |
| Domain Name | 15 | Domain suffix | example.com |
| Broadcast | 28 | Broadcast address | 192.168.1.255 |
| NTP Server | 42 | Time servers | time.example.com |
| Lease Time | 51 | Lease duration | 86400 seconds |
| Message Type| 53 | DHCP message type | 1-8 |
| Server ID | 54 | DHCP server identifier | 192.168.1.1 |
| Parameter Req| 55 | Parameter request list | 1,3,6,15,28 |
| Renewal T1 | 58 | Renewal time | 43200 seconds |
| Rebinding T2| 59 | Rebinding time | 75600 seconds |
+------------------------------------------------------------------+

+------------------------------------------------------------------+
| DHCP Lease Lifecycle |
+------------------------------------------------------------------+
Lease Timeline:
+------------------------------------------------------------------+
T0: IP Assigned
|
| 50% of lease time (T1)
v
T1: Renewal Attempt
|
| If ACK: Lease renewed
| If no response: Continue using IP
|
| 87.5% of lease time (T2)
v
T2: Rebinding
|
| Broadcast to ANY server
| If ACK: Continue
| If NAK or no response: Start DORA
|
| 100% of lease time
v
T3: Lease Expired
|
| Must stop using IP
| Start DORA process
Lease Renewal States:
+------------------------------------------------------------------+
1. BOUND:
+------------------------------------------------------------------+
| IP address successfully assigned |
| Can use IP for duration of lease |
+------------------------------------------------------------------+
2. RENEWING:
+------------------------------------------------------------------+
| T1 reached (50% of lease) |
| Unicast to server to renew |
| Can continue using IP while waiting |
+------------------------------------------------------------------+
3. REBINDING:
+------------------------------------------------------------------+
| T2 reached (87.5% of lease) |
| Broadcast to any DHCP server |
| Last attempt before expiration |
+------------------------------------------------------------------+
4. REBOOTING:
+------------------------------------------------------------------+
| Lease expired or client restarted |
| Broadcast REQUEST for previously assigned IP |
| If NAK, start DORA |
+------------------------------------------------------------------+
Default Lease Times:
+------------------------------------------------------------------+
| Service | Default Lease | Maximum Lease |
|-----------------|---------------|---------------|
| Home router | 24 hours | 24 hours |
| Enterprise | 8 hours | 24 hours |
| Server | 24 hours | 7 days |
| Mobile device | 24 hours | 7 days |
+------------------------------------------------------------------+

+------------------------------------------------------------------+
| DHCP Server Configuration |
+------------------------------------------------------------------+
ISC DHCP Server (dhcpd):
+------------------------------------------------------------------+
# /etc/dhcpd.conf
# Global settings
default-lease-time 86400; # 1 day
max-lease-time 172800; # 2 days
authoritative; # This is authoritative server
# DNS settings
option domain-name "example.com";
option domain-name-servers 8.8.8.8, 8.8.4.4;
option ntp-servers time.example.com;
# Subnet declaration
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.50 192.168.1.200; # Address pool
option routers 192.168.1.1; # Default gateway
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option ntp-servers 192.168.1.1;
# Reserved addresses
host printer {
hardware ethernet 00:1a:2b:3c:4d:5e;
fixed-address 192.168.1.50;
}
}
# Another subnet
subnet 10.0.0.0 netmask 255.255.0.0 {
range 10.0.1.10 10.0.1.250;
option routers 10.0.0.1;
option domain-name-servers 10.0.0.53;
}
# Failover configuration
failover peer "dhcp-failover" {
primary;
address 192.168.1.1;
port 519;
peer address 192.168.1.2;
max-response-delay 30;
max-unacked-updates 10;
mclt 1800;
split 128;
load balance max seconds 3;
}
Start DHCP Server:
+------------------------------------------------------------------+
# Arch Linux
sudo systemctl enable dhcpd4
sudo systemctl start dhcpd4
# Check status
sudo systemctl status dhcpd4
DHCP Client Configuration:
+------------------------------------------------------------------+
systemd-networkd:
+------------------------------------------------------------------+
# /etc/systemd/network/eth0.network
[Match]
Name=eth0
[Network]
DHCP=ipv4
[DHCP]
UseDNS=yes
UseRoutes=yes
UseGateway=yes
dhcpcd:
+------------------------------------------------------------------+
# Install
sudo pacman -S dhcpcd
# Enable
sudo systemctl enable dhcpcd
sudo systemctl start dhcpcd
# For specific interface
sudo systemctl enable dhcpcd@eth0
sudo systemctl start dhcpcd@eth0
View DHCP Lease:
+------------------------------------------------------------------+
# Current leases
cat /var/lib/dhcp/dhcpd.leases
# New format
cat /var/lib/dhcp/dhcpd.leases~
# Client leases
cat /var/lib/dhcpcd/eth0.lease
+------------------------------------------------------------------+

+------------------------------------------------------------------+
| DHCP Relay Agent |
+------------------------------------------------------------------+
Problem:
+------------------------------------------------------------------+
DHCP broadcast doesn't cross routers by default
+------------------------------------------------------------------+
Solution: DHCP Relay Agent (IP Helper)
+------------------------------------------------------------------+
Router receives broadcast and forwards to DHCP server as unicast
Configuration (Cisco):
+------------------------------------------------------------------+
interface GigabitEthernet0/0
ip helper-address 192.168.1.100
Multiple DHCP servers:
+------------------------------------------------------------------+
interface GigabitEthernet0/0
ip helper-address 192.168.1.100
ip helper-address 192.168.1.101
Linux DHCP Relay (relayd):
+------------------------------------------------------------------+
# Install
sudo pacman -S dhcp
# Run relay
sudo dhcrelay -4 -i eth0 192.168.1.100
# With multiple servers
sudo dhcrelay -4 -i eth0 192.168.1.100 192.168.1.101
+------------------------------------------------------------------+

+------------------------------------------------------------------+
| DHCP Security |
+------------------------------------------------------------------+
Attacks:
+------------------------------------------------------------------+
1. Rogue DHCP Server:
+------------------------------------------------------------------+
Attack: Malicious DHCP server responds to requests
Result: Users get wrong IP, gateway, DNS
Defense:
+------------------------------------------------------------------+
| - Port security on switches |
| - DHCP snooping (Cisco) |
| - 802.1X authentication |
+------------------------------------------------------------------+
2. DHCP Starvation:
+------------------------------------------------------------------+
Attack: Request all available IPs
Result: Legitimate users can't get IPs
Defense:
+------------------------------------------------------------------+
| - Rate limiting |
| - Port security |
| - DHCP snooping |
+------------------------------------------------------------------+
DHCP Snooping (Cisco):
+------------------------------------------------------------------+
# Enable on switch
ip dhcp snooping
# On specific VLAN
ip dhcp snooping vlan 10,20
# Configure trusted ports
interface GigabitEthernet0/1
ip dhcp snooping trust
# Verify
show ip dhcp snooping
+------------------------------------------------------------------+

In this chapter, you learned:

  • DHCP fundamentals and why use it
  • DORA process in detail
  • DHCP message types
  • Lease lifecycle
  • Server and client configuration
  • DHCP relay
  • DHCP security

Chapter 12: NAT & PAT


Last Updated: February 2026