Fundamentals 13 min read

How Does Your Computer Automatically Get an IP? Inside DHCP’s Four‑Step Dance

This article explains why a newly plugged‑in computer can instantly obtain an IP address, detailing DHCP’s four‑stage process, the role of UDP broadcasting, packet‑capture commands, unicast optimizations, lease reuse, possible IP conflicts, and the gratuitous ARP messages that follow an ACK.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How Does Your Computer Automatically Get an IP? Inside DHCP’s Four‑Step Dance

What is DHCP?

DHCP (Dynamic Host Configuration Protocol) is the mechanism that automatically assigns an IP address, subnet mask, router, and DNS information to a device when it connects to a network.

DHCP Working Process

The DHCP workflow consists of four distinct phases:

DHCP Discover: The client, lacking an IP address, broadcasts a request asking “Who can give me an IP?” to the local network.

DHCP Offer: Any DHCP server that receives the broadcast selects a free IP from its pool and replies (normally via broadcast) with the offer.

DHCP Request: The client selects one offer and broadcasts a request stating “I accept this IP.”

DHCP ACK: The server acknowledges the request, confirming the lease (often for 24 hours). The client can now use the IP as long as the lease remains valid.

Why Are the Third and Fourth Phases Needed?

If multiple DHCP servers exist on the same segment, each may send an Offer. The client follows a “first‑come‑first‑served” rule, responding to the first Offer with a Request to confirm that the IP is still available before using it.

Capturing DHCP Packets

To force a client to repeat the DHCP process, run the following command (replace en0 with the appropriate interface, e.g., eth0).

sudo ipconfig set en0 DHCP
en0 can be replaced with other network‑interface names such as eth0.

Packet captures show four packets corresponding to the four DHCP stages. The Offer packet contains the IP, subnet mask, router, and DNS server addresses. DHCP operates at the application layer but transports its messages over UDP.

Why DHCP Uses UDP Instead of TCP

UDP is connection‑less, allowing the client to broadcast to the special address 255.255.255.255 without first establishing a connection. TCP would require a prior connection to a specific host, which is impossible when the client does not yet know the server’s address.

Unicast vs. Broadcast in the Offer Phase

Although the protocol specifies a broadcast Offer, many implementations optimise by unicasting the Offer when the client sets the “Broadcast flag = 0”. This reduces unnecessary processing on non‑target machines.

Do You Always Go Through All Four DHCP Stages?

When a device reconnects, it often reuses a previously leased IP. In that case only the Request and ACK stages occur, because the client already knows which server to contact.

Can DHCP‑Assigned IPs Conflict?

Conflicts can arise if a manually configured IP duplicates a DHCP‑assigned one, or if two DHCP servers manage overlapping address pools. Resolving such conflicts requires adjusting the manual configuration or re‑segmenting the DHCP pools.

What Happens After DHCP ACK?

After receiving the ACK, the client sends three gratuitous ARP (ARP announcement) messages that broadcast the new IP‑to‑MAC mapping and check for address conflicts. Only after these ARP messages confirm no conflict does the client start normal communication.

Summary

Plugging in a cable triggers DHCP, which automatically provides IP, subnet mask, router, and DNS information.

DHCP consists of Discover, Offer, Request, and ACK; a previously used lease can skip the first two stages.

DHCP is an application‑layer protocol that uses UDP to enable broadcasting.

DHCP‑assigned IPs can clash with manually set addresses or overlapping DHCP pools.

After the ACK, the client broadcasts three gratuitous ARP messages to announce the IP and verify uniqueness before using it.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

NetworkingPacket CaptureUDPIP addressARPDHCP
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.