DEV Community

Cover image for How the DHCP protocol works, from theory to practice.
Alisson
Alisson

Posted on

How the DHCP protocol works, from theory to practice.

Understanding the DHCP protocol

Good morning, good afternoon, and good evening, everyone. First of all, I would like to apologize for my absence over the last two months. I had several important work commitments, but I'm back now with content of utmost importance for deepening our understanding of computer networks. Today, we'll be discussing the DHCP protocol, as we always do: theory first, followed by PRACTICE!!.

Contrary to what some people may think, the DHCP protocol is not only responsible for intelligently distributing IP addresses; it is also responsible for various configurations such as network mask, sub-net mask, and so on.

Next, we will proceed to a laboratory session using Cisco Packet Tracer to configure a DHCP server and gain a deep understanding of how this protocol operates.

Creating our network.

Creating a network

Initially, we will add four standard computers, a network switch, and a server that will serve as the host for our DHCP server.

Hint:To recap, the switch is the device responsible for acting as a bridge between data flows, where it receives data in a centralized manner and forwards it to the next destination. Within the OSI model, the switch operates at the Data Link layer (the layer responsible for facilitating local communication, organizing data into small encapsulated groups, which are then directed to the next layer).

We will now check the IP settings of our machine 'PC-PT-PC0'. To do this, simply:

  1. Double click on the "PC-PT-PC0"
  2. Select the option "Ip Configuration"
  3. Select the option "Desktop"

Default DHCP configuration

When selecting the "DHCP" option, our machine will search the network for a DHCP server that will provide its network configurations. Since our server does not yet exist, the machine will obtain a private IP address, indicated by the beginning of the address "168...", and the message "DHCP failed, APIPA is being used" will be displayed.

APIPA stands for 'Automatic Private IP Addressing' and indicates that an IP address within the CIDR range 169.254.0.0/16 has been assigned to that machine. This addressing allows the host in question to communicate only locally with other machines, as per internet regulatory standards, as a private IP address cannot be routed to the global network.

Hint:When your internet is failing and you want to check if there is an error in your router, in general you can check your IP address, if your address is private, your router probably has a problem and your operator needs to be contacted.

Configuring our server.

To begin our DHCP configuration, we can access our 'server' element with a double-click and arbitrarily assign a valid IP address to it. For instance, I'll use the 192.168.0.0/24 range, but you can use any range you prefer.

Following the same path desktop -> Ip configuration, we will see the screen below.

We will add our IP address (192.168.0.1), and our sub-net mask will be automatically included.

DHCP server configuration

Next, we will go to the Services tab, where we will name our server and configure some additional parameters.

Our default gateway and DNS server will be the server itself, meaning they should have the same IP address as the server.

Services

Attention is required for these two fields:

Start IP Address and Maximum Number of Users.

Attention fields

First, let's review the calculation of the number of possible IP addresses in our selected CIDR.

Our network has the sub-net mask '/24'. This calculation is done using the following equation:

N = 2^(32-CIDR) - 2.

Since our network has a /24 range, we'll have:

N = 2^(32-24) - 2
N = 2^8 - 2
N = 256 - 2
N = 254
Enter fullscreen mode Exit fullscreen mode

Therefore, the possible IP addresses in this network range are.

192.198.0.1
192.198.0.2
192.198.0.3
...
192.198.0.254.
Enter fullscreen mode Exit fullscreen mode

With this, we will know that this server with these settings can configure up to 253 machines.

"But why 253 if there are 254 available addresses?"

We cannot forget that the server itself consumes one of the available addresses, leaving 253 other addresses for other hosts.

Therefore, our initial address will be '192.168.0.2' and our maximum number of users is: 253.

Then simply activate the server by selecting the 'On' option.

Point of attention:

In this network range, there are two other reserved IP addresses, they are:

192.168.0.0 which represents the IP address of the network itself

192.168.0.255 which is the reserved address in case the network needs to communicate with another network, in other words, the broadcast IP address.

With these settings done, we can access our 'PC-PT-PC0' machine again and verify our IP address.

Checking PCO configurations.

