A very detailed explanation of the Linux DHCP service

A very detailed explanation of the Linux DHCP service

1. DHCP Service (Dynamic Host Configuration Protocol)

1. Background

1. Manual settings are labor-intensive and prone to conflicts

2. Using DHCP can reduce workload and avoid address conflicts

2. Overview

  • Function: Assign IP addresses to computers in the LAN
  • Architecture C/S; Client/Server mode
  • Implementation:

1. Assign IP addresses to computers in the same network segment—DHCP

2. Assign IP addresses to computers in another network segment across network segments—DHCP relay

  • DHCP uses the UDP protocol, port number 67 is used by the client, and port number 68 is the server port number.

3. Advantages

1. Reduce the workload of administrators

2. Avoid the possibility of input errors

3. Avoid IP address conflicts

4. When changing the IP address segment, there is no need to reconfigure the IP address of each user

5. Improved the utilization rate of IP addresses

6. Convenient client configuration

4.DHCP message type

Take job hunting as an example:

DHCP Discover - The message used by the client to find the DHCP server. I submit my resume to the company to find a job.

DHCP Offer - The message used by the DHCP server to respond to the DHCP Discover message. The message carries various configuration information, such as IP address, DNS, etc. The company sent me an offer after receiving my resume.

DHCP Request - The client requests configuration confirmation or lease renewal. I will confirm with the company after receiving the offer.

DHCP ACK - the server's confirmation response to the Request message. The company said that I am OK and can come to work. I have found a job.

5.DHCP allocation method

①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 specifically assigned 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. Instead, after each use, the DHCP client releases the IP address for use by other clients.

2. Install DHCP server

1.DHCP server software

  • dhcp-4.2.5-47.el7.centos.x86_64.rpm in the CentOS CD
  • Generally, there is one. If not, install it with RPM.

mark

  • Main files in the DHCP package

Main configuration file: /etc/dhcpd.conf

Execute programs: /usr/sbin/dhcpd, usr/sbin/dhcrelay

2. Main configuration file

  • Contents of dhcpd.conf
ddns-update-style interim; #Global configuration parameters......
subnet 192.168.0.0 netmask 255.255.255.0 { #subnet The above are all global configuration parameters; network segment declaration (network segment, subnet mask, default gateway address)
   option routers 192.168.0.1; #Configuration options......
   default-lease-time 21600; #Configure parameters host ns {
      ......    
      fixed-address 207.175.42.254; #Host declaration (fix an IP for the host)
   }
}        
  • Subnet segment declaration, 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

3. Configuration steps

1. Use DHCP to dynamically assign IP addresses to PCs

① eNSP

The experimental topology diagram is as follows

Note: You can only connect via serial cable after configuring "Cloud".

mark

  • Configure Cloud

mark

  • SW1 basic configuration
<Huawei>undo terminal monitor 
<Huawei>system-view 
[L2-SW1]user-interface console 0
[L2-SW1-ui-console0]idle-timeout 0 0
[L2-SW1-ui-console0]quit 
[L2-SW1]vlan batch 10 20 100
[L2-SW1]interface Ethernet 0/0/1	
[L2-SW1-Ethernet0/0/1]port link-type access 	
[L2-SW1-Ethernet0/0/1]port default vlan 10
[L2-SW1-Ethernet0/0/1]quit
[L2-SW1]interface Ethernet 0/0/2	
[L2-SW1-Ethernet0/0/2]port link-type access 	
[L2-SW1-Ethernet0/0/2]port default vlan 20
[L2-SW1-Ethernet0/0/2]quit
[L2-SW1]interface Ethernet 0/0/3	
[L2-SW1-Ethernet0/0/3]port link-type access 	
[L2-SW1-Ethernet0/0/3]port default vlan 100
[L2-SW1-Ethernet0/0/3]quit
[L2-SW1]interface Ethernet 0/0/4	
[L2-SW1-Ethernet0/0/4]port link-type access 	
[L2-SW1-Ethernet0/0/4]port default vlan 100
[L2-SW1-Ethernet0/0/4]quit
[L2-SW1]interface GigabitEthernet0/0/1
[L2-SW1-GigabitEthernet0/0/1]port link-type trunk
[L2-SW1-GigabitEthernet0/0/1]port trunk allow-pass vlan 10 20 100
[L2-SW1-GigabitEthernet0/0/1]quit
  • SW2 Basic Configuration
<Huawei>undo terminal monitor 
<Huawei>system-view 
[Huawei]sysname L3-SW2
[L3-SW2]user-interface console 0
[L3-SW2-ui-console0]idle-timeout 0 0
[L3-SW2-ui-console0]quit
[L3-SW2]vlan batch 10 20 100
[L3-SW2]interface GigabitEthernet0/0/1
[L3-SW2-GigabitEthernet0/0/1]port link-type trunk
[L3-SW2-GigabitEthernet0/0/1]port trunk allow-pass vlan 10 20 100
[L3-SW2-GigabitEthernet0/0/1]quit
[L3-SW2]interface Vlanif 10	
[L3-SW2-Vlanif10]ip address 192.168.10.1 255.255.255.0	
[SW2-Vlanif20]un sh
[L3-SW2]interface Vlanif 20	
[L3-SW2-Vlanif20]ip address 192.168.20.1 255.255.255.0	
[SW2-Vlanif20]un sh
[L3-SW2]interface Vlanif 100
[L3-SW2-Vlanif100]ip address 192.168.100.1 255.255.255.0	
[SW2-Vlanif20]un sh
[SW2-Vlanif100]int g0/0/1
[SW2-GigabitEthernet0/0/1]port link-type trunk 
[SW2-GigabitEthernet0/0/1]port trunk allow-pass vlan all 
  • Configuring DHCP Relay (Layer 3 Switch)
