Understanding DHCP – How Dynamic IP Allocation Works

Understanding DHCP – Dynamic Host Configuration Protocol

When a host gets connected to a network, it needs some basic configurations like IP address, subnet mask, gateway IP, DNS server’s IP and so on. These can be given manually, but it will be difficult to do this every time, especially when the device moves from one network to another. This is where DHCP helps. It will provide all these parameters automatically when a host connects to the network.

In a DHCP setup, we will have a DHCP server with a pool of IP addresses. From this pool, the server will allocate IPs to the clients who request them. Along with the IP, the server will also provide other details like mask, gateway, DNS, and lease time. DHCP works on UDP, with port 67 used at the server side and 68 at the client side.

How the DHCP process works

DHCP DORA process diagram showing Discover, Offer, Request, and Acknowledge packet flow with broadcast, unicast, and port numbers between client and server.
DHCP DORA Process

When a client connects to a network (like switching on a computer, turning on Wi-Fi, or plugging in a cable), the DHCP client inside the device will automatically start. It will send a DHCP Discover packet to request an IP. This is sent as a broadcast packet, so every device on the network will receive it. But only the DHCP server, which listens on port 67, will respond.

The server replies with a DHCP Offer packet, which contains all the details needed for the client to start working: IP, mask, gateway, DNS, etc.

In many networks, there will be more than one DHCP server for redundancy. So the client may get multiple Offer packets. Normally, the client will choose the first offer it receives. But here is a point to note: every server that sent an offer will temporarily reserve one IP for that client. Now the client has to inform the servers which offer it has accepted. For this, it sends a DHCP Request packet, where it mentions the IP it is accepting and also the server’s IP.

The server from which the client accepted the IP will then send back a DHCP Acknowledgement packet. At this point, the process is complete and the client starts using the allocated IP.

This entire sequence: Discover, Offer, Request, Acknowledge is what we call the DORA process in DHCP.

Example in real life

Take your home Wi-Fi router. The router acts as a DHCP server. When you connect your phone:

  • The phone sends a Discover message.
  • The router replies with an Offer (with an IP something like 192.168.1.1)
  • The phone sends a Request asking to use that IP.
  • The router replies with an Acknowledgement.

Now the phone has an IP and is ready to use the internet. You didn’t configure anything – DHCP did it for you.


DHCP Lease Time and Renewal

When a DHCP server gives an IP address to a client, it is not permanent. The IP is given only for a fixed duration, and this period is called the lease time.

If the client wants to continue using the same IP for longer, it must renew the lease. This renewal process also helps the server keep track of whether the client is still connected to the network. If the client disconnects and does not renew, the server can take back that IP and assign it to someone else.

Renewal Timers – T1 and T2

DHCP uses two important timers for the renewal process: T1 and T2.

  • T1 (Renewal Timer)
    • This is set at 50% of the lease time.
    • When the client has completed half of its lease period, it sends a DHCP Request packet directly to the server (unicast).
    • If the server is available and willing to renew, it replies with a DHCP Acknowledgement (ACK).
    • The client can then continue using the IP for the extended lease time mentioned in this ACK.
  • T2 (Rebinding Timer)
    • If the server does not respond to the T1 attempt, the client waits until 87.5% of the lease time is completed.
    • At this point, it tries again, but this time by sending the request as a broadcast.
    • This way, if there are any other DHCP servers on the network, they can also respond and renew the IP for the client.
  • Lease Expiry
    • If both renewal attempts fail (at T1 and T2), then once the lease time is completely over, the client will give up the IP.
    • The IP is set back to 0.0.0.0, and the client restarts the DORA process to get a fresh IP from the network.

Example

Suppose the DHCP lease time is 24 hours:

  • At the 12th hour (50%), the client tries to renew using T1.
  • If no reply, at the 21st hour (87.5%), it tries again using T2 (broadcast).
  • If still no reply, at the end of 24 hours, the client loses the IP and has to start DORA again.

Other DHCP Packet Types

