In this tutorial, you will learn how to how to configure DHCP client on Ubuntu 22.04. Assuming you want to assign the IP addresses to your Ubuntu 22.04, you would simply configure DHCP client to automatically help obtain and assign the IP addresses from the DHCP server.
How to Configure DHCP Client on Ubuntu 22.04
DHCP operates in client-server architecture. Thus, for you to be able to configure DHCP client and obtain the IP address automatically, you need to be having a running DHCP server.
We have covered how to install and configure DHCP server on various guides.
So, how can you configure DHCP client on Ubuntu 22.04?
You realize that by default, Linux systems ship with The Internet Systems Consortium DHCP Client
, dhclient
, by default.
So how does dhclient operates? The operation of dhclient has been extensively explained on the dhclient manual page;
man dhclient
The DHCP protocol allows a host to contact a central server which maintains a list of IP addresses which may be assigned on one or more subnets. A DHCP client may request an address from this pool, and then use it on a temporary basis for communication on network. The DHCP protocol also provides a mechanism whereby a client can learn important details about the network to which it is attached, such as the location of a default router, the location of a name server, and so on.
There are two versions of the DHCP protocol DHCPv4 and DHCPv6. At startup the client may be started for one or the other via the -4 or -6 options.
On startup, dhclient reads the dhclient.conf for configuration instructions. It then gets a list of all the network interfaces that are configured in the current system. For each interface, it attempts to configure the interface using the DHCP protocol.
In order to keep track of leases across system reboots and server restarts, dhclient keeps a list of leases it has been assigned in the dhclient.leases file.
On startup, after reading the dhclient.conf file, dhclient reads the dhclient.leases file to refresh its memory about what leases it has been assigned.When a new lease is acquired, it is appended to the end of the dhclient.leases file. In order to prevent the file from becoming arbitrarily large, from time to time dhclient creates a new dhclient.leases file from its in-core lease database. The old version of the dhclient.leases file is retained under the name dhclient.leases~ until the next time dhclient rewrites the database.
Old leases are kept around in case the DHCP server is unavailable when dhclient is first invoked (generally during the initial system boot process). In that event, old leases from the dhclient.leases file which have not yet expired are tested, and if they are determined to be valid, they are used until either they expire or the DHCP server becomes available.
A mobile host which may sometimes need to access a network on which no DHCP server exists may be preloaded with a lease for a fixed address on that network. When all attempts to contact a DHCP server have failed, dhclient will try to validate the static lease, and if it succeeds, will use that lease until it is restarted.
A mobile host may also travel to some networks on which DHCP is not available but BOOTP is. In that case, it may be advantageous to arrange with the network administrator for an entry on the BOOTP database, so that the host can boot quickly on that network rather than cycling through the list of old leases.
Having read that, it is very easy to configure Ubuntu 22.04 to obtain IP addresses automatically via DHCP server.
You first need to identify the interface which you want to automatically assign the IP address via DHCP;
ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:3a:f3:89 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 86360sec preferred_lft 86360sec 3: enp0s8: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:b6:e8:c6 brd ff:ff:ff:ff:ff:ff
In my client server, I want to automatically assign the IP address to the interface enp0s8.
In order to be able to get the IP addresses from the DHCP server, there has to be direct LAN connection or just any other way of connection between the DHCP client interface and the DHCP server.
So, to obtain IP address automatically from the DHCP server, simply run the dhclient
command against the interface.
dhclient enp0s8
Next, confirm the IP address assignment;
ip a show dev enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:b6:e8:c6 brd ff:ff:ff:ff:ff:ff inet 192.168.0.100/24 brd 192.168.0.255 scope global dynamic enp0s8 valid_lft 3485sec preferred_lft 3485sec
As you can see, the client has been assigned an IP address, 192.168.0.100/24
, which was in the range defined in the DHCP server.
If you are on the desktop version of Ubuntu 22.04, then you can check from the network settings.
Find out the information about DHCP IP address leased including getting the IP address of the DHCP server;
cat /var/lib/dhcp/dhclient.leases
lease { interface "enp0s8"; fixed-address 192.168.0.100; option subnet-mask 255.255.255.0; option routers 192.168.0.1; option dhcp-lease-time 3600; option dhcp-message-type 5; option domain-name-servers 192.168.0.1,8.8.8.8; option dhcp-server-identifier 192.168.0.1; option domain-name "itnixpro.com"; renew 3 2022/04/27 17:08:30; rebind 3 2022/04/27 17:35:24; expire 3 2022/04/27 17:42:54; }
You can also see that the route is added as defined on the DHCP server;
default via 192.168.0.1 dev enp0s8
192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.100
You can also get the DNS address details;
resolvectl status enp0s8
Link 3 (enp0s8) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 192.168.0.1 DNS Servers: 192.168.0.1 8.8.8.8 DNS Domain: itnixpro.com
You can also configure the interface to be able to automatically get the IP address on system boot.
Thus, edit the netplan configuration file as follows;
sudo nano /etc/netplan/01-network-manager-all.yaml
network: version: 2 renderer: networkd ethernets: enp0s8: dhcp4: yes
Save the configure and apply netplan configs;
netplan apply
You can reboot your system and confirm, if at all it is okay to reboot.
On the DHCP server, you will also see the logs;
sudo tail -f /var/log/dhcpd.log
Apr 27 20:03:47 dhcp dhcpd[9620]: DHCPACK on 192.1680.100 to 08:00:27:b6:e8:c6 (itnixpro) via ens33 Apr 27 20:03:50 dhcp dhcpd[9620]: reuse_lease: lease age 4 (secs) under 25% threshold, reply with unaltered, existing lease for 192.1680.100 Apr 27 20:03:50 dhcp dhcpd[9620]: DHCPREQUEST for 192.1680.100 from 08:00:27:b6:e8:c6 (itnixpro) via ens33 Apr 27 20:03:50 dhcp dhcpd[9620]: DHCPACK on 192.1680.100 to 08:00:27:b6:e8:c6 (itnixpro) via ens33 Apr 27 20:04:01 dhcp dhcpd[9620]: reuse_lease: lease age 15 (secs) under 25% threshold, reply with unaltered, existing lease for 192.1680.100 Apr 27 20:04:01 dhcp dhcpd[9620]: DHCPDISCOVER from 08:00:27:b6:e8:c6 (itnixpro) via ens33 Apr 27 20:04:01 dhcp dhcpd[9620]: DHCPOFFER on 192.1680.100 to 08:00:27:b6:e8:c6 (itnixpro) via ens33 Apr 27 20:04:01 dhcp dhcpd[9620]: reuse_lease: lease age 15 (secs) under 25% threshold, reply with unaltered, existing lease for 192.1680.100 Apr 27 20:04:01 dhcp dhcpd[9620]: DHCPREQUEST for 192.1680.100 (192.168.0.1) from 08:00:27:b6:e8:c6 (itnixpro) via ens33 Apr 27 20:04:01 dhcp dhcpd[9620]: DHCPACK on 192.1680.100 to 08:00:27:b6:e8:c6 (itnixpro) via ens33
And that is how you can configure DHCP client on Ubuntu 22.04.