1. Overview
There are several occasions when we need to renew the DHCP-assigned system IP address, for example:
- For resolving connection issues
- For resolving IP address conflict in a place where many computers use the same DHCP server
- For some reason, the administrator sets a short lease time for the assigned DHCP IP
Besides these reasons, when a computer switches to a different network, its IP also changes.
In this tutorial, we’ll discuss renewing the DHCP-assigned IP address on a Ubuntu 22.04 system. Let’s first see an overview of the DHCP protocol.
2. Dynamic Host Configuration Protocol (DHCP)
To identify a computer/host on a network, we use IP addresses. Using these IP addresses, computers communicate with each other on a network. Basically, there are two ways of configuring IP addresses on a client machine:
- Static IP Allocation: Manually setting IP configuration on each individual client
- Dynamic Allocation: Automatically assigning IP addresses through a DHCP server
A DHCP server automatically assigns IP addresses to connected hosts on a network. The client cannot interfere with the DHCP settings they receive. However, these settings are transparent to the client side. An IP address set using the above way is not a permanent one, and we refer to it as a DHCP lease. If we do not renew it, the DHCP lease may be allotted to another computer. Therefore, to renew the lease, the client has to periodically check back with the DHCP server.
3. DHCP Clients
Broadly speaking, there are three DHCP clients that are common on Ubuntu:
However, not all versions of Ubuntu support all three clients. This is true for other Linux-based distributions too. For example, some of them come shipped with only one of the three clients, whereas others may come shipped with two or all of the three. However, dhclient is a commonly available DHCP client on Ubuntu systems.
Ideally, every system has a default DHCP client. On Ubuntu 22.04, the dhclient is the default DHCP client. At the time of writing this article, the other clients pump and dhcpcd are not available on Ubuntu 22.04.
Since we are working on Ubuntu 22.04 system, we’ll have to use dhclient as the DHCP client.
4. Process of DHCP Client-Server Interaction
There are several messages that pass between the DHCP server and the client, as described below:
- DHCPDISCOVER: the client broadcasts a DHCPDISCOVER message asking for an IP address
- DHCPOFFER: in response to the DHCPDISCOVER message, a DHCP server may transmit a DHCPOFFER message
- DHCPREQUEST: the client will respond with a DHCPREQUEST message for a DHCPOFFER message from the DHCP server it wants to be configured with. The client broadcasts this message so all DHCP servers on the network can be informed about the selected server
- DHCPACK: When a client finalizes a DHCP server, it receives a DHCPACK message from this DHCP server. This message also has other required configuration details
- DHCPRELEASE: The client can send a DHCPRELEASE message to terminate the lease
After the last DHCPACK message, the client can use the leased IP address and other settings till the end of the lease time.
When half the lease time is over, the client can request IP renewal. For this, the client sends a DHCPREQUEST message to the DHCP server. If the current DHCP server says yes to the request, it responds with a DHCPACK message.
However, if the client does not get a response, the client might go on with the current DHCP settings. It can continue using them until the lease time is over.
Once the lease time is over, it is mandatory for the client to again restart with the DHCPDISCOVER procedure.
5. Renewing DHCP IP Using dhclient
Let’s first check if the dhclient service is running on our Ubuntu 22.04 system:
$ ps fax | grep dhclient
4988 pts/0 S+ 0:00 | \_ grep --color=auto dhclient
The ps command shows the information corresponding to different services running on the system.
Let’s now check our current leased DHCP IP address on our target interface wlo1:
$ ip addr | grep wlo1
wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
inet 192.168.35.150/24 brd 192.168.35.255 scope global dynamic noprefixroute wlo1
As we can see, the current assigned IP is 192.168.35.150. Let’s release our leased DHCP IP using the -r flag of the dhclient command:
$ sudo dhclient -r
The -r flag releases the current DHCP lease and halts the running DHCP client. Note that releasing an IP address can result in loss of connection, so carefully perform this step on remote systems. Next, we will now request a new DHCP IP from the server:
$ sudo dhclient -v
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/wlo1/d0:c5:d3:3d:22:cf
Sending on LPF/wlo1/d0:c5:d3:3d:22:cf
Listening on LPF/eno1/c8:d9:d2:ee:4f:f9
Sending on LPF/eno1/c8:d9:d2:ee:4f:f9
Sending on Socket/fallback
DHCPDISCOVER on wlo1 to 255.255.255.255 port 67 interval 3 (xid=0xaee34c7f)
DHCPDISCOVER on eno1 to 255.255.255.255 port 67 interval 3 (xid=0xebcb010d)
DHCPOFFER of 192.168.35.151 from 192.168.35.47
DHCPREQUEST for 192.168.35.151 on wlo1 to 255.255.255.255 port 67 (xid=0x7f4ce3ae)
DHCPACK of 192.168.35.151 from 192.168.35.47 (xid=0xaee34c7f)
bound to 192.168.35.151 -- renewal in 1452 seconds.
Let’s again check back our IP on the wlo1 interface:
$ ip addr | grep wlo1
wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
inet 192.168.35.151/24 brd 192.168.35.255 scope global dynamic wlo1
As we can see, the IP address has now changed to 192.168.35.151.
The syslog file stores the logs generated by the DHCP server. We can search through these logs for DHCP logs:
$ cat /var/log/syslog | grep dhcp
Oct 30 05:10:01 LHB NetworkManager[840]: <info> [1667086801.8104] dhcp4 (wlo1):
activation: beginning transaction (timeout in 45 seconds)
Oct 30 05:10:01 LHB NetworkManager[840]: <info> [1667086801.8418] dhcp4 (wlo1):
state changed new lease, address=192.168.35.150
Oct 30 05:40:01 LHB NetworkManager[840]: <info> [1667088601.7480] dhcp4 (wlo1):
state changed new lease, address=192.168.35.150
These logs are very helpful while troubleshooting DHCP issues.
6. Conclusion
In this article, we have learned how we can acquire a new IP address on the Ubuntu system. Specifically, we have used the dhclient utility to renew our IP configuration.