How does DHCP work?

How does DHCP work?

If you are running windows, you can get your connection information by running the following command — ipconfig/all.

You can see the IP address, physical address, subnet mask, and information like leas obtained and expiry which means that this connection can be used from so and so data up to some date.

You can also see DNS server, DHCP server address, and Default Gateway.

How does our network adapter get this information? The magic is DHCP.

DHCP stands for Dynamic Host Configuration Protocol. DHCP allows a server to automatically set the TCP/IP network configuration for a client computer. Without DHCP, a manual setup of these configurations would have to be done by an IT guy, which is referred to as static addressing.

Let’s see how DHCP works step-by-step:

  • Step 1 — Whenever a computer boots up, it broadcasts a DHCPDISCOVER packet looking for a DHCP server. The message can be assumed as “Hi is there any DHCP server out there?”

  • Step 2 — Suppose there is a DHCP server close by and it intercepts the DHCPDISCOVER message, then the DHCP server responds with a DHCPOFFER message saying “Hi I can lease you this IP address and this subnet mask so that you can connect to the local network, and here is the IP address of the Default Gateway and DNS servers. You can connect to the internet using these addresses for 2 days”. The lease can be renewed after 2 days.

  • Step 3 — The DHCP client responds with a DHCPREQUEST saying “I accept all the TCP/IP information you provide.

  • Step 4 — The DHCP server replies with a DHCPACK packet stating “OK go ahead and use the network”. The DHCP server registers the MAC address of the client in its DHCP database.

In summary, DHCP is a dynamic way of giving a client its network credentials. All the communications between the DHCP server and the DHCP client are broadcast, UDP not TCP. The client uses port 68 and the server uses port 67.