Internet Educational Series #5: DHCP (Dynamic Host Configuration Protocol)

How can the Internet have millions of devices connected between them? Does each device have always the same IP?

Internet Educational Series #5: DHCP (Dynamic Host Configuration Protocol)
DHCP

In the last article, we talked about DNS, how does the internet know where each host is allocated, and translates each host name to its IP address.

However, having a static IP address for each host is not at all scalable.

That’s why nowadays we use DHCP. This protocol automates the process of configuring the network parameters of network devices.

This means that each time a device opens, and connects itself to the Internet, it uses the DHCP protocol to configure its parameters, and to be able to be properly connected to the rest of the world (public) devices.

These parameters include the assigned IP, the network mask, the IP address of one or more DNS servers and the IP address of the default router.

DHCP example
DHCP example

DHCP consists of two components:

  • A mechanism for allocation of network addresses (IPs) to devices.
  • A protocol for delivering network addresses and other host-specific configuration parameters from the DHCP server to the device being configured.

DHCP Allocation Mechanisms

DHCP supports 3 mechanisms for IP address allocation:

  1. Manual Allocation: A particular IP address is pre-allocated to a network device by an administrator. The network device is typically identified by its MAC address (but other methods like secret keys can be used).
  2. Automatic Allocation: DHCP automatically assigns an IP address permanently to a device, selecting it from a pool of available addresses.
  3. Dynamic Allocation: DHCP automatically assigns an IP address to a client for a limited period of time (or until the client explicitly relinquishes the address).

The 3d mechanism (dynamic) is by far the most widely used, as it is the most scalable of them all.

It allows automatic reuse of addresses, as the huge majority of internet hosts will just connect temporarily, or they won’t need a static IP address.

The period of time over which a network address is allocated to a client is called the lease.

The dynamic allocation mechanism works as follows:

  • A client requests the use of an address for X period of time (lease time).
  • The allocation mechanism guarantees that the assigned address will not be reallocated to any other device during the requested time.
  • Later, the client may extend its lease with subsequent requests.
  • Each time the client requests an address, it usually includes the previous assigned address and the allocation mechanism attempts to assign the same network address.
  • If the client does not need the address anymore, it can issue a message to release the address back to the server.
  • Finally, the client may ask for a permanent assignment by asking for an infinite lease.

Obviously the DHCP server needs a database or persistent storage of network parameters for network clients, as it has to know the state of the configured hosts to operate properly.

DHCP servers usually store a key-value entry for each client, which is a unique identifier, and contains the configuration parameters for the client.

DHCP Protocol

DHCP messages use ports 67 and 68.

The messages that are sent from client → server use the server port 67.

The messages that are sent from server → client use the client port 68.

Process to allocate network addresses:

DHCP allocation process
DHCP allocation process
  1. The client broadcasts a DHCPDISCOVER message from the port 68, to the port 67 of the server.
  2. Then, each DHCP server available in the network may respond from its port 67 to the port 68 of the client with a DHCPOFFER message, which includes an available network address and other configuration parameters.
  3. Then the client selects a server, and broadcasts a DHCPREQUEST message to request a specific IP address. It does also do this from its own port 68, to the port 67 (still broadcasting).
  4. The server responds with a DHCPACK message with the requested parameters, from its port 67 to the port 68 of the client, who will now have an assigned IP with a lease time.
DHCP message exchange
DHCP message exchange

There are other DHCP messages that servers and clients can use, such as DHCPNAK (server to client indicating network address is incorrect), DHCPDECLINE (client to server indicating network adress is already in use), DHCPRELEASE (client to server relinquishing network address and cancelling remaining lease) and DHCPINFORM (client to server asking only for local configuration parameters).

Using DHCP in Linux

DCHP server (dhcpd)

You can start, stop, get the status or restart the DHCP server with:

server# /etc/init.d/dhcp3-server start/stop/status/restart

DHCP Client (dhclient)

You can start/stop the DHCP client for Linux with the command:

client# dhclient3

The previous command sends a DHCP discover through all the interfaces of the host.

However, it is typical to start the client only in some interfaces of your network, for example:

client# dhclient3 eth1

And now we would only be looking for dhcp servers through eth1 interface.

If you want to release a certain interface (eth1 in this case) lease:

client# dhclient3 -r eth1

When the DHCP client is started, it reads a configuration file to know what to do, which is typically located in /etc/dhcp3/dhclient.conf.

DHCP Commands Summary

Server

  • Script to start/stop the DHCP server: /etc/init.d/dhcp3-server
  • Configuration file for ISC DHCP server: /etc/dhcp3/dhcpd.conf
  • Current leases of the DHCP server: /var/lib/dhcp3/dhcpd.leases

Client

  • Start, stop, restore the DHCP client: dhclient3
  • Configuration file for the DHCP client: /etc/dhcp3/dhclient.conf
  • Current leases of the DHCP client: /var/lib/dhcp3/dhclient.leases

Other important commands:

/var/log/syslog

This is the file where is stored the DHCP event log.


This is reaching to an end, DHCP itself is not the most complex protocol used in the Internet, but it is by far one of the most used of them all.

I hope this article helped in clarifying some ideas, and if you still have any doubts, hit me up at [email protected], I’ll be happy to share thoughts with you!


If you want to check my previous articles to understand some other Internet concepts feel free to do so!


Leave a clap if you enjoyed this post, you’ll support my work and help me keep myself motivated to write more! Knowledge is power!