We’re here to discuss how to use DHCP on Linux and its uses. Find out everything you need to know about the protocol and its uses in networking.
Dynamic Host Configuration Protocol (DHCP) is used to assign the IP addresses dynamically to nodes. This enables the device to use services like network time protocol (NTP), domain name service (DNS), and other transmission control protocol (TCP)/ user datagram protocol (UDP) based services. This protocol can be used to change the configuration of network devices.
After the configuration of these devices, they can easily communicate with other nodes and services on the network. These parameters include internet protocol (IP) address, subnet mask, DNS servers, gateways, and routers. DHCP is based on a client-server model. Most routers have built-in DHCP support; however, you can also set up your own DHCP server.
We will briefly discuss the DHCP protocol along with how it works on Linux.
About DHCP
Without DHCP, network administrators will be required to set up and configure hosts on the network manually. Using DHCP, the nodes in the network will discover the DHCP server that assigns network parameters and configuration dynamically to the nodes. Primarily, DHCP protocol comprises a client and a server.
The server side is installed at a central location accessible to every other host in the network. The client component is installed on individual nodes that would like to acquire IP addresses and other configurations from the server. The overall protocol includes information about how individual devices communicate with the central server to get the IP address and other information without conflicting with other network nodes.
The various configuration parameters attached to a node by the DHCP server are valid for a specific time period. The process is called DHCP lease, while the period is called DHCP lease time. Once the lease expires, the client can’t further use the same configuration to access network services. Using the lease renewal mechanism, the client can further extend its lease time period.
How to Use DHCP on Linux
DHCP servers are usually deployed in small offices as well as large corporations. For larger networks, there are relay agents at routers that can be used to serve numerous links. The various use cases include initial client connection, IP usage extension, client connection after a reboot, and client disconnection.
DHCP client-server model
DHCP is a client-server model and comprises a client and a server component. Any device willing to acquire network configuration will first broadcast a request intended for the DHCP server. The client usually sends this request whenever it boots up or before the expiry of its lease time periodically.
The problem is that when the client boots up, it doesn’t have any IP address assigned to it. So, to acquire an IP address, it sends a request to the DHCP server via broadcasting. Upon receiving the request, the DHCP server sends a response back to the client.
The actual response depends on several factors, such as previous administration configuration, IP address, and other relevant information about the network.
Running DCHP protocol on Linux
For Linux, the dhcpd
daemon is used to run the DHCP server. The server will read the /etc/dhcpd.conf
containing information about various configurations, such as available IP address, and lease time. Let’s discuss the basic steps to configure DHCP in Linux.
To install the DHCP server on Ubuntu, run the following command:
$ sudo apt install isc-dhcp-server
Now to start the DHCP server, you need to define a subnet n dhcpd.conf
file. Open a terminal and change to the directory via the following command:
$cd /etc
Now, open your favorite editor, such as nano, to edit the file as follows:
$nano dhcpd.conf
Enter the following information in the file and save:
subnet 10.1.1.0 netmask 255.255.255.9 {
range 10.1.1.3 10.1.1.254;
}
subnet 192.168.0.0 netmask 255.255.0.0 {
}
Here, we have defined two subnet masks: 10.1.1.0
and 192.168.0.0
. The second one is an empty definition. While the first one will listen on the subnet 10.1.1.0
and assign IP addresses in the range 10.1.1.3
to 10.1.1.254
.
Setting other configuration information
There are several other settings we can specify in the dhcpd.conf
file. These may include the following information:
- IP addresses
- Network masks
- Domain Names servers ( DNS )
- NTP servers
- Default Gateways
- Syslog hosts
- Proxy servers
- X Font servers
Let’s first set the default DHCP lease time, which is the maximum amount of time for which the IP address is leased to the specific node unless a renewal request is made by the client. The following lines show the changes you have to make in dhcpd.conf
:
default-lease-time 600;
max-lease-time 7200;
Similarly, you can set the DNS server information as follows:
subnet 10.1.1.0 netmask 255.255.255.0 {
range 10.1.1.3 10.1.1.254;
option domain-name-servers 10.1.1.1, 8.8.8.8;
}
The following lines set the default gateway to 10.1.1.1
:
subnet 10.1.1.0 netmask 255.255.255.0 {
range 10.1.1.3 10.1.1.254;
option domain-name-servers 10.1.1.1, 8.8.8.8;
option routers 10.1.1.1;
}
We discussed details about the DHCP server, its client-server response cycle, and various use cases. Finally, we discussed how DHCP could be run on Linux with a step-by-step guide on how to do it. More information about DCHP can be obtained from the Linux manual.
If this guide helped you, please share it.