With this, just access the other machines and activate the 'DHCP' option in the same way it was activated on the first machine, and the other hosts will automatically receive their IP address, sub-net mask, sub network mask, and other configuration parameters.

New host configurations

"But shahahaco, how could I set up a real DHCP server?"

Creating our own DHCP server.

I will assume that you have a virtualizer on your machine, such as Proxmox, VirtualBox, Vmware, Nutanix, or any other.

We will perform a real configuration on an Ubuntu Server virtualized by VirtualBox.

To start, we will install the package responsible for the protocol.

sudo apt upgrade
sudo apt install isc-dhcp-server
Enter fullscreen mode Exit fullscreen mode

My network interfaces.

isc-dhcp-server

After the installation, we will define which network interface to use.

We will check our interfaces using the command "ifconfig".

ifconfig

Hint: If this package is not present on your machine, execute the command sudo apt-get install net-tools.

As you can see below, my Ubuntu Server has two network interfaces:

  • lo: Loopback, it is a virtual network interface for internal server operations. It enables processes to communicate with each other within the virtual environment.

  • enp0s3: It is a network interface associated with VirtualBox, meaning it is through this interface that the virtual machine can access the internet provided by the host machine. This strange name comes from a convention that created a nomenclature for network interfaces called Predictable Network Interface Names, where:

    en: Represents an Ethernet interface.
    p0: Represents the position of the server in the network bus topology or PCI (Peripheral Component Interconnect).
    s3: Irrelevant in this context.

Starting to configure our server.

As you may have already noticed, we will use the interface enp0s3 for our DHCP server.

To do this, we will navigate to our configuration file.

vim /etc/default/isc-dhcp-server

DHCP server file

Hint: If you don't have Vim installed, you can use any other text editor such as Vi, Nano, Xed...

With this, we will delete the variables 'INTERFACESv4' and 'INTERFACESv6' as we do not want to manipulate any specific IPv4 or IPv6 protocol, and we will only add our previously listed interface.

Hint: To install Vim, simply execute sudo apt-get install vim. After opening the file, press the 'i' key to enter interactive mode, and after making the necessary changes, press the 'esc' key, followed by :wq! to close the document while saving the changes. Afterward, you can execute cat /etc/default/isc-dhcp-server to verify the changes in the document.

Reading the changed file

For better understanding, we will create our DHCP configuration file from scratch. To do this, we will navigate to the directory '/etc/dhcp' and remove the default configuration file and create a new one using the command rm dhcpd.conf && vim dhcpd.conf.

New DHCP configuration file

Notice that the configuration parameters are the same as those used in Packet Trace, for example:

Network IP: 192.168.0.0
Server IP: 192.168.0.1
Network Range: 192.168.0.1 - 192.168.0.245
DNS: 192.168.0.1 + Google DNS

We didn't cover the other parameters in Packet Trace, but we have the indicated log level, the hierarchical level on our server, where 'authoritative' designates it as the ultimate authority, and the allowed request times.

It is a fundamental characteristic of the protocol in question to rotate IP addresses among connected hosts. However, in some cases, it is possible to assign a fixed IP address to a host that will not be rotated. To configure it this way, simply follow the syntax.

host hostname {
    hardware mac address
    fixed-address desired ip
}
Enter fullscreen mode Exit fullscreen mode

According to the figure below,

Fixed ipaddress

With this, our DHCP server has been configured, and you simply need to restart our service with the command:

systemctl restart isc-dhcp-server

to apply our changes.

Configuration requests from machines connected to the network can be checked in the 'syslog' file accessed at /var/log/syslog.

Conclusion.

We have practically and thoroughly discussed the configuration of a DHCP server in a virtualized environment, using Ubuntu Server and VirtualBox as examples. We have explored the fundamental concepts of the DHCP protocol, from identifying network interfaces to configuring essential parameters such as IP addresses, network ranges, and DNS servers. Additionally, we have emphasized the importance of DHCP in efficiently managing IP addresses in local networks, simplifying the process of automatically assigning network configurations to connected devices. Hope this guide has been helpful in understanding and implementing DHCP in your own network environments. Keep exploring and experimenting to enhance your network administration skills!

Top comments (0)