Linux--DHCP Principle and Configuration

1. How DHCP works

1.1. Understand the DHCP service

DHCP (Dynamic Host Configuration Protocol, dynamic host configuration protocol) is designed and developed by the Internet Task Force, and is specifically used to automatically allocate TCP/IP parameters to computers in a TCP/IP network. The 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 simple TCP/IP network settings, reducing the burden of configuring IP addresses.
Insert picture description here

1.2. Benefits of using DHCP

Reduce the workload of the administrator.

Avoid the possibility of input errors.

Avoid IP address conflicts.

When the network changes the IP address range, there is no need to reconfigure each user's IP address.

Improved the utilization of IP addresses.

Convenient for client configuration.

1.3, DHCP allocation method

In the working principle of DHCP, the DHCP server provides three IP address allocation methods: automatic allocation
(Automatic Allocation), manual allocation and dynamic allocation (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 .

1.4, DHCP lease process

The process by which the client obtains the IP address from the DHCP server is called the DHCP lease process. This process is mainly divided into the following four steps.
The client searches for a server in the network; the
server responds to the client with a service; the
client sends a service request to the server; the
server provides services to the client.

Insert picture description here
(1) The client requests an IP address.
When a DHCP client starts, the client does not yet have an IP address, so the client must obtain a legal address through DHCP. At this time, the DHCP client broadcasts the DHCP Discover information to find the DHCP server.
Insert picture description here
(2) The server responds.
When the DHCP server receives the information from the client requesting an IP address, it searches its own IP address pool to see if it is legal The IP address is provided to the client. If so, the DHCP server will mark the IP address and add it to the DHCP Offer message, and then the DHCP server will broadcast a DHCP Offer message.
Insert picture description here
(3) The client selects the IP address. The
DHCP client extracts the IP address from the first DHCP Offer message received, and the DHCP server that sends the IP address reserves the address so that the address cannot be allocated to another DHCP client . Then the DHCP client will send a DHCP Request message to the server.
Insert picture description here
(4) The server confirms 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 the valid lease of the IP address and other configurable information. When the client receives the DHCP ACK message, it configures the IP address and completes the initialization of TCP/IP.
Insert picture description here
(5) Re-login to the
DHCP client every time it re-logs in to the network, it does not need to send DHCP Discover information, but directly sends the DHCP Request request information containing the IP address assigned the previous time.
Insert picture description here
(6) Renew the lease
When the lease period of 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 that provides the lease, requesting to renew the existing address lease.

2. Use DHCP to dynamically configure the host address

Use DHCP service to automatically assign addresses to a large number of clients and provide centralized management

Reduce management and maintenance costs and improve network configuration efficiency.

The address information that the DHCP service can assign mainly includes:

The IP address and subnet mask of the network card; the
corresponding network address and broadcast address; the
default gateway address; the
DNS server address;

2.1. Configure DHCP server

2.1.1. Install DHCP server software


Dhcp...rpm in the centos mirror of the DHCP server software

The main files of the dhcp software package
Main configuration file: /etc/dhcpd.conf
Execution program: /usr/sbin/dhcpd, /usr/sbin/dhcrelay
Execution parameter configuration: /etc/sysconfig/dhcpd

2.1.2, establish the main configuration file

(1) Configuration composition of the /etc/dhcp/dhcpd.conf file

Insert picture description here
Statement: It is used to describe the division of network layout in dhcpd server, which is the logical scope of network settings. The more common declarations are subnet and host. The subnet declaration is used to constrain a network segment, and the host declaration is used to constrain a specific host.

Parameters: Consists of configuration keywords and corresponding values, always ending with ";" (semicolon), generally within the specified declaration range, used to set the operating characteristics of the range (such as default lease time, maximum lease time Wait).

Options: Guided by "option", followed by specific configuration keywords and corresponding values, also ending with ";", used to specify various address parameters assigned to the client (such as default gateway address, subnet mask, DNS server address, etc.).
(2) Global configuration of dhcpd service

Common configuration options and parameters meaning
ddns-update-style Dynamic DNS update mode
default-lease-time Default lease time
max-lease-time Maximum lease time
option domain-name Default search area
option domain-name-servers DNS server address

(3) Subnet segment statement, which affects the entire subnet segment

range parameter: set the IP address pool for allocation

option subnet-mask parameter: Set the subnet mask of the client

option routers parameter: Set the default gateway address of the client

(4) Host host statement,
used for a single host host statement is used to set the network properties of a single host, usually used to assign fixed IP addresses (reserved addresses) to network printers or individual servers. The common feature of these hosts is that they require each access The IP addresses are the same to ensure the stability of the service.

hardware ethernet parameter: specify the MAC address of the corresponding host

fixed-address parameter: specify the IP address reserved for the host

2.1.3, start DCHP service

Start service

[root@localhost ~]# systemctl start dhcpd
[root@localhost ~]# netstat -anpu | grep ":67"
udp	0	0 0.0.0.0:67	0.0.0.0:*	5684/dhcpd

View the lease file /var/lib/dhcpd/dhcpd.lease

[root@localhost ~]# less /var/lib/dhcpd/dhcpd.leases 

2.2. Use DHCP client

(1) Edit the configuration file of the corresponding network card

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
ONBOOT=yes 
BOOTPROTO=dhcp
[root@localhost ~]# ifdown ens33 ; ifup ens33

Where BOOTPROTO=dhcp
(2) Use dhclient

命令基本格式
dhclient [-d] [网络接口名]
例如
[root@localhost ~]# dhclient -d ens33

2.3, DHCP relay

2.3.1 Principle of DHCP Relay

When the internal network of the enterprise is relatively large, the network is usually planned into multiple different subnets through VLAN. But in this case, the next DHCP server cannot provide services for clients on different network segments at the same time, because the DHCP protocol uses broadcast, and VLAN can isolate broadcasts.

For example, if the
DHCP server is in VLAN 100, only clients in VLAN 100 can obtain an IP address from the DHCP server. If the client of VLAN 2 or VLAN 3 cannot obtain an IP address through this DHCP server.

2.3.2, DHCP relay configuration

DHCP relay configuration commands

命令基本格式
ip helper-address HDCPsrv-IPAddress
(HDCPsrv-IPAddress为DHCP服务器的IP地址)

Guess you like

Origin blog.csdn.net/weixin_48191211/article/details/109187186