DevOps Journey

DHCP

Dynamic Host Configuration Protocol

DHCP

DHCP automatically assigns IP addresses and network configuration to devices.

How DHCP Works

Four-Step Process (DORA)

1. DISCOVER: Client broadcasts "Does anyone have an IP for me?"
2. OFFER: DHCP server responds with available IP
3. REQUEST: Client requests that specific IP
4. ACKNOWLEDGE: Server confirms and assigns IP

What DHCP Assigns

  • IP address
  • Subnet mask
  • Default gateway
  • DNS servers
  • DHCP lease time
  • NTP server
  • Domain name

DHCP Ports

Port 67 - Server (listens for client broadcasts)
Port 68 - Client (sends requests)
Protocol: UDP

DHCP Lease

Lease Time

  • Typical: 24 hours
  • After 50% time: Client tries to renew
  • If renewal fails: Tries at 87.5%
  • If no renewal: Address released at 100%

Renewal

# Release and renew IP
sudo dhclient -r # release
sudo dhclient eth0 # request new IP

# Force renewal
sudo dhclient -n eth0

DHCP Server Setup

ISC DHCP Server

# Install
sudo apt install isc-dhcp-server

# Configuration
/etc/dhcp/dhcpd.conf

Sample Configuration

subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.100 192.168.1.200;
    option routers 192.168.1.1;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
    option domain-name "example.com";
    default-lease-time 3600;
    max-lease-time 7200;
}

host server1 {
    hardware ethernet 00:11:22:33:44:55;
    fixed-address 192.168.1.50;
}

DHCP Client Configuration

Linux Configuration

# Automatic (default)
# /etc/network/interfaces
auto eth0
iface eth0 inet dhcp

# Restart networking
sudo systemctl restart networking

View DHCP Info

# Show current lease
cat /var/lib/dhcp/dhclient.eth0.leases

# Show interface config
ip addr show
ifconfig

# Check DHCP server
echo "DHCP server available"

Dnsmasq (Simple DHCP)

# Install
sudo apt install dnsmasq

# Configuration
/etc/dnsmasq.conf

Simple Configuration

# Listen on interface
interface=eth0

# DHCP range
dhcp-range=192.168.1.100,192.168.1.200,12h

# Gateway
dhcp-option=3,192.168.1.1

# DNS
dhcp-option=6,8.8.8.8,8.8.4.4

Troubleshooting

No IP Address Assigned

# Check DHCP client status
sudo systemctl status isc-dhcp-client

# Try renewing
sudo dhclient -v eth0

# Check logs
sudo tail -f /var/log/syslog | grep dhcp

Slow DHCP

# DHCP discover timeout is common
# Check default gateway
ip route show

# Verify DHCP server reachable
ping 192.168.1.1

Best Practices

  • Use appropriate lease times
  • Reserve addresses for servers
  • Monitor DHCP pool usage
  • Implement DHCP snooping for security
  • Use redundant DHCP servers
  • Monitor DHCP logs
  • Configure correct scopes
  • Keep DHCP server updated

On this page