DEV Community

Mufida Zuhra
Mufida Zuhra

Posted on

Understanding How DHCP Works

There are two ways a computer can be assigned an IP address:

  1. Using static IP addresses
  2. Using dynamic IP addresses

Dynamic IP, as the name suggests, is the IP address that can change.

When you go to a cafe, you are using their wifi, right?. You are using an IP address assigned by the cafe's ISP.

When you leave and went home, you are no longer assigned to that IP address but now assigned to your house's.

That IP address you previously used in the cafe can now be used for other devices in that cafe.

The protocol that assigns dynamic IP to devices is called DHCP.

DHCP stands for Dynamic Hosting Configuration Protocol.

It is a protocol that governs how devices set an IP address to connect to the internet.

For a static IP address, you'll have to manually set the IP address yourself.

This normally involves an IT man coming to your house, where they also have to configure the subnet mask, the default gateway, and the DNS server.

If you have more than one computer, you'll have to set the configuration for other computers/devices manually as well.

On the other hand, every device that follows DHCP will immediately connect to the internet and request for their own IP addresses.

This whole process happens automatically.

DHCP will set your IP address, the subnet mask, the default gateway, and the DNS server, all automatically. And these are important information in order to connect to the internet.

You can check the settings in your computer ipconfig or phone to see whether your IP is set to automatic or not.

If it is, then your device is employing a dynamic hosting protocol.

But how does it work?

When you first turn on your device, your device will broadcast a request to the DHCP servers, asking to be assigned an IP address. this broadcast message is called

DHCPDISCOVER
Enter fullscreen mode Exit fullscreen mode

Wherever your device is located, it will broadcast this message, looking for a DHCP server.

The broadcast is sent via UDP connection, not TCP/IP.

Why?

Notice the word 'broadcast'.

This device has no idea where the DHCP server is.

In a TCP/IP protocol, your device needs to connect to its destination first before communicating over the internet.

In this case, your device has no idea who the destination is. It just broadcast the message, hoping there's a server somewhere to pick it up.

Say there's indeed a DHCP server close by.

This server hears the dhcpdiscover message, establishes a connection, and sends a response with a corresponding IP address, a subnet mask, DNS server, and the default gateway.

It figures out which IP address to give to you based on the available ranges they have.

One more information they send back to you is something called the

lease

What is this?

Since the IP is dynamic, that means there is a certain duration for how long you can use that particular IP address, right?.

This is why dynamic IP addresses are

leased

They are leased to your device so that they can be reusable.

If you type this in the terminal:

ipconfig /all
Enter fullscreen mode Exit fullscreen mode

You'll see the obtained date and the expiration date of the lease.

Just to show you how dynamic it is, If you want to terminate your IP release right now, you can type this in your terminal:

ipconfig /release
Enter fullscreen mode Exit fullscreen mode

But note that you'll lose internet connection :p

To renew your lease again, you can type this in the terminal:

ipconfig /renew
Enter fullscreen mode Exit fullscreen mode

and your internet connection will be back.

And this pretty much sums up the basics of DHCP!.

Top comments (0)