G
GuideDevOps
Lesson 12 of 28

DHCP

Part of the Networking Basics tutorial series.

DHCP is the protocol that automatically assigns IP addresses to devices when they join a network. Without DHCP, every device would need manual IP configuration—a DevOps nightmare at scale.

What is DHCP?

DHCP (Dynamic Host Configuration Protocol) automates the assignment of:

  • IP Address (from a pool)
  • Subnet Mask (network boundaries)
  • Default Gateway (router address)
  • DNS Servers (domain name resolution)
  • Lease Time (how long the address is valid)

Without DHCP:

Every device needs manual config
192.168.1.100 → remember this
Subnet: 255.255.255.0 → remember this
Gateway: 192.168.1.1 → remember this
 → Tedious, error-prone, unscalable

With DHCP:

Device boots → sends DHCP request
DHCP server responds → "Here's your config"
Device gets: 192.168.1.100, mask, gateway, DNS
 → Automatic, consistent, scalable

DHCP Process (DORA)

DHCP uses a 4-step process called DORA (Discover, Offer, Request, Acknowledge):

Step 1: DISCOVER

Client: "Is any DHCP server out there?"
         (Broadcasts to 255.255.255.255:67)

Step 2: OFFER

DHCP Server: "I'm here! I have IP 192.168.1.100 for you"
             (Offers IP from available pool)

Step 3: REQUEST

Client: "I'll take 192.168.1.100!"
        (Accepts the specific offer)

Step 4: ACKNOWLEDGE

DHCP Server: "Done! You get 192.168.1.100 for 24 hours"
             (Confirms and sets lease time)

DHCP Server vs Client

DHCP Server:

  • Maintains pool of IP addresses
  • Tracks which IPs are in use
  • Assigns and renews leases
  • Responds to client requests
  • Examples: ISC DHCP, Microsoft DHCP, Dnsmasq

DHCP Client:

  • Requests configuration on boot
  • Renews lease periodically
  • Every end device (PC, laptop, phone, server)
  • Requests IP 87,600 times/year if lease = 1 hour!

DHCP Scopes and Pools

Scope: Range of IP addresses available for lease

Network: 192.168.1.0/24 (256 addresses total)

DHCP Scope: 192.168.1.100 - 192.168.1.254
Reserved for servers: 192.168.1.1 - 192.168.1.99
  ├─ 192.168.1.1 = Gateway/Router
  ├─ 192.168.1.2 = DNS Server
  ├─ 192.168.1.3-10 = Reserved for future expansion
  ├─ 192.168.1.11-99 = Reserved for static servers
  └─ 192.168.1.100-254 = Available for DHCP (155 addresses)

Lease Time

Lease Time: How long a client can use an IP address before renewing.

DurationUse CaseRenewal Interval
5 minutesTemporary networksVery frequent
1 hourConference/event networks~30 min
8 hoursOffice networks~4 hours
24 hoursStable networks~12 hours
7 daysHome networks~3.5 days

Renewal Timeline:

Client gets lease (24 hours)
↓
After 12 hours → Sends renewal request
↓
If server responds → Lease renewed, client keeps IP
↓
If no response after 18 hours → Client panics
↓
After 21 hours → Client can request new IP
↓
After 24 hours → IP expires, lease ends

DHCP Configuration

ISC DHCP Server Config (/etc/dhcp/dhcpd.conf):

subnet 192.168.1.0 netmask 255.255.255.0 {
  # DHCP pool
  range 192.168.1.100 192.168.1.254;
  
  # Default gateway
  option routers 192.168.1.1;
  
  # DNS servers
  option domain-name-servers 8.8.8.8, 8.8.4.4;
  
  # Lease time (in seconds)
  default-lease-time 86400;      # 24 hours
  max-lease-time 172800;          # 48 hours
  
  # Domain name
  option domain-name "office.local";
}

# Static assignment (MAC address → fixed IP)
host myserver {
  hardware ethernet AA:BB:CC:DD:EE:FF;
  fixed-address 192.168.1.50;
}

Starting DHCP Server:

# Ubuntu/Debian
sudo service isc-dhcp-server start
 
# Verify it's running
sudo systemctl status isc-dhcp-server
 
