DHCP on Windows Servers – Why are the expired IP addresses not getting re-assigned?
Introduction
DHCP is a network protocol that allows dynamic IP allocation to IP clients. This protocol has made the administration and the management of IP addresses within a company easier to do and maintain. This Wiki article focuses on the expiration and cleanup of DHCP leases and provides a clear view to administrators using Windows Server DHCP service about how DHCP database cleanup works in Windows and how it can be tuned.
Expiration of DHCP IP addresses
The expiration of DHCP IP addresses can be configured per DHCP scope. An administrator can specify limited and unlimited duration as shown in the figure below:
When an IP is assigned with a limited duration, there are two ways to get its expiration date and time:
- On the client device using ipconfig /all command for Windows systems
- On the DHCP server by going to Address Leases within a DHCP scope
As you can see in the two previous figures, the client and the DHCP server report different values for expiration time of the IP address:
- The server reports that the lease expiration will be on 6/26/2014 at 8:51:46 PM
- The client reports that the lease expiration will be on 6/26/2014 at 9:44:07 PM
There is then a mismatch between what the client and the server are reporting: The client will release the IP address 53 minutes after the DHCP server does it.
Imagine that the DHCP server cleanup the IP lease immediately after its expiry to allocate it to another client. What happens is that a duplicate IP address conflict will occur if the IP address gets assigned to another client. Hopefully, Windows DHCP service adds, by default, a grace period after the expiry of an IP lease. A DHCP IP lease gets then scavenged only if the expiration time and the grace period are over.
Windows DHCP grace period
Windows DHCP has a default grace period of four (4) hours. It protects a client lease in the following cases:
- The client and server are in different time zones
- The internal clocks of the client and server computers are not synchronized
- The client is off the network when the lease expires
By adding a grace period, the following is expected:
- When a DHCP IP address expires, it will disappear from dhcpmgmt.msc administrative tool UI
- You can get the expired IPs only by querying the DHCP database. This is feasible by using Get-DHCPServerv4Lease and Get-DHCPServerv6Lease cmdlets (Available since the release of Windows Server 2012 R2 and PowerShell 4.0). Below is an example of a script that can be used for get the expired IP addresses for an IPv4 DHCP scope (You will need to replace $computername variable value with the server name and $scopeid with the IP address of the DHCP scope):
$computername = “Server1” $scopeid = “x.x.x.x” import-module DHCPServer foreach ($object in Get-DhcpServerv4Lease –ComputerName $computername –ScopeId $scopeid) { if ($object.leaseExpiryTime –le (Get-Date)) { $object } } |
- If the scope reaches 90 percent or more IP addresses in use, it will display a warning icon and this might not match what is shown under Address Leases in dhcpmgmt.msc administrative tool (This is usually what confuses administrators and this is due to the fact that expired IP addresses are not displayed)
You can shorten the lease grace period by creating or updating LeaseExtension DWORD registry key under HKLM/System/CurrentControlSet/Services/DHCPServer/Parameters. The value can be specified in Decimal and should be in minutes.
Remark: Please note that having a shorter lease grace period increases the risk of having the duplicate IP problem mentioned previously.
Windows DHCP Database Cleanup Interval:
Windows DHCP does the cleanup of its database on regular intervals (By default, every one (1) hour). A DHCP lease that expired becomes eligible for deletion when the grace period is over. The IP lease gets then removed in the next cleanup cycle.
You can shorten the database cleanup interval by updating DatabaseCleanupInterval DWORD registry key under HKLM/System/CurrentControlSet/Services/DHCPServer/Parameters.
Below is the summary of how an IP lease gets removed from the DHCP database:
Why is it important to know the IP lease cleanup process in Windows?
Some administrators do not take in consideration the grace period and the frequency of DHCP database cleanup. This usually ends with problems especially for very dynamic DHCP scopes (Example: DHCP scopes used for Wifi). Such scopes are, in most of the cases, configured to have short lease durations (Example: 30 minutes) and the common mistake is to estimate the number of clients that can connect and how long a lease remains assigned to a client using only the lease duration when doing calculation.
Below is an example of scenario:
CONTOSO is a company that would like to implement Wifi in a branch office to support 200 users connected at the same time.
CONTOSO network administrator has then configured 192.168.0.1 – 192.168.0.253 as address pool (It contains 253 IP addresses) and 30 minutes as lease duration for Wifi. He noticed that the DHCP scope gets very quickly in warning state and was wondering what is getting wrong. CONTOSO network administrator has then contacted Microsoft Support for assistance.
The behavior that CONTOSO network administrator noticed is normal as the IP lease gets eligible for cleanup 4 hours and 30 minutes after its allocation or last renewal. CONTOSO network administrator has made the sizing by using 30 minutes only as lease duration which caused the unexpected behavior.
It is then important that administrators take in consideration or tune the parameters for the DHCP grace period and cleanup cycle.
Conclusion
This Wiki article explained the process used to remove an IP lease. It shows the different steps that are involved and how a tuning could be done. By taking in consideration the different involved steps, a DHCP administrator should be capable to provide a correct sizing and configuration for DHCP.