So far we have seen the main packets in the DORA process. But DHCP also uses a few more packet types for different situations.

  • DHCP Decline
    • If a client receives an IP from the server but finds that the IP is already in use (for example, by checking with an ARP probe), it will send a Decline message back to the server.
    • This tells the server that the offered IP is not usable, and the server should mark it as bad and pick another IP for allocation.
  • DHCP Release
    • When a client no longer needs an IP (for example, when shutting down or moving to another network), it can send a Release message to the server.
    • This informs the server to put that IP back into the available pool so it can be assigned to another client.
  • DHCP Inform
    • Sometimes a client may already have an IP address (manually configured or otherwise) but still needs other network parameters like DNS or gateway.
    • In this case, the client can send a DHCP Inform message, and the server will reply with the required details without assigning a new IP.
  • DHCP Nak (Negative Acknowledgement)
    • If a client tries to use an IP that is not valid in the server’s network or if the IP has expired, the server can respond with a Nak.
    • This tells the client to stop using that IP and start the DORA process again to get a new valid IP.

DHCP Relay Agent

In small networks, the DHCP server is usually in the same LAN as the clients, so the Discover message (which is a broadcast) can directly reach the server. But in large enterprise networks, you cannot keep a DHCP server in every LAN or VLAN.

The issue here is that broadcast packets do not cross routers. So if the DHCP server is in a different network, the client’s DHCP Discover will never reach it.

To solve this, we use a DHCP Relay Agent.

  • The relay agent is usually configured on the router or Layer-3 switch that connects the client’s network to the DHCP server’s network.
  • When the client sends a Discover message as a broadcast, the relay agent will capture it.
  • The relay agent then forwards this request as a unicast packet to the DHCP server. It will also add the information about which network the request came from (using the giaddr field in the DHCP packet).
  • The DHCP server, after preparing an Offer, will send the reply back to the relay agent.
  • Finally, the relay agent forwards this reply back to the client in the original LAN.

This way, even if the server is in a different network, clients can still get IP addresses automatically.

Example:
Suppose you have 10 different VLANs in an office but only one DHCP server in the data center. Instead of keeping 10 servers, you configure each router interface as a DHCP relay agent. Now all client requests from the VLANs will be forwarded to the central DHCP server, and the server will assign the correct IPs based on the VLAN they came from.



Configuring DHCP on a Cisco Router

Network topology showing a router configured as DHCP server at 1.1.1.1/24 connected to a switch and two hosts.
Example topology for DHCP configuration with a router as DHCP server and two client hosts

In the above topology, we have a router with the IP address 1.1.1.1/24. The network is 1.1.1.0 255.255.255.0 and we want to configure this router as a DHCP server so that it can assign IPs to the clients automatically.

  • Gateway (default-router): 1.1.1.1
  • DNS server: 4.4.4.4
  • Lease time: 10 minutes (0 days 0 hours 10 minutes)
  • Excluded IPs: 1.1.1.1 to 1.1.1.10 (these will not be assigned to clients)

Step 1: Exclude the IP range

First, we exclude the IPs that should not be given to clients. These are usually reserved for routers, switches, servers, or static devices.

Router(config)# ip dhcp excluded-address 1.1.1.1 1.1.1.10

Step 2: Create the DHCP pool

Now we create a pool and give it a name (for example, OfficeNetwork)

Router(config)# ip dhcp pool OfficeNetwork

Step 3: Define the network range

Here we tell the router which network this pool belongs to.

Router(dhcp-config)# network 1.1.1.0 255.255.255.0

Step 4: Configure the default gateway

This is the IP that clients will use as their gateway (in this case, the router itself).

Router(dhcp-config)# default-router 1.1.1.1

Step 5: Configure the DNS server

We can give the clients a DNS server. Here we set it to 4.4.4.4.

Router(dhcp-config)# dns-server 4.4.4.4

Step 6: Configure the lease time

Finally, we set the lease time to 10 minutes. The format is days hours minutes.

Router(dhcp-config)# lease 0 0 10

Now the DHCP configuration is complete. When any client connects to this network, it will get an IP from the range 1.1.1.11 – 1.1.1.254 (because .1 to .10 are excluded). Along with the IP, it will also receive the mask, gateway, DNS server, and lease time.


We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.