Implementation principle of DHCP
DHCP definition
DHCP Dynamic Host Configuration Protocol (DHCP) is a local area network protocol, which uses UDP protocol to work. It is a popular Client/Server protocol, which is generally used to specify relevant configuration information for hosts or routers. DHCP service has been widely used in enterprises and families. It can automatically assign ip addresses and other information The whole process is transparent to customers.
DHCP allocation method
Automatic Allocation: the DHCP server assigns a permanent IP address to the host. Once the DHCP client successfully leases the IP address from the DHCP server for the first time, it can permanently use the address.
In Dynamic Allocation, the DHCP server assigns an IP address with time limit to the host. When the time expires or the host explicitly gives up the address, the address can be used by other hosts.
In Manual Allocation, the IP address of the client is specified by the network administrator, and the DHCP server only tells the specified IP address to the client host.
DHCP working process
When the DHCP client starts, it searches for the existence of a DHCP server in the network. If found, send a request to the DHCP server. After receiving the request, the DHCP server selects TCP/IP configuration parameters for the DHCP client and sends these parameters to the client. If the conflict detection setting is configured, the DHCP server uses Ping to test the connectivity of each available address in the scope before providing the address in the lease to the client. This ensures that each IP address provided to the customer is not used by another non DHCP computer using manual TCP/IP configuration.
DHCP works differently depending on whether the client logs in to the network for the first time.
Initial login
DHCP work includes four steps when logging in for the first time
1. Find DHCP server
When the DHCP client logs in to the network for the first time, the computer finds that there is no IP address set on the machine. It will broadcast the DHCP discover y information to find the DHCP server, that is, to 255.255 255.255 send specific broadcast information. Every host installed with TCP/IP protocol on the network will receive this broadcast information, but only the DHCP server will respond.
2. Assign IP address
The DHCP server that receives the DHCP discover discovery information in the network will respond. It selects one from the unassigned IP address pool and assigns it to the DHCP client, and sends a DHCP offer containing the assigned IP address and other settings to the DHCP client to provide information.
3. Accept IP address
After receiving the DHCP offer information, the DHCP client selects the first received offer information, and then answers a DHCP request information in the form of broadcast. The information includes the content of requesting an IP address from the selected DHCP server.
4. IP address assignment confirmation
When the DHCP server receives the DHCP request information answered by the DHCP client, it sends a DHCP ack confirmation information including the IP address and other settings it provides to the DHCP client to tell the DHCP client that the IP address it provides can be used. Then, the DHCP client will bind its TCP/IP protocol to the network card. In addition, except for the DHCP server selected by the DHCP client, other DHCP servers will recover the IP address once provided.
Login again
In the future, each time the DHCP client logs in to the network again, it does not need to send the DHCP discover discovery information, but directly send the DHCP request information containing the IP address assigned last time. When the DHCP server receives this message, it will try to let the DHCP client continue to use the original IP address and answer a DHCP ack confirmation message. If this IP address can no longer be allocated to the original DHCP client, the DHCP server will answer a DHCP nack denial message to the DHCP client. When the original DHCP client receives the DHCP nack denial information, it must resend the DHCP discover discovery information to request a new IP address.
1. If the IP address in the client DHCP request is not used on the server, the DHCP server replies to the DHCP ACK and continues to use the IP.
2. If the IP address in the client DHCP request has been used on the server, the DHCP server replies DHCP NACK to tell the client that the IP has been used.
3. After replying to NACK, restart the process when DHCP logs in for the first time
relocatio
The IP address leased by the DHCP server to the DHCP client generally has a lease term. After the lease term expires, the DHCP server will take back the leased IP address. If a DHCP client wants to extend its IP lease, it must update its IP lease. When the DHCP client starts and the IP lease term reaches 50% of the lease, the DHCP client will automatically send the DHCP server the information to update its IP lease.
Install DHCP package on CentOS7
[root@jhr-hub ~]# yum -y install dhcp [root@jhr-hub ~]# cat /etc/dhcp/dhcpd dhcpd6.conf dhcpd.conf [root@jhr-hub ~]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page # [root@jhr-hub ~]# [root@jhr-hub ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf cp: overwrite '/etc/dhcp/dhcpd.conf'? y [root@jhr-hub ~]#
Modify DHCP configuration file
[root@jhr-hub ~]# vim /etc/dhcp/dhcpd.conf [root@jhr-hub ~]# [root@jhr-hub ~]# [root@jhr-hub ~]# [root@jhr-hub ~]# 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 3.7.191.1; #Set the validity period of the current IP address in seconds default-lease-time 60; #Set the maximum validity period of the application max-lease-time 60; # Use this to enble / 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. #Declare IP address segment and subnet mask subnet 192.168.1.0 netmask 255.255.255.0 { #Address pool: set an address segment range 192.168.1.100 192.168.1.200; #Specify gateway option routers 3.7.191.1; #Get DNS option domain-name-servers 192.168.1.1; } # This is a very basic subnet declaration. subnet 10.254.239.0 netmask 255.255.255.224 { range 10.254.239.10 10.254.239.20; option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org; } # This declaration allows BOOTP clients to get dynamic addresses, # which we don't really recommend. subnet 10.254.239.32 netmask 255.255.255.224 { range dynamic-bootp 10.254.239.40 10.254.239.60; option broadcast-address 10.254.239.31; option routers rtr-239-32-1.example.org; } # A slightly different configuration for an internal subnet. subnet 10.5.5.0 netmask 255.255.255.224 { range 10.5.5.26 10.5.5.30; option domain-name-servers ns1.internal.example.org; option domain-name "internal.example.org"; option routers 10.5.5.1; option broadcast-address 10.5.5.31; default-lease-time 600; max-lease-time 7200; } # Hosts which require special configuration options can be listed in # host statements. If no address is specified, the address will be # allocated dynamically (if possible), but the host-specific information # will still come from the host declaration. host passacaglia { hardware ethernet 0:0:c0:5d:bd:95; filename "vmunix.passacaglia"; server-name "toccata.fugue.com"; } # Fixed IP addresses can also be specified for hosts. These addresses # should not also be listed as being available for dynamic assignment. # Hosts for which fixed IP addresses have been specified can boot using # BOOTP or DHCP. Hosts for which no fixed address is specified can only # be booted with DHCP, unless there is an address range on the subnet # to which a BOOTP client is connected which has the dynamic-bootp flag # set. host fantasia { hardware ethernet 08:00:07:26:c0:a5; fixed-address fantasia.fugue.com; } # You can declare a class of clients and then do address allocation # based on that. The example below shows a case where all clients # in a certain class get addresses on the 10.17.224/24 subnet, and all # other clients get addresses on the 10.0.29/24 subnet. class "foo" { match if substring (option vendor-class-identifier, 0, 4) = "SUNW"; } shared-network 224-29 { subnet 10.17.224.0 netmask 255.255.255.0 { option routers rtr-224.example.org; } subnet 10.0.29.0 netmask 255.255.255.0 { option routers rtr-29.example.org; } pool { allow members of "foo"; range 10.17.224.10 10.17.224.250; } pool { deny members of "foo"; range 10.0.29.10 10.0.29.230; } } [root@jhr-hub ~]#
Start service
[root@jhr-hub ~]# systemctl start dhcpd [root@jhr-hub ~]# systemctl status dhcpd ● dhcpd.service - DHCPv4 Server Daemon Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2021-12-09 11:25:37 CST; 2s ago Docs: man:dhcpd(8) man:dhcpd.conf(5) Main PID: 142669 (dhcpd) Status: "Dispatching packets..." Memory: 5.0M CGroup: /system.slice/dhcpd.service └─142669 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
Linux operation and maintenance Communication Community
Linux operation and maintenance exchange community, Internet news and technology exchange.
71 original content
official account
https://blog.csdn.net/qq_3392...
https://my.oschina.net/u/3981543
https://www.zhihu.com/people/...
https://segmentfault.com/u/hp...
https://juejin.cn/user/331578...
https://space.bilibili.com/35...
https://cloud.tencent.com/dev...
Zhihu, CSDN, open source China, Sifu, Nuggets, BiliBili, Tencent cloud
This article uses Article synchronization assistant synchronization