Need to install DHCP on Linux?
This guide is for you.
DHCP (Dynamic Host Configuration Protocol) is a network protocol used to assign various network parameters to a device.
It dynamically assigns unique IP addresses and other information to client systems.
Some of the information that is assigned by DHCP includes hostname, domain name, IP address of the default gateway and DNS server, and so on. It saves the system administrator from the hassle of manually assigning IP addresses and related information to each of the client systems.
Usually, in the home or small company setup, routers serve the role of a DHCP server. However, in a large setup, we need to configure a dedicated server to serve as a DHCP server. In Linux, you can easily set up a DHCP server and assigns IP addresses to clients.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to configure DHCP on their Linux Server.
In this context, we shall look into how to configure DHCP in Linux to automatically assign IP addresses to clients.
Here, we will look into how DHCP works.
Consider a scenario where the client and DHCP server are connected to the same wired network.
i. When a client machine is turned on, it sends a broadcast message DHCPDISCOVER to every device on the network.
ii. When this message reached the DHCP server, it sends back a DHCPOFFER message with an IP address to client.
iii. The client then sends a DHCPREQUEST to DHCP server to request for this IP address.
iv. The DHCP server then sends DHCPACK to acknowledge the request and grants the client with an IP address.
To configure DHCP, you need the following:
i. Minimum two Linux machines (For DHCP server and client).
ii. User with sudo privileges.
iii. Static IP configured on DHCP server.
In this case, we have used three Linux machines which were on the same LAN. You can think of them as all are connected through each other via switch.
For DHCP server, We will use Ubuntu 20.04 LTS with static IP "192.168.9.1" and hostname "ubuntu".
For For DHCP client, we have used two machines, we will use:
i. Ubuntu 20.04 with hostname "linuxaptpc".
ii. Mint 20 with hostname "mint".
i. First, update the system repository index in your system. Issue the below command in Terminal to do so:
$ sudo apt update
ii. Now issue the following command in Terminal to install DHCP server:
$ sudo apt-get install isc-dhcp-server -y
Before configuring a Linux system as a DHCP server, make sure it has a static IP address configured so that clients can easily connect to it. Otherwise, it will be difficult for the clients to find which DHCP server to connect to.
i. In the /etc/default/isc-dhcp-server file, you will need to specify the interface on which DHCP server will listen to.
ii. Issue the following command in Terminal to edit this file:
$ sudo nano /etc/default/isc-dhcp-server
iii. In the INTERFACESv4 entry, specify the interface you want the DHCP server to listen to such as ens33, eth0, eth1, etc.
INTERFACESv4="interface_name"
iv. In our scenario, the interface is ens33, so the entry would change to:
INTERFACESv4="ens33"
v. Once you have configured the file, save, and close it.
vi. Now we will configure DHCP Server using its default configuration file located at /etc/dhcp/dhcpd.conf.
vii. Edit the /etc/dhcp/dhcpd.conf file as follows:
$ sudo nano /etc/dhcp/dhcpd.conf
Uncomment the following line by removing the # symbol before it.
authoritative;
viii. Also, add the following lines in the file:
default-lease-time 600;
max-lease-time 7200;
# subnet
subnet 192.168.9.0 netmask 255.255.255.0 {
# range of subnet
range 192.168.9.5 192.168.9.15;
# gateway address
option routers 192.168.9.1;
# DNS server address
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
ix. Once you have configured the file, save, and close it.
The DHCP server issues dynamic IP addresses to the clients by default. However, you can also configure it to assign fixed or static IP address to clients. Fixed addresses are required for some network devices like printers, routers, or when you need to remotely connect a device from outside. To assign a static IP address to a specific machine, you will need its MAC address.
Let's say we want to assign a client with MAC address 00:0c:29:c1:d5:d4 a fixed IP 192.168.1.13 every time.
i. Edit the /etc/dhcp/dhcpd.conf file and add host entry containing the MAC address of the client and the fixed IP address you want to assign to the client:
host mint {
hardware ethernet 00:0c:29:c1:d5:d4;
fixed-address 192.168.9.13;
}
Note: Fixed IP address should not be from the range of the Dynamic IP range.
Now, every time the client with the matching MAC address (00:0c:29:c1:d5:d4) sends a DHCPDISCOVER message to the DHCP server, will receive the same IP address 192.168.9.13.
Once you are done with the configurations, restart the DHCP server service. Issue the below command in Terminal to do so:
$ sudo systemctl restart isc-dhcp-server.service
Now verify the status of the DHCP server using the below command:
$ sudo systemctl status isc-dhcp-server.service
Now we will configure two client machines. One client will be assigned random IP from the subnet range defined in the configuration file. The other client will be assigned the static IP.
By default, dynamic IP addressing is configured in Ubuntu OS. However, if it is using the static IP address, you can configure it to obtain an IP address from the DHCP server.
i. First, find your network interface name using the following command:
$ ifconfig
ii. Once you have found the interface name, issue the following command in Terminal to configure the network interface:
$ sudo nano /etc/network/interfaces
iii. Insert the below lines in the file:
auto <interface_name>
iface <interface_name>inet dhcp
In our case, it would be:
auto ens33
iface ens33 inet dhcp
iv. Save and close the file and restart the network-manager service using the following command in Terminal:
$ sudo systemctl restart network-manager.service
v. Now run the ifconfig command to verify the IP address of your client.
$ ifconfig
In order to find the IP address of the DHCP server, issue the following command in Terminal:
$ sudo grep -R “DHCPOFFER” /var/sys/log
In case, there is no IP address obtained by the client machine from the DHCP server, then use the following commands to release/renew the IP address:
$ sudo dhclient -r
$ sudo dhclient -v
Now run the ifconfig command to verify the IP address of the client. This time hopefully the client will receive the IP address from the DHCP server.
For our second client machine, we have added the static/fixed IP address entry in the DHCP server configuration file. On the client machine, first configure the interface to acquire the IP address from the DHCP server.
Find your network interface name using the following command:
$ ifconfig
Once you have found the interface name, issue the following command in Terminal to configure the network interface:
$ sudo nano /etc/network/interfaces
Insert the below lines in the file:
auto <interface_name>
iface <interface_name>inet dhcp
In our case, it would be:
auto ens33
iface ens33 inet dhcp
Now save and close the file and restart the network-manager service using the following command in Terminal:
$ sudo systemctl restart network-manager.service
Now run the ifconfig command to verify the IP address of your client.
$ ifconfig
In order to find the IP address of the DHCP server, issue the following command in Terminal:
$ sudo grep -R “DHCPOFFER” /var/sys/log
In case, there is no IP address obtained by the client machine from the DHCP server, then use the following commands to release/renew IP address:
$ sudo dhclient –r -v
$ sudo dhclient -v
Now run the ifconfig command to verify the IP address of the client. This time hopefully the client will receive the IP address from the DHCP server.
From the DHCP server, you can also verify the dynamic addresses assigned by it to the clients.
In the DHCP server machine, run the following command in Terminal to list the addresses assigned by the DHCP server:
$ dhcp-lease-list
You will see only the IP addresses dynamically assigned by the DHCP server to a client. Remember, it will not list the static IP addresses that are issued by the DHCP server.
This article will guide you on steps to install and configure the #DHCP #server in the Linux system and assign dynamic and fixed #IP addresses to the clients. It takes just a few simple steps to configure the DHCP server in a #Linux machine. Once you have configured it, it will automatically assign the assigning IP addresses and related information to each machine on the network.
The main DHCP #configuration file is /etc/dhcp/dhcpd. conf. The file is used to store the network configuration information required by DHCP #clients.
To enable DHCP or change other TCP/IP settings:
1. Select Start , then select Settings > #Network & Internet.
2. Do one of the following: For a Wi-Fi network, select Wi-Fi > Manage known networks.
3. Under IP assignment, select Edit.
4. Under Edit IP settings, select Automatic (DHCP) or Manual.
5. When you're done, select Save.