[L3-SW2]dhcp enable ##Enable DHCP function [L3-SW2]interface Vlanif 10 ##Configure the virtual interface IP, select DHCP relay mode, and request to point to the DHCP server IP
[L3-SW2-Vlanif10]dhcp select relay 	
[L3-SW2-Vlanif10]dhcp relay server-ip 192.168.100.100	
[L3-SW2-Vlanif10]quit 	
[L3-SW2]interface Vlanif 20	
[L3-SW2-Vlanif20]dhcp select relay 
[L3-SW2-Vlanif20]dhcp relay server-ip 192.168.100.100	
[L3-SW2-Vlanif20]quit 	
[L3-SW2]interface Vlanif 100	
[L3-SW2-Vlanif100]dhcp select relay 
[L3-SW2-Vlanif100]dhcp relay server-ip 192.168.100.100
[L3-SW2-Vlanif100]quit 

②Virtual Machine

  • Select the VM network type

mark

  • Configure the network card IP

mark

  • Install the dhcp package and view and find the configuration file
[root@xjj ~]# yum -y install dhcp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package 12: dhcp-4.2.5-58.el7.centos.x86_64 has been installed and is the latest version. No processing is required. [root@xjj ~]# cd /etc/dhcp/ ##The configuration file of dhcp is /etc/dhcp/dhcpd.conf
[root@xjj dhcp]# ls
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
[root@xjj dhcp]# less dhcpd.conf ##Check the content [root@xjj ~]# cd /usr/share/doc/dhcp-4.2.5/ Go to this directory to find the dhcp configuration template [root@xjj dhcp-4.2.5]# ls ##“dhcpd.conf.example”
dhcpd6.conf.example dhcpd.conf.example ldap
[root@xjj dhcp-4.2.5]# cp dhcpd.conf.example /etc/dhcp/dhcpd.conf ##Overwrite the template to /etc/dhcp/dhcpd.confcp: Do you want to overwrite "/etc/dhcp/dhcpd.conf"? y

[root@xjj ~]# cd /etc/dhcp/ ##Return to the /etc/dhcp directory [root@xjj dhcp]# ls
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
[root@xjj dhcp]# less dhcpd.conf ##View the configuration file and proceed to the next step of editing

Edit the dhcp configuration file

  • [root@xjj dhcp]# vim dhcpd.conf

mark

  • Disable the firewall, restart the network card, and then use "ping" to verify the network.
[root@xjj dhcp]# systemctl stop firewalld
se[root@jj dhcp]# setenforce 0
##Remember to restart the network card!
[root@xjj dhcp]# systemctl restart dhcpd


[root@xjj dhcp]# ping 192.168.100.1
...omitted[root@xjj dhcp]# ping 192.168.10.1
...omitted[root@xjj dhcp]# ping 192.168.20.1
...slightly

③Verification

  • Verify it in eNSP
  • First, turn on DHCP on both PCs to automatically obtain IP addresses.

mark

mark

④Enter the command line "ipconfig" to test

  • pc1

mark

  • pc2

mark

pc1 and pc2 succeed! ! !

The above is a super detailed explanation of the details of the Linux DHCP service. For more information about the Linux DHCP service, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to build a DHCP server in Linux
  • Linux starts dhcp server steps
  • DHCP service configuration tutorial under Linux
  • Complete configuration of DHCP service under Linux (with pictures and text)

<<:  Several commonly used single-page application website sharing

>>:  Implementation and optimization of MySql subquery IN

Recommend

Example code for evenly distributing elements using css3 flex layout

This article mainly introduces how to evenly dist...

MySQL database terminal - common operation command codes

Table of contents 1. Add users 2. Change the user...

MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

1: Download from mysql official website https://d...

Detailed explanation of MySQL index principles and optimization

Preface This article was written by a big shot fr...

MySql development of automatic synchronization table structure

Development Pain Points During the development pr...

Vue implements the magnifying glass effect of tab switching

This article example shares the specific code of ...

JavaScript CollectGarbage Function Example

First, let's look at an example of memory rel...

WeChat applet uses canvas to draw clocks

This article shares the specific code of using ca...

MySQL 5.7 deployment and remote access configuration under Linux

Preface: Recently I am going to team up with my p...

JQuery implements hiding and displaying animation effects

This article shares the specific code of JQuery t...

How to add website icon?

The first step is to prepare an icon making softwa...

How to Completely Clean Your Docker Data

Table of contents Prune regularly Mirror Eviction...

Some thoughts and experience sharing on web page (website) design and production

First, before posting! Thanks again to I Want to S...

How to implement vertical text alignment with CSS (Summary)

The default arrangement of text in HTML is horizo...

Solution to Linux server graphics card crash

When the resolution of the login interface is par...