How to build a DHCP server in Linux

How to build a DHCP server in Linux

1. Basic knowledge:

1. DHCP Introduction:
DHCP (Dynamic Host Configuration Protocol) is usually used in large local area network environments. Its main function is to centrally manage and allocate IP addresses, so that hosts in the network environment can dynamically obtain IP addresses, Gateway addresses, DNS server addresses and other information, and can improve the utilization rate of addresses.
2. DHCP server introduction:
A DHCP server refers to a server that controls a range of IP addresses. When a client logs in to the server, it can automatically obtain the IP address and subnet mask assigned by the server.

2. DHCP server configuration:

Today we will make a DHCP server:

這里寫圖片描述

I use the virtual machine server as a DHCP server and use destop for testing. For convenience, I change the server host name to dhcp.server.com and the desktop host name to dhcp.test.com
Next, we configure it on the host dhcp.server.com:

1. Check the server's IP

這里寫圖片描述

2. Check the dependency packages of the DHCP service:

這里寫圖片描述

3. Install DHCP service:

這里寫圖片描述

4. Configure:

(1) Enable DHCP service

systemctl start dhcpd

(2) Change the configuration file

cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

vim /etc/dhcp/dhcpd.conf
Change the following section:

  7 option domain-name "server.com"; 
      ##Domain name: see /etc/resolv.conf
  8 option domain-name-servers 172.25.10.254;
      ##Specify DNS servers, separate multiple servers with commas.
 30 subnet 172.25.10.0 netmask 255.255.255.0 {
      ##Specify subnet and subnet mask 31 range 172.25.10.10 172.25.10.20;
      ##Specify IP range 32 option routers 172.25.10.254;
 ##Specify the default gateway 33 }
 34#### Delete lines 27, 28, 34 and beyond

The following is the configuration on dns.test.com:
(1) Network parameter settings:
Edit /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=dhcp
###The key part is dhcp
ONBOOT=yes

(2) Restart the network:

systemctl restart network
###In this experiment, I encountered the following problem: an error occurred when executing the command, and an IP address could not be obtained through the DHCP server. The solution is: DHCP is usually a communication protocol used in a local area network. It mainly sends broadcast data packets to all hosts in the entire physical network segment through the client. Only when there is a DHCP server in the local area network will it respond to the client's IP parameter requirements. Therefore, the DHCP server and client should be in the same physical network segment. The entire DHCP packet interaction between the server and the client is shown in Figure (1):
###So I guess the firewall is blocking the transmission of data packets###The solution I took is:
Both virtual machines execute the command:
firewall-cmd --permanent --add-service=dhcp
firewall-cmd --reload 
Check whether DHCP is working:
firewall-cmd --list-all
The problem is solved after restarting the virtual machine. 


(Figure (1) Schematic diagram of the interaction between DHCP packets between the server and the client)

The test machine obtains IP through the DHCP server:

這里寫圖片描述

(3) After the entire network is restarted, if the execution result finds the correct DHCP host, the following files may be modified.

1. View /etc/resolv.conf

這里寫圖片描述

2. Check the route

這里寫圖片描述

3. View the DHCP information recorded by the server cat /var/lib/dhcpd/dhcpd.leases

這里寫圖片描述

3. A configuration example

cat /etc/dhcp/dhcpd.conf

#dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 192.168.0.199;

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

# Use this to enable / disable dynamic dns updates globally.
#ddns-update-style none;

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

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.


# This is a very basic subnet declaration.

subnet 192.168.0.0 netmask 255.255.255.0 {
  range 192.168.0.10 192.168.0.20;
  option routers 192.168.0.199;
}

This is the end of this article about setting up a DHCP server in Linux. For more information about setting up a Linux DHCP server, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Linux starts dhcp server steps
  • DHCP service configuration tutorial under Linux
  • Complete configuration of DHCP service under Linux (with pictures and text)
  • A very detailed explanation of the Linux DHCP service

<<:  25 fresh useful icon sets for download abroad

>>:  v-for directive in vue completes list rendering

Recommend

How to implement Mysql switching data storage directory

How to implement Mysql switching data storage dir...

Keepalived implements Nginx load balancing and high availability sample code

Chapter 1: Introduction to keepalived The purpose...

JS Asynchronous Stack Tracing: Why await is better than Promise

Overview The fundamental difference between async...

A pitfall and solution of using fileReader

Table of contents A pitfall about fileReader File...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

Teach you how to get the pointer position in javascript

The method of obtaining the position of the point...

Setting up shadowsocks+polipo global proxy in Linux environment

1. Install shadowsocks sudo apt-get install pytho...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...

How to use geoip to restrict regions in nginx

This blog is a work note environment: nginx versi...

Common HTML tag writing errors

We better start paying attention, because HTML Po...

Press Enter to automatically submit the form. Unexpected discovery

Copy code The code is as follows: <!DOCTYPE ht...

What to do if the container started by docker run hangs and loses data

Scenario Description In a certain system, the fun...