How to Install and Configure DHCP on Ubuntu 18.04

configure dhcp ubuntu

A DHCP server configuration properties include Host Name, Domain Name, Time Server and Print Server. The server can allocate IP using client MAC address, using address pool and using automatic method.

In this article, we are going to show how to install and configure dhcp on Ubuntu 18.04. We are going to install isc-dhcp-server, set IP range which will use the DHCP server to provide IPs to clients and assign a fixed IP address to a client with specific MAC address.

How to install DHCP server

First of all, you need to install the DHCP server, which you can do by running the following command:

sudo apt install isc-dhcp-server

How to configure DHCP server

Once the installation is complete, you need to assign network interfaces on which the DHCP server will serve. To do so, edit the default configuration file of the DHCP server via any text editor (I've used vim for that purpose):

sudo vim /etc/default/isc-dhcp-server

In the default configuration file edit the value of INTERFACESv4 and write the one you want the DHCP server to serve requests.

INTERFACESv4="eth0"

If you have more than one interface DHCP to serve add them inside the quotes separating with spaces.

INTERFACESv4="eth0 eth1"

Once the interface(s) is(are) assigned, you can proceed to DHCP server configuration. To configure the DHCP  server edit the /etc/dhcp/dhcpd.conf file via any text editor. To do so, type the command below:

sudo vim /etc/dhcp/dhcpd.conf

Change the domain name and domain name servers (DNS) according to yours in the section mentioned below:

# option definitions common to all supported networks...
 option domain-name "your_domain.com";
 option domain-name-servers ns1.your_domain.com, ns2.your_domain.com;

If this DHCP server is the official DHCP server for the local network, the authoritative directive should be uncommented.

authoritative;

For an internal subnet configuration find the section with  "A slightly different configuration for an internal subnet." comment, uncomment all lines in the section and change the values according your needs. For example:

subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.150 192.168.0.170;
option domain-name-servers ns1.your_domain.com;
option domain-name "local.your_domain.com";
option subnet-mask 255.255.255.0;
option routers 192.168.0.1;
option broadcast-address 192.168.0.255;
default-lease-time 600;
max-lease-time 7200;
}

In this configuration, we have mentioned the local domain name, DNS, IP range from which IPs will be assigned to clients, default and max lease times.

Restart the DHCP server and it will start serve according to your configuration. To restart, type:

systemctl restart isc-dhcp-server

Assign fixed IP address to client with specific MAC address

If you want to assign specific IP address to specific client you can use client's MAC address to achieve that goal. This means that the fixed IP address will be assigned to the client whose MAC address is configured in the configuration file and it won't be assigned to any other client.

To check MAC address on the client's machine run:

ip a

The output will look like the one below:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
 link/ether 00:0c:29:39:c7:81 brd ff:ff:ff:ff:ff:ff
 inet 192.168.0.125/24 brd 192.168.0.255 scope global dynamic eth0
 valid_lft 73924sec preferred_lft 73924sec
 inet6 2600:3c01::f03c:91ff:fe62:5d78/64 scope global dynamic mngtmpaddr noprefixroute
 valid_lft 598sec preferred_lft 298sec
 inet6 fe80::f03c:91ff:fe62:5d78/64 scope link
 valid_lft forever preferred_lft forever

The underlined part is the MAC address of the mentioned network adapter. To assign fixed IP address to specific MAC address, edit the vim /etc/dhcp/dhcpd.conf file and add the following section (you must add different sections for different clients):

host fixed-ip-client {
 hardware ethernet 00:0c:29:39:c7:81;
 fixed-address 192.168.0.220;
 }

As you can notice we've given an IP address to the client out of the range that we configured to use the DHCP server. If you use an IP address within the IP range, DHCP server will skip that IP address to lease to clients dyamically and your IP range actually will be decreased by one IP address.

After making changes in the configuration file, save it and restart the DHCP server to apply the changes. To restart type the following command:

systemctl restart isc-dhcp-server

Client side configuration

On client machine, you need to make sure that Automatic(DHCP) method is set in IPv4 settings and restart your connection if you are already connected. To do so follow these steps. Press on network connection icon on the top right corner of your screen, make sure "Enable Networking" is checked, disconnect from you current network if you are connected and press edit connections as it's shown in the screenshot below:

ubuntu18 network connections

Once "Edit connections" window is opened, select your connection and press edit:

Ubuntu18 DHCP settings

In the opened window select IPv4 Settings tab, choose Automatic(DHCP) as default method and press "Save" button:

Ubuntu18 DHCP settings

Close all opened windows, press on the network connections icon again and press on your connection name to connect to the network.

Ubuntu18 DHCP settings

Once all these steps are done, the client machine will get the IP address from DHCP server. However, there can be cases when just disconnect/connect will not make changes happen. For that cases you can restart network service to apply changes and receive IP address from new DHCP server as we have set the isc-dhcp-server as authoritative. Thus it will be the default dhcp server of your subnet. To restart the network service on Ubuntu 18 machine command line you can run one of the following commands:

sudo systemctl restart NetworkManager.service

or

sudo service network-manager restart

After network service restart, if you check network configuration with ifconfig or ip command, you will see output like the one below:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:39:c7:81 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.220/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 73924sec preferred_lft 73924sec
inet6 2600:3c01::f03c:91ff:fe62:5d78/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 598sec preferred_lft 298sec
inet6 fe80::f03c:91ff:fe62:5d78/64 scope link
valid_lft forever preferred_lft forever

This exact output is from the client machine that we have configured to receive fixed IP address. As it's seen from output it received 192.168.0.220 IP address which we have set in /etc/dhcp/dhcpd.conf file on dhcp server.

Read also

The DHCP server installation and configuration will not take more than few minutes from you if you refer to this tutorial. Setting up the DHCP server network admins are saving a lot of time which they would spend configuring clients' machines one by one.

Hayk Gevorgyan 1:54 am

About Hayk Gevorgyan

Technical Support Engineer experienced in Linux servers administration of production environments. Exploring DevOps culture and tools. Interested in containerization and open source monitoring tools.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.