Logo

How to Setup DHCP Server in Ubuntu Server 20.04

Jul 24, 2021

DHCP is a service used to automatically assign network configuration to client systems. DHCP can be used as a handy tool when you have a large pool of systems that needs to be configured for network settings. Plus, when you need to change the network configuration, say to update a DNS server, all you need to do is update the DHCP server and all the connected hosts will be reconfigured with new settings. Also, you get reliable IP address configuration that minimizes configuration errors and address conflicts. You can easily add a new host to the network without spending time on network planning.

DHCP is most commonly used to provide IP configuration settings, such as IP address, net mask, default gateway, and DNS servers. However, it can also be set to configure the time server and hostname on the client.

DHCP can be configured to use the following configuration methods:

Manual allocation: Here, the configuration settings are tied with the MAC address of the client's network card. The same settings are supplied each time the client makes a request with the same network card.

Dynamic allocation:This method specifies a range of IP addresses to be assigned to the clients. The server can dynamically assign IP configuration to the client on first come, first served basis. These settings are allocated for a specified time period called lease; after this period, the client needs to renegotiate with the server to keep using the same address. If the client leaves the network for a specified time, the configuration gets expired and returns to pool where it can be assigned to other clients. Lease time is a configurable option and it can be set to infinite.

Follow these steps to install a DHCP server:

Step 1 : Install a DHCP server:

sudo apt-get install isc-dhcp-server

Step 2 : Open the DHCP configuration file:

sudo nano /etc/dhcp/dhcpd.conf

Step 3 : Change the default and max lease time if necessary:

default-lease-time 600;
max-lease-time 7200;

Step 4 : Add the following lines at the end of the file (replace the IP address to match your network):

subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.80 192.168.1.90;
    option routers 192.168.1.1;
    option domain-name-servers 192.168.1.2, 192.168.1.3;
    option domain-name "example.com";
  }

This will result in IP addresses ranging from 192.168.1.80 to 192.168.1.90 to be assigned to clients. The default lease time is set to 600 seconds with maximum bound of 7200 seconds. A client can ask for a specific time to a maximum lease period of 7200 seconds. Additionally, the DHCP server will provide a default gateway (routers) as well as default DNS servers.

Step 5 : You can reserve an IP address to be assigned to a specific device on network. Reservation ensures that a specified device is always assigned to the same IP address. To create a reservation, add the following lines to dhcpd.conf. It will assign IP 192.168.1.88 to the client with the 08:00:07:26:c0:a5 MAC ID:

host Server1 {
    hardware ethernet 08:00:07:26:c0:a5;
    fixed-address 192.168.1.88;
  }

Step 6 : Save the configuration file and exit with Ctrl + O and Ctrl + X.

Step 7 : After changing the configuration file, restart dhcpd

sudo service isc-dhcp-server restart