Back to all articles
DHCPIP AddressingNetworking ProtocolsLayer 7

DHCP: How You Actually Get an IP Address

Alex MorganJune 5, 2024

Think back to the last time you connected a new device to a WiFi network. You selected the network name, typed in the password, and a second later, the WiFi icon turned solid. You were online.

But for your device to actually communicate on the network, it needed an IP address, a Subnet Mask, a Default Gateway (router address), and a DNS Server address.

Who gave it all that information? You certainly didn't type it in manually.

In the early days of networking, you actually *did* have to type it in manually. Every time you added a computer to a network, a human administrator had to keep a master spreadsheet of all used IP addresses, find an available one, walk over to the new computer, and manually configure the network settings. It was tedious, prone to human error, and completely unscalable.

To fix this, engineers created a protocol to automate the process: DHCP (Dynamic Host Configuration Protocol).

DHCP is the friendly, automated receptionist of the networking world. When you walk into the building, it hands you a badge (an IP address) and tells you where the exits are (the Default Gateway).

The DORA Process: A Lightning-Fast Negotiation

When your laptop connects to a new network, it has no IP address. Because it has no IP address, it cannot communicate using standard IP routing. It is effectively blind and deaf.

So how does it ask for an IP address if it doesn't have an IP address to ask from?

It uses the same trick we discussed in the ARP blog: it shouts. It sends a broadcast message to the entire local network using the MAC Address.

The entire DHCP negotiation happens in four distinct steps, famously remembered by the acronym DORA: Discover, Offer, Request, Acknowledge.

Step 1: Discover (The Shout)

Your laptop connects to the WiFi. It immediately formats a tiny packet called a DHCP Discover message. Because it doesn't have an IP address, it sets its source IP to `0.0.0.0`. It sets the destination IP to the broadcast address `255.255.255.255` (meaning "everyone on this network").

The message essentially says: *"Hello! I am a new device with MAC address AA:AA:AA. Is there a DHCP server out there who can give me an IP address?"*

Step 2: Offer (The Reply)

Somewhere on your network (usually inside your home router) sits a DHCP Server. The server hears the Discover broadcast. It looks at its internal pool of available IP addresses. Let's say `192.168.1.50` is free.

The server formats a DHCP Offer message and broadcasts it back (or sends it directly to the MAC address): *"Hello AA:AA:AA. I am a DHCP server. I can offer you the IP address 192.168.1.50."*

Step 3: Request (The Acceptance)

Your laptop receives the Offer. It likes what it sees. But it doesn't just quietly start using the IP address. Why? Because there might be *multiple* DHCP servers on a large corporate network, and the laptop might have received multiple offers simultaneously.

The laptop must explicitly choose one. It broadcasts a DHCP Request message: *"Attention everyone! I am officially accepting the offer of 192.168.1.50 from DHCP Server X. All other servers, please take back your offers."*

Step 4: Acknowledge (The Final Handshake)

The chosen DHCP Server hears the Request. It permanently marks `192.168.1.50` as "in use" in its database so it doesn't accidentally hand it out to anyone else. It then sends a final DHCP Acknowledgment (ACK) back to the laptop: *"Confirmed. The IP address is yours. Here is your Subnet Mask, your Router address, and your DNS servers. Have a nice day."*

The laptop applies the settings, and you are officially on the internet. This entire four-step DORA process usually takes less than half a second.

The Concept of the Lease

There is a catch. When the DHCP server hands you an IP address, it doesn't give it to you forever. It leases it to you.

Every DHCP Offer comes with a "Lease Time." On a typical home network, the lease time is usually 24 hours. On a public coffee shop WiFi, the lease time might be only 1 hour.

Why use leases? Imagine a busy airport WiFi. Thousands of phones connect, get an IP address, check their email, and then fly to another city. If the DHCP server gave away IP addresses permanently, it would run out of its 254 available addresses within an hour, and no new travelers could connect.

By using leases, the DHCP server knows that if a device leaves the airport, its lease will eventually expire, and that IP address can be thrown back into the available pool for the next person.

If you are sitting at home on your laptop for two days straight, you won't lose your connection. At the halfway point of the lease (e.g., 12 hours into a 24-hour lease), your laptop quietly sends a private message to the DHCP server asking to renew the lease. The server says yes, and the 24-hour timer resets.

Static IPs vs Dynamic IPs

Because DHCP is so convenient, almost all consumer devices (phones, laptops, smart TVs) use it. We call these Dynamic IP addresses, because they can technically change every time the lease expires (though most routers try to give you the same IP back if possible).

However, there are times when you *don't* want DHCP.

If you are running a web server, a network printer, or a security camera, you want that device to stay at the exact same IP address forever. If the printer's IP address suddenly changed on Tuesday, none of the laptops in the office would know how to print to it anymore.

For these devices, network administrators bypass DHCP and configure a Static IP address. They type the IP address manually into the printer's settings. They also tell the DHCP server, "Hey, exclude this IP from your pool, because I gave it to the printer."

Alternatively, a cleaner method is a DHCP Reservation. The admin tells the DHCP server: "Whenever you see the specific MAC address of the printer, always offer it 192.168.1.100. Never give that IP to anyone else." This combines the reliability of a Static IP with the centralized management of DHCP.

Conclusion

DHCP is one of those invisible protocols that makes the modern, mobile internet possible. Without it, moving from your house, to a coffee shop, to your office would require a degree in network administration just to get online. Thanks to DORA, it's just a click of a button.