DHCP (Dynamic Host Control Protocol), as you all know is a network protocol that automatically assigns IP addresses to clients machines in the network. It eliminates the tedious work of manually assigning IP address to every machine in a large network. In addition, it removes the conflict that occurs because of mistakenly assigning the same IP address to multiple machines. We can define IP ranges in the DHCP server configuration. Along with IP address, we can also define other information too like subnet mask, default gateway, DNS server information, etc. and this information is then distributed to client machines across the network.

In this article, we will see how to install a DHCP server in Ubuntu and configure one client to obtain IP address automatically from that DHCP server. We will use Ubuntu 18.04 LTS describing the procedure mentioned in this article. You can follow the same procedure in the previous releases of Ubuntu too.

Installing DHCP Server

Before proceeding towards installing a DHCP server, first update the packages by running the following command in Terminal:

$ sudo apt get update

Then run the following command in the Terminal to install DCHP server:

$ sudo apt-get install isc-dhcp-server -y

Install DHCP Server

Wait for a while until the installation is completed.

Configuring DHCP Server

After the installation of DHCP server is completed, we will move towards its configuration. The IP address of our DHCP server is 192.168.110.1. To find the IP address of your DHCP server, type the following command in Terminal:

$ ip a

ip a command

DHCP configuration file is located at /etc/dhcp/dhcpd.conf. We can open this file by running the following command in Terminal

$ sudo nano /etc/dhcp/dhcpd.conf

Defining the Subnet

Add the following lines in the configuration file to define the subnet, range of IP addresses, domain and domain name servers like below:

To define Subnet, add the following lines:

subnet 192.168.110.0 netmask 255.255.255.0 {

When defining subnet information (range, default gateway, domain name server), make sure to end the lines with a semi-colon (;) and enclosed them in curly braces { }.

To specify the range of leased addresses, add the following line. The range defines the set of IP address pool, from which the IP addresses are allocated to DHCP clients

range 192.168.110.5 192.168.1.10;

To specify the default gateway, add the following line:

option routers 192.168.110.1;

To specify the domain name servers, add the following line:

option domain-name-servers 8.8.8.8, 8.8.4.4;

Make the changes as per your network values.

Define subnet of addresses to be handed out by DHCP

DHCP Global configuration

Here are the basic steps that we need in order to configure the global settings for setting up a DHCP server.

1. To specify the default and maximum lease time, find the parameters default-lease-time and max-lease-time in the config file and change their values.

default-lease-time 600;

max-lease-time 7200;

2. If you have multiple interfaces, you will need to define which interface the DHCP server should use to serve DHCP requests. In the configuration file, find and edit the value of INTERFACESv4 and modify it with the interface that we want to serve the requests.

INTERFACESv4="eth0"

3. To make DHCP server the official DHCP server for the clients, uncomment the following line in the configuration file (by removing the # character):

$ authoritative;

This is the basic configuration that we need in order to run a DHCP server. Once done with it, save and close the configuration file.

Manage DHCP services

Once you have done with the configuration, you can use the following commands to manage the DHCP server:

To verify if the service is running fine, check the status of DHCP service by running the following command in Terminal:

$ sudo systemctl status isc-dhcp-server.service

Following is the screenshot showing the status of the DHCP server. You can see active (running) highlighted in green verifying that DHCP server is running fine:

Check DHCP server status

There are some more command that we can use to start, stop and restart the DHCP service.

To start the DHCP service, run the following command in Terminal:

$ sudo systemctl start isc-dhcp-server.service

To stop the DHCP service, run the following command in Terminal:

$ sudo systemctl stop isc-dhcp-server.service

To restart the DHCP service, run the following command in Terminal:

$ sudo systemctl restart isc-dhcp-server.service

Configuring DHCP Client

We will need to configure the network settings in the client computer to obtain an IP address from a DHCP server. Here we will use another Ubuntu 18.04 LTS as a client computer.

In the client computer, open Settings application from Ubuntu’s Dash menu.

Network settings

Then select the Network tab from the left pane of the Settings application. Then open the adapter settings by clicking on the cog icon in front of it. Make sure it is turned on.

DHCP configuration

It will open the adapter settings window. Select the IPv4 tab from the top menu. Then select the Automatic (DHCP) option. Then click Apply to save the changes.

IPv4 > Automatic (DHCP)

Then restart network services by running the following command in Terminal:

$ sudo systemctl restart NetworkManager.service

Now Launch the Terminal and type the following command to find the IP address of the system.

$ ip a

Check network config with IP command

You will see an IP address that will be from the range that we have defined in the DHCP server configuration. If the client still does not obtain IP address from the DHCP server, restart your system.

List leased addresses

To find out what addresses have been assigned to clients by the DHCP server, open the machine which you have configured as a DHCP server, and type the following command in Terminal:

$ dhcp-lease-list

The lease is the amount of time for which an IP address is assigned to a computer.

List of leased DHCP addresses

From this list, you can verify our DHCP client with the MAC: 00:0c:29:d4:cf:69 has provided with the IP address 192.168.110.5 from the DHCP server.

Now the setup has completed and we have a DHCP server up and running. We can now use this DHCP server to assign IP addresses.

 

How to setup DHCP server on Ubuntu