Introduction

DHCP, or Dynamic Host Configuration Protocol is a networking protocol that you most likely use every day on almost all of your devices. If you don’t have to set a static IP address for your devices, odds are they are set with DHCP. DHCP is not just for IP address, subnet mask, and Gateway, however. DHCP provides information you typically don’t look at, for example: NTP servers, DNS servers, FTP and configuration servers for devices such as desk phones, and many other services that can be set using custom “option sets.” Proper configuration of DHCP is critical to maintaining a properly functioning and streamlined networking environment.

How it works

DHCP is one of those great protocols that has been around so long, it just “works” most of the time. Occasionally, there will be instances of IP address conflicts or the wrong information being propagated to the network, but these types of problems are usually resolved by simple configuration changes. This being said, it is a good idea to understand how DHCP works in case you do encounter a situation where DHCP decides to exhibit strange behavior on your network. The process is as follows:

1.DHCPDiscover: A client device broadcasts a DHCP Discover message to all devices on the network asking for a DHCP server and address.

2.A DHCP server on the network receives this message and sees the request. It checks its current leases and finds an available IP address.

3.DHCPOffer: The server then provides a “DHCP offer” message for the client.

4.DHCPRequest: The client then sends a “DHCP request” packet back to the server letting it know it has chosen the IP offered to it.

5.DHCPACK: The Server responds with a DHCP ACK to acknowledge the request and send over any other DHCP options that have been set. This message is still a broadcast because the client doesn’t have an IP address officially assigned to it yet.

This process sounds complicated, but typically happens in seconds or even milliseconds. I have performed a packet capture to illustrate this process in action:

 

user_58180_58700f5468690.png_800.jpg

 

 

 

As you can see in the “Time” column, the entire process takes less than a second for everything to complete and my computer to be assigned an IP address!

Configuration

You may have noticed a few words I haven’t talked about yet in the previous example, such as “lease” and “scope.” I am going to go over many of the common DHCP terminology here.

Pool/Scope

The IP addresses available for clients to request. Many home networks today use a 192.168.x.0/24, like 192.168.1.0/24. This provides an address range of 192.168.1.1 – 192.168.1.254. Any address that is not taken in this range is available to be “leased” to clients on the network.

Lease

A lease is an IP address given to a client. This is called a lease because it expires after a certain period of time and can return to the address pool if necessary. Typically, a client will continue requesting the same leased IP address at half the configured lease time. For instance, if an address is leased to a device for 24 hours, it will request the address again at 12 hours to prevent the address from returning to the pool and being used by another device. If this request does not happen (perhaps if the device has left the network), the IP address will return to the pool and be reassigned to another device if one requests.

IP Reservation

With an IP reservation, you can instruct the DHCP server to always assign the same address to a device using that device’s MAC address. A MAC address is the hardcoded or virtual hardware address used by a device’s network interface card (NIC).

IP Conflict

An IP conflict occurs when 2 or more devices on a network attempt to request the same IP address. This typically happens when one device has a static IP address set that is within the address pool and another device attempts to request that IP address. To avoid this, it is best to configure an IP reservation for the static client or modify the address scope such that they do not contain the IP address statically assigned to the client. For instance, if a client is given 192.168.1.10, set the scope to only provide IP addresses from 192.168.1.11 – 192.168.1.253. This will allow all of the addresses below 192.168.1.11 to be statically assigned without conflicting with a DHCP host.

DHCP Option Sets

As previously stated, DHCP can provide other information aside from just the typical IP address, subnet mask, and DNS records. These option sets can help minimize the amount of manual work you have to do to maintain a network that contains many servers.

DHCP options are configured using an “Option Code” followed by the required information. Here is an example of a DHCP option set configured on an AWS VPC network:

 

user_58180_58700f7b1def4.png_800.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This network is configured to use different DNS servers than what are automatically assigned. These are Google public DNS servers and are not typically configured in an environment like this. I have also configured a custom NTP server. These settings will propagate to any client configured for DHCP within this VPC.

I have also taken a packet capture to show some option sets on my local network:

 

user_58180_58700faa7c42f.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

As you can see:

DHCP Option 54 is the DHCP Server of my router with IP address 192.168.1.1

DHCP Option 51 is the IP address lease time, which is set to 1 day.

DHCP Option 1 is the subnet mask, which is 255.255.255.0 or a /24.

DHCP Option 3 is the Router or Gateway, which is also 192.168.1.1

DHCP Option 6 is the DNS Server, also 192.168.1.1

I can change any of these within my router to send different information to DHCP clients.

Troubleshooting

Luckily, DHCP is a fairly straightforward protocol to troubleshoot. Many issues can be resolved by simply forcing the client to request a new IP address with a DHCP release command and DHCP renew command appropriate for the OS you are using.

The commands for Linux are:

dhclient –r 

to release and

dhclient 

to renew

Running these commands will force the client to request settings again. Doing so will help if there is an IP address conflict you have manually resolved, or if you have changed the DHCP configuration since the last lease renewal.

You can also add a “-v” switch to these commands. The “v” or “verbose” switch actually shows the leasing process! You can see the discover, request, offer, and other DHCP information as the system obtains a new lease.

Feel free to try this now! Just make sure you have a machine you can access “Out of Band” (Without internet) such as a local machine. If you run this on a machine that can’t be accessed without the internet, such as a Lab server, you will have to recreate it if you run the release command independently.

Here, I have run the commands and instructed them to run one after the other:

[root@localhost ~]# dhclient -r -v && dhclient -v
Internet Systems Consortium DHCP Client 4.2.5
Copyright 2004-2013 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eno16777736/00:0c:29:7b:7d:d5
Sending on LPF/eno16777736/00:0c:29:7b:7d:d5
Sending on Socket/fallback
DHCPRELEASE on eno16777736 to 192.168.1.1 port 67 (xid=0x28f2d183)
Internet Systems Consortium DHCP Client 4.2.5
Copyright 2004-2013 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eno16777736/00:0c:29:7b:7d:d5
Sending on LPF/eno16777736/00:0c:29:7b:7d:d5
Sending on Socket/fallback
DHCPDISCOVER on eno16777736 to 255.255.255.255 port 67 interval 8 (xid=0x1e1291f)
DHCPREQUEST on eno16777736 to 255.255.255.255 port 67 (xid=0x1e1291f)
DHCPOFFER from 192.168.1.1
DHCPACK from 192.168.1.1 (xid=0x1e1291f)
bound to 192.168.1.114 -- renewal in 40402 seconds.

As you can see, the DHCP lease is released first. It then performs the DHCPDISCOVER, DHCPREQUEST, DHCPOFFER, AND DHCPACK steps before receiving another IP address!

Conclusion

That about does it for the intro to DHCP! DHCP is an incredibly powerful tool that can simplify even the largest networks as long as it is configured properly! So get out there and set it up!

Comments are disabled for this guide.