Install and Configure DHCP client on Rocky Linux

In this tutorial, you will learn how to how to install and configure DHCP client on Rocky Linux. Assuming you want to assign the IP addresses to your Rocky Linux, you would simply install and configure DHCP client to automatically help obtain and assign the IP addresses from the DHCP server.

How to Install and Configure DHCP Client on Rocky Linux

DHCP operates in client-server architecture. Thus, for you to be able to install and 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 install and configure DHCP client on Rocky Linux?

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 Rocky Linux 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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:d0:bd:81 brd ff:ff:ff:ff:ff:ff
    inet 192.168.171.151/24 brd 192.168.171.255 scope global dynamic enp0s8
       valid_lft 31728sec preferred_lft 31728sec
    inet6 fe80::a00:27ff:fed0:bd81/64 scope link 
       valid_lft forever preferred_lft forever

As you can see, the client has been assigned an IP address, 192.168.171.151/24, which was in the range defined in the DHCP server.

Find out the information about DHCP IP address leased including getting the IP address of the DHCP server;

cat /var/lib/dhclient/dhclient.leases
lease {
  interface "enp0s8";
  fixed-address 192.168.171.151;
  option subnet-mask 255.255.255.0;
  option routers 192.168.171.1;
  option dhcp-lease-time 31710;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.171.1,8.8.8.8;
  option dhcp-server-identifier 192.168.171.128;
  option domain-name "itnixpro.com";
  renew 4 2022/04/28 12:45:48;
  rebind 4 2022/04/28 16:35:29;
  expire 4 2022/04/28 17:41:33;
}

You can also see that the route is added as defined on the DHCP server;

ip r
default via 192.168.171.1 dev enp0s8 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
192.168.171.0/24 dev enp0s8 proto kernel scope link src 192.168.171.151 

You can also get the DNS address details using resolvectl if at all

resolvectl status
Global
       LLMNR setting: yes
MulticastDNS setting: yes
  DNSOverTLS setting: no
      DNSSEC setting: allow-downgrade
    DNSSEC supported: yes
  Current DNS Server: 192.168.171.1
         DNS Servers: 192.168.171.1
                      8.8.8.8
          DNS Domain: itnixpro.com
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test
...
Link 3 (enp0s8)
      Current Scopes: LLMNR/IPv4 LLMNR/IPv6
       LLMNR setting: yes
MulticastDNS setting: no
  DNSOverTLS setting: no
      DNSSEC setting: allow-downgrade
    DNSSEC supported: yes
...

You can also configure the interface to be able to automatically get the IP address on system boot.

nmcli con add type ethernet ifname enp0s8 con-name enp0s8 connection.autoconnect yes
nmcli con mod enp0s8 ipv4.method auto

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/messages
...
Apr 28 06:38:11 localhost dhcpd[3807]: DHCPDISCOVER from 08:00:27:d0:bd:81 via ens160
Apr 28 06:38:12 localhost dhcpd[3807]: DHCPOFFER on 192.168.171.151 to 08:00:27:d0:bd:81 via ens160
Apr 28 06:38:12 localhost dhcpd[3807]: DHCPREQUEST for 192.168.171.151 (192.168.171.128) from 08:00:27:d0:bd:81 via ens160
Apr 28 06:38:12 localhost dhcpd[3807]: DHCPACK on 192.168.171.151 to 08:00:27:d0:bd:81 via ens160
...

And that is how you can install and configure DHCP client on Rocky Linux.

Other Tutorials

How to Configure DHCP Client on Ubuntu 22.04

Install NFS Server on Rocky Linux

Setup NTP Server on Rocky Linux

Founder of itnixpro.com|Linux Engineer|Author at Itnixpro.com

Leave a Comment