How to Install and Configure DHCP Server on Ubuntu 20.04

If you are a system administrator and working in a large environment then you may often need to set up a new client system and assign IP addresses and other network-related information manually. It is a very time-consuming process for you. This is the case, where DHCP comes into the picture.

DHCP also known as a "Dynamic Host Configuration Protocol" is a service that can be used to dynamically assigns unique IP addresses and other information to client systems. You can assign the IP address, domain name, hostname, default gateway, and DNS server using the DHCP service.

In this post, we will show you how to install the DHCP Server and Client on Ubuntu 20.04 server.

Prerequisites

  • Two systems running Ubuntu 20.04.
  • A root password is configured on the server.

Install DHCP Server

By default, the DHCP server package is included in the Ubuntu default repository. You can install it with the following command:

apt-get install isc-dhcp-server -y

Once the installation is completed, start the DHCP service and enable it to start at system reboot with the following command:

systemctl start isc-dhcp-server
systemctl enable isc-dhcp-server

Configure DHCP Service

DHCP server default configuration file located at /etc/default/isc-dhcp-server. You will need to edit it and define your network interface.

You can edit it with the following command:

nano /etc/default/isc-dhcp-server

Define your network interface as shown below:

INTERFACESv4="eth0"

Save and close the file when you are finished then edit the /etc/dhcp/dhcpd.conf file and define DHCP lease:

nano /etc/dhcp/dhcpd.conf

Uncomment the following line:

authoritative;

Next, change the following lines:

default-lease-time 660;
max-lease-time 6300;

# range of subnet
range 192.168.0.2 192.168.0.20;

# gateway address
option routers 192.168.0.1;

# DNS server address
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Save and close the file when you are finished.

Configure DHCP Server to Assign Static IP to Client

By default, the DHCP server is configured to assign a dynamic IP address to all clients. In this section, we will configure the DHCP server to assign a fixed IP address based on the Client's Mac address.

For example, let's assign the IP address 192.168.0.5 to a client with MAc Address 4c:bb:58:9c:f5:55.

To do so, edit the /etc/dhcp/dhcpd.conf file with the following command:

nano /etc/dhcp/dhcpd.conf

Add the following lines:

host client1 {

hardware ethernet 4c:bb:58:9c:f5:55;

fixed-address 192.168.0.5;

}

Save and close the file when you are finished then restart the DHCP service to apply the changes:

systemctl restart isc-dhcp-server

Now, verify the status of the DHCP service with the following command:

systemctl status isc-dhcp-server

Configure DHCP Client to Obtain Static IP address

Next, you will need to configure your client machine to obtain a static IP address from the DHCP server.

Go to the client machine with Mac address 4c:bb:58:9c:f5:55 then edit the network interface file with the following command:

nano /etc/network/interfaces

Remove default lines and add the following lines:

auto eth0
iface ens33 inet eth0

Save and close the file then restart the Network Manager service to apply the changes:

systemctl restart network-manager

Now, run the following command to verify the IP address of the client machine:

ifconfig

You should get the following output:

eth0      Link encap:Ethernet  HWaddr 4c:bb:58:9c:f5:55  
          inet addr:192.168.0.5  Bcast:192.168.0.255  Mask:255.255.0.0
          inet6 addr: 2401:4900:1955:16a2:1d:1e7:518d:6481/64 Scope:Global
          inet6 addr: fe80::4ebb:58ff:fe9c:f555/64 Scope:Link
          inet6 addr: 2401:4900:1955:16a2:4ebb:58ff:fe9c:f555/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:280316 errors:0 dropped:0 overruns:0 frame:0
          TX packets:179641 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:321303950 (321.3 MB)  TX bytes:36083350 (36.0 MB)

If you don't get any IP address from the DHCP server then run the following command to release/renew IP address:

dhclient –r -v
dhclient -v

Now, run the ifconfig command again to verify the IP address of the client.

Configure DHCP Client to Obtain Dynamic IP address

Next, go to the other client machine and configure it to obtain the IP address automatically from the DHCP server.

To do so, edit the network interface file with the following command:

nano /etc/network/interfaces

Add the following lines:

auto eth0
iface eth0 inet dhcp

Save and close the file then restart the Network Manager service to apply the changes:

systemctl restart network-manager

Next, run the following command to verify the IP address assigned by DHCP server:

ifconfig

You should get the following output:

eth0      Link encap:Ethernet  HWaddr 56:84:7a:fe:97:99  
          inet addr:192.168.0.11  Bcast:192.168.0.255  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:561189 (561.1 KB)  TX bytes:561189 (561.1 KB)

Conclusion

Congratulations! you have successfully installed and configured the DHCP server and client on Ubuntu 20.04. Now, all clients connected to the DHCP server will get IP addresses and other network information automatically. Feel free to ask me if you have any questions.

Share this page:

1 Comment(s)