Linux-DHCP principle and configuration (working process of DHCP, configuring dynamic host and installing server) (configuration steps)

One, understand the DHCP service

1 Overview

  • DHCP (Dynamic Host Configuration Protocol, dynamic host configuration protocol), is designed and developed by the Internet task force, specifically used to automatically assign TCP/IP parameters to the computer in the TCP/IP network protocol
  • DHCP service avoids errors caused by manually setting the IP address, and also avoids address conflicts caused by assigning an IP address to multiple workstations
  • DHCP provides safe, reliable and secure TCP/IP network settings, reducing the burden of configuring IP addresses

2. Advantages

The Internet is currently one of the most user-friendly services in the world. Hundreds of millions of people use the Internet. Due to the uncertainty of online time and the different technical levels of users, each user is assigned a fixed IP address, which not only causes IP The waste of addresses will also bring high maintenance costs to ISP service providers. Using DHCP service has the following advantages:

  • Reduce the workload of administrators
  • Avoid IP address conflicts
  • When the network changes the IP address segment, there is no need to reconfigure each user's IP address
  • Improved utilization of IP addresses
  • Convenient client configuration

3. Distribution method

  • The typical application mode of DHCP is as follows
    • Assuming a dedicated DHCP server in the network, responsible for the centralized distribution of various network address parameters (mainly including IP address, subnet mask, broadcast address, default gateway address, DNS server address)
    • Other hosts act as DHCP clients, configure the network card to automatically obtain addresses, and then communicate with the DHCP server to complete the automatic configuration process
    • In the working principle of DHCP, the DHCP server provides three allocation methods: automatic allocation, manual allocation, and dynamic allocation
  • Automatic allocation:
    When the DHCP client successfully obtains an IP address from the DHCP server for the first time, it will use this IP address permanently
  • Manual allocation: the
    IP address is specified by the DHCP server administrator
  • Dynamic allocation:
    When a DHCP client obtains an IP address from a DHCP server for the first time, it does not use the address permanently, but after each use, the DHCP client releases the IP address for other clients to use

2. The working process of DHCP

  • The process by which the
    client obtains an IP address from the DHCP server is called the DHCP lease process. The process by which the client obtains an IP address from the DHCP server is called the DHCP lease process.
  • Divided into the following four steps

1. The client searches for the server in the network

  • When a DHCP client is started, the client does not have an IP address, so the client must obtain a legal address through DHCP
  • At this time, the DHCP client broadcasts the DHCP Discover to find the DHCP server

2. The server responds to the client

  • When the DHCP server receives the information requesting the IP address from the client, it searches its own IP address pool to find out whether there is a legal IP address provided to the client
  • If so, the DHCP server will mark the IP address, add it to the DHCP Offer message, and then broadcast a DHCP Offer message

3. The client selects the IP address

  • The DHCP client extracts the IP address from the first DHCP Offer message received
  • The DHCP server that issued the IP address reserves the address so that the address cannot be assigned to another DHCP client

4. The server determines the lease

  • After the DHCP server receives the DHCP Request message, it broadcasts a successful confirmation to the client in the form of a DHCP ACK message
  • The message contains a valid lease of the IP address and other configurable information
  • When the client receives the DHCP ACK message, configure the IP address and complete the initialization of TCP/IP

5. Log in again

  • The DHCP client does not need to send DHCP Discover information every time it logs on to the network again
  • Instead, it directly sends the DHCP Request request message containing the IP address assigned last time

6. Renew the lease

  • When the IP address leased by the DHCP server to the client reaches 50%, the lease must be renewed
  • The client directly sends a DHCP Request packet to the server providing the lease, requesting to renew the existing address lease

Three, use DHCP to dynamically configure the host address

1. DHCP service

  • Automatically allocate addresses for a large number of clients and provide centralized management
  • Reduce management and maintenance costs and improve network configuration efficiency

2. The address information that can be allocated mainly includes

  • The IP address and subnet mask of the network card
  • Corresponding network address, broadcast address
  • Default gateway address
  • DNS server address

Fourth, install the DHCP server

1. DHCP service software

  • Dhcp-4.2.5-47.el7.centos.x86_64.rpm in the CentOS CD
    • Generally there are, if not, install RPM
  • The main files of the DHCP software package
    • Main configuration file: /etc/dhcpd.conf
    • Execution program: /usr/sbin/dhcpd, usr/sbin/dhcrelay

2. The main configuration file

  • The content of dhcpd.conf
