What is DHCP?
DHCP, or Dynamic Host Configuration Protocol, is used to automatically assign random IP addresses to devices. When using a server or router with a DHCP server, clients do not need to manually set their IP addresses. They need to request it from DHCP. The IP addresses provided by DHCP are referred to as dynamic IP addresses, and manual IP addresses are referred to as static IP addresses.
Renew IP address in Debian 11 using DHCP (all network cards):
The dhclient tool is the main Linux method for working with DHCP or dynamic IP addresses. When our system boots up, dhclient tries to set up a dynamic IP address for every network device on our system. However, dhclient can be called by the user at any time to update the IP address of the interface.
In the first example of this article, we will execute dhclient with the -r flag to release the current IP address. It is important to note that the dhclient command will release all IP addresses if no network device is specified.
Note. After running this command, you will lose your network connection. You will restore it by following the steps in this article:
sudo dhclient -r
Now your system has freed up its IP address and your internet connection is gone.
You can also implement the -v (verbosity) option to get the entire IP release process detailed in the output, as shown in the example below:
sudo dhclient -v -r
Once your IP address is freed, you can request a new one using the dhclient command without additional parameters, as shown in the screenshot below:
And you can also implement the -v flag to get verbose output as shown in the following example:
sudo dhclient -v
As you can see, the IP address 192.168.1.103 has been assigned to the wlp3s0 device.
Update IP address in Debian 11 with DHCP (specific network card):
As mentioned earlier, if you do not specify a device when you run the dhclient command, all the IP addresses of the NICs will be affected.
It is quite easy to specify the network card when starting dhclient. Just add the name of your network card at the end of the command.
The following example shows how to release the IP address of the wlp3s0 Wi-Fi card. As mentioned in the first steps of this tutorial, the -v option was added for detailed results only. You can skip this.
sudo dhclient -v -r wlp3s0
Then, to get a new dynamic IP address specifically for the wlp3s0 wifi card, run the following command:
sudo dhclient -v wlp3s0
As you can see, the IP address has been correctly assigned to the wlp3s0 network device.
How does DHCP work?
When you run the dhclient command, or when you boot the computer, dhclient sends out broadcasts (DHCP Discover) on the subnet to discover available DHCP servers. The DHCP Discover package includes the MAC address of the physical computer for the client to be identified by the server. The DHCP server then sends a response (DHCP OFFER) with an offer to send an IP address. The client responds with a request packet or DHCP request, accepting the offer. The DHCP server then assigns an IP address to the client, integrating it into the network. This process is known as DHCP negotiation.
For packets, we can summarize this process as DHCPDISCOVER> DHCPOFFER> DHCPREQUEST> DHCPACK, where:
DHCPDISCOVER: The client sends a packet to the subnets looking for available DHCP servers to obtain an IP address.
DHCPOFFER: When a packet is received by a DHCP server, the server sends a DHCPOFFER message back to the client identified by its MAC address and then offers to assign it a free or unused IP address.
DHCPREQUEST: The client sends a packet to the DHCP server, accepting the offer. If more than one DHCP server has offered an IP address, the client will accept the first one it receives, and the other DHCP servers will be notified that the initial request has already been granted.
DHCPACK: Finally, after the DHCP server received the last request packet from the client. It will respond with a DHCPACK completing the IP assignment.
How do I check the DHCP lease history?
In some cases, you might want to view the dynamic IP address history. This can be accomplished by checking the system logs located in / var / log / syslog. The following command allows you to see all reconciliation processes that involve the dhclient command:
sudo grep dhclient /var/log/syslog
As you can see, we can see the entire negotiation process and the previously assigned IP addresses.
As you can see, it is quite easy to update the IP address in Debian 11 using DHCP. Understanding how DHCP servers and clients work is the basic knowledge any Linux user should learn to understand how many devices get their IP addresses. While dynamic IP addresses do not make sense on scheduled networks, they are a great way to manage public IP addresses since no two devices can have the same address. As you can see, a dynamic IP address request from a Linux device can be done at any Linux user level with privileged access. In many cases, when you connect to a public network but cannot access the Internet, you can try to request a dynamic IP address using the method described. This helped me in some situations.
For more information on the dhclient command, you can visit the man page at https://linux.die.net/man/8/dhclient.
Thank you for reading this article, which explains how to renew DHCP IP addresses in Debian 11.