How the DHCP protocol works, from theory to practice.
Tags: linux, dhcp, network
Published: 2024-03-11 04:19:13

## 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](https://www.packettracernetwork.com/download/download-packet-tracer.html) to configure a DHCP server and gain a deep understanding of how this protocol operates.

### Creating our network.

![Creating a network](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m1h5vabngsp1rtp73sc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktlla2x3bkasiq8eq4w7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkdm2dzlnpayl04zomtx.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ee8kkjlpb6f2wn9fp7th.png)

**Attention is required for these two fields:**

_**Start IP Address** and **Maximum Number of Users**_.

![Attention fields](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bezozs5hacq1qd8524u7.png)

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
```

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.
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mx7fsnlv8z7iytouu66.png)

> _"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
```


### My network interfaces.

![isc-dhcp-server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acip1edrj93vejbsvt19.png)

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

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


![ifconfig](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sb6conujwhq2u0y0oar2.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0qjomhfn7tsjpyhvihh.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcjrhva5ifq9o63olbxr.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyw0ldyzoktx843ink8d.png)

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
}
```
According to the figure below,


![Fixed ipaddress](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xejhqwnjv20h3tmcytn.png)

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!