ddns-update-style  interim;     #全局配置参数
subnet  netmask {       #subnet以上的都是全局配置参数;网段声明(网段、子网掩码、默认网关地址)
   option routers;       #配置选项
   default-lease-time 21600;   #配置参数
   host ns {
      fixed-address;    #主机声明(固定一个IP给主机用的)
  • Global configuration, acting on the entire DHCP server
    • ddns-update-style none;
    • default-lease time 21600;
    • max-lease-time 43200;
    • option domain-name “”;
    • option domain-name servers;
  • Subnet segment statement, which applies to the entire subnet segment
    • Range parameter: set the IP address pool for allocation
    • option subnet-mask parameter: set the client's subnet mask
    • option routers parameter: set the default gateway address of the client
[root@xcf1 ~]# vim /etc/httpd/conf.d/^C

Five, configure the DHCP server steps

1. Use DHCP to dynamically assign IP addresses to PCs

1.1 eNSP

  • The experimental topology is as follows
  • Note: After configuring "Cloud", you can use the serial cable to connect
  • Configure Cloud
    (Note: the first choice is UDP, the second choice is only host mode VMnet1, do not choose 8, it comes with dhcp function, which conflicts with this experiment)
  • SW1 basic configuration
[Huawei]sys SW1
[SW1]un in en
Info: Information center is disabled.
[SW1]vlan batch 10 20 100
Info: This operation may take a few seconds. Please wait for a moment...done.
[SW1]int e0/0/1
[SW1-Ethernet0/0/1]p l a
[SW1-Ethernet0/0/1]p d v 10
[SW1-Ethernet0/0/1]int e0/0/2
[SW1-Ethernet0/0/2]p l a
[SW1-Ethernet0/0/2]p d v 20
[SW1-Ethernet0/0/2]int e0/0/4
[SW1-Ethernet0/0/4]p l a
[SW1-Ethernet0/0/4]p d v 100
[SW1-Ethernet0/0/4]int e0/0/3
[SW1-Ethernet0/0/3]p l t
[SW1-Ethernet0/0/3]p t a v a
  • SW2 basic configuration
[Huawei]sys SW2
[SW2]un in en
Info: Information center is disabled.
[SW2]vlan batch 10 20 100
Info: This operation may take a few seconds. Please wait for a moment...done.
[SW2]int Vlanif 10
[SW2-Vlanif10]ip add 24
[SW2-Vlanif10]un sh
Info: Interface Vlanif10 is not shutdown.
[SW2]int Vlanif 20
[SW2-Vlanif20]ip add 24
[SW2-Vlanif20]un sh
Info: Interface Vlanif20 is not shutdown.
[SW2]int Vlanif 100
[SW2-Vlanif100]ip add 24
[SW2-Vlanif100]un sh
Info: Interface Vlanif100 is not shutdown.
[SW2-Vlanif100]int g0/0/1
[SW2-GigabitEthernet0/0/1]port link-type trunk 
[SW2-GigabitEthernet0/0/1]port trunk allow-pass vlan all 
  • Configure DHCP relay (Layer 3 switch)
[SW2]dhcp enable        ##开启DHCP功能
Info: The operation may take a few seconds. Please wait for a moment.done.
[SW2]int Vlanif 10        ##配置虚接口ip,选择dhcp中继模式,请求指向dhcp服务器ip
[SW2-Vlanif10]dhcp select relay 
[SW2-Vlanif10]dhcp relay server-ip
[SW2]int Vlanif 20
[SW2-Vlanif20]dhcp select relay 
[SW2-Vlanif20]dhcp relay server-ip
[SW2-Vlanif20]int Vlanif 100
[SW2-Vlanif100]dhcp select relay 
[SW2-Vlanif100]dhcp relay server-ip

1.2 Virtual Machine

  • Select the virtual machine network type
  • Configure the network card IP
  • Install the dhcp software package, and view and find the configuration file
[root@xcf1 ~]# yum -y install dhcp
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 12:dhcp-4.2.5-58.el7.centos.x86_64 已安装并且是最新版本
[root@xcf1 ~]# cd /etc/dhcp/        ##dhcp的配置文件是/etc/dhcp/dhcpd.conf
[root@xcf1 dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
[root@xcf1 dhcp]# less dhcpd.conf        ##查看一下内容
[root@xcf1 ~]# cd /usr/share/doc/dhcp-4.2.5/        到该目录下去寻找dhcp配置模板
[root@xcf1 dhcp-4.2.5]# ls        ##“dhcpd.conf.example”
dhcpd6.conf.example  dhcpd.conf.example  ldap
[root@xcf1 dhcp-4.2.5]# cp dhcpd.conf.example /etc/dhcp/dhcpd.conf        ##将模板覆盖到/etc/dhcp/dhcpd.conf中
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y
[root@xcf1 ~]# cd /etc/dhcp/        ##回到/etc/dhcp目录下
[root@xcf1 dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
[root@xcf1 dhcp]# less dhcpd.conf        ##查看配置文件并进行下一步编辑
  • Edit dhcp configuration file
    • [root@xcf1 dhcp]# vim dhcpd.conf
  • Turn off the firewall, restart the network card, and then go to "ping" to verify the network
[root@xcf1 dhcp]# systemctl stop firewalld
se[root@xcf1 dhcp]# setenforce 0
[root@xcf1 dhcp]# systemctl restart dhcpd

[root@xcf1 dhcp]# ping
[root@xcf1 dhcp]# ping
[root@xcf1 dhcp]# ping

1.3 Verification

  • Verify in eNSP
  • First turn on the DHCP of the two PCs to automatically obtain an IP address
  • Enter the command line "ipconfig"
    • pc1
    • pc2

2. Assign a fixed IP address to the PC

  • First change the network adapter of the virtual machine Win10 to VMnet1 host-only mode

2.1 Modify the host network adapter (note that it is not a virtual machine!)


  • Set up VMnet1

2.2 Find the host MAC address in the w10 virtual machine


  • Enter "ipconfig/renew" or "ipconfig/release" to refresh the network information (press enter to display)
  • View MAC address

2.3 Edit the dhcp configuration file in the virtual machine CentOS7

[root@xcf1 ~]# cd /etc/dhcp/
[root@xcf1 dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
[root@xcf1 dhcp]# vim dhcpd.conf 


  • Remember to press wq to save and exit
  • The modification is complete, restart the dhcp service
[root@xcf1 dhcp]# systemctl stop dhcpd        ##重启DHCP服务
[root@xcf1 dhcp]# systemctl start dhcpd

2.4 Go back to win10 and refresh the network configuration and then check the bound IP.


  • You're done, go to bed
    Insert picture description here

Guess you like