# Check lease file
cat /var/lib/dhcp/dhcpd.leases

DHCP Client Configuration (/etc/netplan/01-netcfg.yaml):

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true          # Enable DHCP
      dhcp-identifier: mac # Use MAC address as ID

Apply config:

sudo netplan apply

Viewing DHCP Status

Check assigned IP:

ip addr show eth0
# or
ifconfig eth0

View DHCP lease info:

cat /var/lib/dhcp/dhclient.leases
 
# Example output:
# lease {
#   interface "eth0";
#   fixed-address 192.168.1.150;
#   option subnet-mask 255.255.255.0;
#   option routers 192.168.1.1;
#   expire date 4 2026/04/15 08:30:45;
# }

Force DHCP renewal:

sudo dhclient -r eth0    # Release current lease
sudo dhclient eth0       # Request new lease

DHCP in Kubernetes

Kubernetes doesn't use DHCP for Pod IPs—instead, CNI plugins assign IPs from configured ranges:

# Pod CIDR configuration
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
networking:
  podSubnet: "10.244.0.0/16"
  
# Each node gets a /24 subnet
Node 1: 10.244.1.0/24
Node 2: 10.244.2.0/24
Node 3: 10.244.3.0/24

DHCP vs Static IP

AspectDHCPStatic IP
AssignmentAutomaticManual
ReliabilityCan changeAlways same
AdministrationAuto-managedManual management
Use CaseClients, temporary devicesServers, infrastructure
FlexibilityEasy to add/remove devicesHarder to scale

DevOps Practice:

Dynamic: Compute nodes, containers, test machines
Static: Database servers, load balancers, DNS servers

DHCP Best Practices

1. Reserve IP Ranges for Infrastructure

Total: 192.168.1.0/24 (256 addresses)
├─ Reserved for servers: .1-.99
├─ DHCP pool: .100-.200
└─ Future expansion: .201-.254

2. Use Appropriate Lease Times

✓ Long leases (24h) for stable infrastructure
✓ Short leases (1h) for temporary/test networks
✗ Don't use 10-minute leases unless needed

3. Configure DHCP Failover

Primary DHCP Server (192.168.1.2)
    ↓
Secondary DHCP Server (192.168.1.3)

If primary fails, secondary takes over
Clients can renew leases with secondary

4. Static IPs for Servers

  • ✓ Assign fixed IPs to servers (even if getting from DHCP)
  • ✓ Prevents IP changes from breaking infrastructure
  • ✗ Don't rely on DHCP for persistent server IPs

5. Monitor DHCP Activity

# Check DHCP server logs
tail -f /var/log/syslog | grep DHCP
 
# Count active leases
grep "binding state" /var/lib/dhcp/dhcpd.leases | wc -l

Common DHCP Issues

"Device can't get IP"

1. Check DHCP server is running
   sudo systemctl status isc-dhcp-server

2. Verify pool has available IPs
   cat /var/lib/dhcp/dhcpd.leases

3. Check device can reach DHCP server
   → May be blocked by firewall/ACL

4. Restart DHCP client
   sudo dhclient -r; sudo dhclient eth0

"IP conflicts on the network"

1. Check DHCP scope overlaps with static IPs
   Scope: 100-254
   Static: 1-99 (must not overlap!)

2. Verify no duplicate static assignments
   grep fixed-address /etc/dhcp/dhcpd.conf

3. Check for rogue DHCP servers
   Use dhcp-monitor or nmap to scan

"Client keeps getting different IPs"

1. Lease time too short
   Increase default-lease-time in config

2. Client not sending DHCP-ID consistently
   Check MAC address doesn't change

3. Set static IP for that device
   Use hardware ethernet (MAC) matching

Key Concepts

  • DHCP = Protocol for automatic IP assignment
  • Scope = Range of IPs available for lease
  • Lease = Time period before IP expires
  • DORA = Discovery, Offer, Request, Acknowledge
  • Renewal = Client requests to keep same IP
  • Static = Fixed IP for important servers
  • Pool = Available IPs to assign
  • Failover = Secondary DHCP takes over if primary fails
  • Always reserve static IPs for infrastructure
  • Monitor DHCP activity and lease utilization