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

Python Flask WeChat applet login process and login api implementation code

1. Let’s take a look at the effect first Data ret...

Running PostgreSQL in Docker and recommending several connection tools

1 Introduction PostgreSQL is a free software obje...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

HTML table tag tutorial (13): internal border style attributes RULES

RULES can be used to control the style of the int...

Solution to MySQL failure to start

Solution to MySQL failure to start MySQL cannot s...

Summary of Mysql high performance optimization skills

Database Command Specification All database objec...

HTML table tag tutorial (45): table body tag

The <tbody> tag is used to define the style...

MySQL Optimization: Cache Optimization

I am happy that some bloggers marked my article. ...

MySQL 5.6.27 Installation Tutorial under Linux

This article shares the installation tutorial of ...

MySQL 8.0.14 installation and configuration method graphic tutorial

This article records the installation and configu...

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later wi...

10 Tips to Improve Website Usability

Whether it is a corporate website, a personal blo...

Install Percona Server+MySQL on CentOS 7

1. Environmental Description (1) CentOS-7-x86_64,...