動態主機配置協議(DHCP)詳解

一、 概述

DHCP協議

Dynamic Host Configuration Protocol ,動態主機配置協議

作用:動態的進行IP地址分配

服務端的監聽端口 67/udp

客戶端監聽端口 68/udp

網絡架構 C/S:client/server

DHCP的優勢

  • 提高配置效率

  • 減少配置錯誤

DHCP的分配方式

  • 手動分配:固定不變,工程師進行地址綁定

  • 自動分配:但是不進行地址回收

  • 動態分配:進行地址回收

應用場景

  • 更加準確的配置網絡參數的情況下

  • 網絡環境較大時

注意

同一個網絡環境下不允許存在多個DHCP服務器

工作流程(背會)

1、當客戶端配置為自動獲得IP地址時,客戶端發送discover廣播包(發現),用來尋找網絡中的DHCP服務器

2、假如網絡存在DHCP服務器,此時服務器給出回應,向客戶端發送Offer廣播包(邀約),攜帶了IP地址的信息,詢問客戶端是否使用該IP地址

3、假如客戶端使用上述IP地址,向服務端發送Request廣播包(請求),并將請求信息寫入到該包內。

4、服務端向客戶端發送Ack廣播包(確認),并確定IP地址的租約期。

何時更新租約

當租約期達到50%時

當客戶端重啟后

客戶端直接發送Request包:

A、IP地址空閑 服務端直接回應Ack

B、IP地址被占用 服務端回應noAck 客戶端需要將上述“工作流程”完整執行一遍

客戶端類型

Linux DHCP服務器不存在,沒有IP

Windows DHCP不存在,會啟用備用IP地址 169.254.0.0/16 ~ 168.254.255.255/16

二、DCHP安裝與配置

部署

基礎環境

配置yum源

關閉防火墻及SElinux

[root@dhcpserver ~]# systemctl  stop  firewalld  && systemctl disable firewalld

查看SElinux狀態

[root@dhcpserver ~]# getenforce
###設置寬容模式
[root@dhcpserver ~]# setenforce  0
###關閉SElinux,重啟才能生效
[root@dhcpserver ~]# vim  /etc/selinux/config
SELINUX=enforcing  改為 SELINUX=disabled

配置靜態IP

###關閉網絡圖形化工具
[root@dhcpserver ~]# systemctl  stop NetworkManager ; systemctl disable NetworkManager
[root@dhcpserver ~]# cd /etc/sysconfig/network-scripts/
[root@dhcpserver ~]# cp ifcfg-ens33 ifcfg-ens34
[root@dhcpserver ~]# cat ifcfg-ens34
TYPE=Ethernet
BOOTPROTO=static
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.100.254
PREFIX=24
[root@dhcpserver ~]# systemctl restart network 
安裝DHCP軟件包

[root@localhost ~]# yum install -y dhcp-server

配置

配置文件存儲路徑 /etc/dhcp

默認配置文件副本路徑 /usr/share/doc/dhcp-4.2.5/

數據文件存儲路徑 /var/lib/dhcpd

核心配置文件 /etc/dhcp/dhcpd.conf


[root@dhcpserver ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/
[root@dhcpserver ~]# cd /etc/dhcp/
[root@dhcpserver ~]# cp dhcpd.conf.example dhcpd.conf
[root@dhcpserver ~]# cat dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
## option definitions common to all supported networks...
option domain-name "example.org";    ##指定DNS服務器域名
option domain-name-servers ns1.example.org, ns2.example.org;  ##指定DNS服務器域名default-lease-time 600;  ##默認租約。單位s
max-lease-time 7200; ##最大租約時間,單位s# 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;  ##日志輸出通道,交給syslog服務管理# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.
#######每一個subnet都是一個分配地址段的定義######################
subnet 10.152.187.0 netmask 255.255.255.0 {
}# 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都是進行地址綁定的配置項###############
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;  ###固定分配地址的主機的MAC地址fixed-address fantasia.fugue.com;  ###需要進行分配的IP地址
}# 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@dhcpserver dhcp]# cat dhcpd.conf | grep -v "^#" | grep -v "^$"
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 192.168.100.0 netmask 255.255.255.0 {range 192.168.100.100 192.168.100.200;option domain-name-servers ns1.internal.example.org;option domain-name "internal.example.org";option routers 192.168.100.254;option broadcast-address 192.168.100.255;default-lease-time 600;max-lease-time 7200;
}
host passacaglia {hardware ethernet 0:0:c0:5d:bd:95;filename "vmunix.passacaglia";server-name "toccata.fugue.com";
}
host fantasia {hardware ethernet 08:00:07:26:c0:a5;fixed-address fantasia.fugue.com;
}
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;}
}
###重啟DHCP服務器
[root@dhcpserver dhcp]# systemctl restart dhcpd
###查看監聽
[root@dhcpserver dhcp]# netstat -anptu | grep :67
udp        0      0 0.0.0.0:67              0.0.0.0:*                           28005/dhcpd 
地址綁定配置文件

[root@dhcpserver dhcp]# cat dhcpd.conf | grep -v "^#" | grep -v "^$"
....省略.....
host s1 {hardware ethernet 00:0c:29:dd:24:41;fixed-address 192.168.100.110;
}
....省略.....
###重啟DHCP服務器
[root@dhcpserver dhcp]# systemctl restart dhcpd
###查看監聽
[root@dhcpserver dhcp]# netstat -anptu | grep :67
udp        0      0 0.0.0.0:67              0.0.0.0:*                           28005/dhcpd 
####客戶端驗證
[root@client ~]# ifdown ens34 ; ifup ens34
[root@client ~]# ip a
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:dd:24:41 brd ff:ff:ff:ff:ff:ffinet 192.168.100.110/24 brd 192.168.100.255 scope global noprefixroute dynamic ens34valid_lft 599sec preferred_lft 599secinet6 fe80::20c:29ff:fedd:2441/64 scope link valid_lft forever preferred_lft forever
多地址池配置
路由器配置
###安裝dhcp軟件,提供dhcrelay命令
[root@nginx1 ~]# yum install -y dhcp
##開啟路由功能
[root@nginx1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@nginx1 ~]# sysctl -p
net.ipv4.ip_forward = 1
##分別對連接兩個網絡的網卡配置IP地址
[root@nginx1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens34 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.100.253
PREFIX=24
[root@nginx1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens37
TYPE=Ethernet
BOOTPROTO=static
NAME=ens37
DEVICE=ens37
ONBOOT=yes
IPADDR=192.168.200.253
PREFIX=24
##使用dhcrelay進行DHCP廣播的中繼轉發
[root@nginx1 ~]# dhcrelay 192.168.100.254  
DHCP服務器配置

##DHCP分配地址配置文件,添加如下配置:
[root@dhcpserver dhcp]# vim dhcpd.conf
....省略.....
subnet 192.168.200.0 netmask 255.255.255.0 {
range 192.168.200.100 192.168.200.200;
option domain-name-servers ns1.internal.example.org;
option domain-name "internal.example.org";
option routers 192.168.200.253;
option broadcast-address 192.168.200.255;
default-lease-time 600;
max-lease-time 7200;} 
....省略.....
##重啟DHCP服務器
[root@dhcpserver ~]# systemctl restart dhcpd
##設置DHCP服務器的網關
[root@dhcpserver ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens34 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.100.254
PREFIX=24
GATEWAY=192.168.100.253
##驗證網關
[root@dhcpserver ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.253 0.0.0.0         UG    103    0        0 ens34
客戶端驗證
[root@nginx2 ~]# ifdown ens34 ;ifup ens34
[root@nginx2 ~]# ifconfig ens34
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.200.100  netmask 255.255.255.0  broadcast 192.168.200.255inet6 fe80::20c:29ff:fe8a:4a83  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:8a:4a:83  txqueuelen 1000  (Ethernet)RX packets 40  bytes 9956 (9.7 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 172  bytes 27844 (27.1 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

三、常見故障

1. 服務啟動異常,排查配置文件,特別注意修改的位置!!!
2. 地址參數分配異常,排查網絡連接情況,再排查subnet字段!!!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/923547.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/923547.shtml
英文地址,請注明出處:http://en.pswp.cn/news/923547.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

單變量單步時序預測 | TCN-LSTM時間卷積結合長短期記憶神經網絡(MATLAB)

? 一、主要功能 該代碼實現了一個結合時序卷積網絡(TCN)和長短期記憶網絡(LSTM)的混合深度學習模型,用于時間序列預測。具體任務是:利用前24個時間步的數據(輸入特征維度為24),來預測下一個時間步的值(輸出維度為1),屬于單變量時間序列滾動預測。 ? 二、算法步驟…

【智能體】rStar2-Agent

rStar2-Agent 是一篇在大模型推理領域極具洞察力和工程實力的工作&#xff0c;它沒有追求參數規模的堆砌&#xff0c;而是通過精巧的算法設計和系統優化&#xff0c;在一個14B的小模型上實現了媲美671B大模型的數學推理能力。 核心思想非常明確&#xff1a;讓模型“想得更聰明”…

Coze源碼分析-資源庫-創建知識庫-后端源碼-核心技術與總結

11. 核心技術特點 11.1 知識庫創建的分層架構設計 清晰的職責分離&#xff1a; API層&#xff08;knowledge_service.go&#xff09;&#xff1a;負責知識庫創建請求處理、參數驗證、響應格式化應用層&#xff08;knowledge.go&#xff09;&#xff1a;負責知識庫創建業務邏輯編…

Nano Banana制作3D立體打印效果圖

Nano Banana介紹Nano Banana 是 Google 于 2024 年推出的革命性 AI 驅動圖像生成與編輯模型&#xff0c;正式名稱為 Gemini 2.5 Flash Image。以下是對它的詳細介紹&#xff1a;技術背景&#xff1a;Nano Banana 基于 Google DeepMind 最新的 Gemini 2.5 Flash Image 架構&…

繼續吐槽Rstudio

前言 繼上次《怪談級別疑難問題收錄》后&#xff0c;怪談級別的疑難問題又更新了&#xff0c;這次更新了三個讓人吐血的奇葩問題&#xff0c;其中就包括大家又愛又恨的Rstudio&#xff0c;一起圍觀下。 本教程基于Linux環境演示&#xff0c;計算資源不足的同學可參考&#xf…

C++:string模擬實現中的賦值拷貝函數現代寫法詭異地崩掉了......

事情是這樣的&#xff1a;博主今天回看以前實現過的string&#xff0c;當時就遇到了一個bug:可見博主當時的破防。因為最近在集中復盤C初階部分&#xff0c;就有點好奇年輕的時候自己寫的模擬string是什么樣。沒想到給我自己留了個bug。現在來細看這個場景&#xff1a;為了測試…

機器學習-Bagging

Bagging-Bootstrap AGGrgratING Bagging并行訓練n個基本學習器&#xff08;base learner&#xff09;通過平均所有學習器的輸出&#xff08;回歸&#xff09;或主投票&#xff08;分類&#xff09;做決策每個模型是用在訓練集上通過bootstrap采樣得到的新的數據集進行訓練得到的…

Unity3D Shader 入門知識

Unity3D Shader 入門知識詳解。 Unity3D Shader 入門知識 Shader&#xff08;著色器&#xff09;對很多 Unity 初學者來說像是“黑魔法”。 實際上&#xff0c;Shader 并沒有那么神秘&#xff0c;它本質上就是一段運行在 GPU 上的小程序&#xff0c;用來控制 屏幕上每個像素的顏…

【面試之Redis篇】主從復制原理

從面試的角度來解釋 Redis 主從復制原理&#xff0c;按照“總-分-總”的結構&#xff0c;清晰地闡述其核心概念、工作流程和關鍵要點&#xff0c;這能體現出你不僅知道是什么&#xff0c;還理解為什么以及如何應對相關問題。總覽&#xff1a;一句話定義 面試官您好&#xff0c;…

數據庫開啟ssl

數據庫&#xff1a;阿里云rds 系統&#xff1a;centos 需要修改的&#xff1a;nacos連接項目連接本地navicat連接 重點&#xff1a;為了兼容本地和服務器&#xff0c;ssl證書路徑由原來的絕對路徑換成環境變量參數&#xff0c;所以有步驟4 文章目錄步驟1 阿里云步驟2 navicat…

Redis 事件驅動與多路復用源碼剖析

Redis 事件驅動與多路復用源碼剖析1. 前言 Redis 是 單線程 I/O 多路復用 的典型代表。 它并不是多線程處理請求&#xff0c;而是依賴 事件驅動&#xff08;event-driven&#xff09;模型&#xff0c;在一個線程內高效管理海量連接。 核心組件&#xff1a; ae.c&#xff1a;事…

VR煤礦實訓系統相較于傳統煤礦培訓方式的獨特優勢?-廣州華銳互動

高度逼真&#xff0c;沉浸體驗?VR煤礦實訓系統運用先進的3D建模、動態仿真技術&#xff0c;對煤礦井下的復雜環境進行1:1還原。從幽深的巷道、運轉的采煤設備&#xff0c;到潮濕的空氣、昏暗的燈光&#xff0c;甚至細微的煤塵顆粒&#xff0c;都能逼真呈現。使用者戴上VR設備后…

javaweb XML DOM4J

XMLXML作用就是配置文件&#xff0c;properties使用不了較復雜的需求&#xff0c;xml應運而生配置文件對比 xml更方便tips1:新建resources目錄&#xff0c;并將src中的jdbc.properties移到resourcs中&#xff0c;并且右鍵標記為源代碼根目錄&#xff0c;這樣運行src時就會和pro…

多模態視頻理解領域 Benchmark 與 Leaderboard 整理

多模態視頻理解是當前人工智能領域的研究熱點&#xff0c;其核心目標是讓模型像人類一樣&#xff0c;綜合視頻中的視覺、聽覺&#xff08;部分場景&#xff09;及文本信息&#xff0c;實現對視頻內容的深度感知、理解與推理。為客觀評估模型性能&#xff0c;行業內涌現了眾多權…

18j621-3通風天窗圖集pdf(免費高清版)

18j621-3通風天窗已經替代05j621-3通風天窗圖集成為目前比較通用的建筑屋頂通風選型重要參考標準&#xff0c;18j621-3圖集是對前圖集的優化和革新&#xff0c;在18j621-3圖集中新增了TC8圓拱型電動采光天窗&#xff0c;豐富了屋面通風排煙設備的選型。在18j621-3天窗圖集中&am…

LawGPT:基于中文法律知識的大模型

本文轉載自&#xff1a;https://www.hello123.com/lawgpt ** 一、?? LawGPT&#xff1a;中文法律界的 “AI 法助”&#xff0c;啃透了 15 萬份判決書&#xff01; LawGPT 是一系列專攻中文法律知識的開源大模型&#xff0c;在通用中文基座&#xff08;如 ChatGLM&#xff0…

用 go-commons 快速寫一個監控 CPU/內存的 Exporter

歡迎加入開源項目&#xff0c;提你的 mr Go Commons&#xff1a;Golang 開發者的常用工具集&#xff0c;一站式解決常見需求 在 Go 語言的開發過程中&#xff0c;你是不是經常遇到這樣的情況&#xff1a; 想要做點小功能&#xff0c;卻得從零寫起&#xff0c;或者到處找三方…

KingbaseES客戶端工具Ksql使用全指南:從安裝到高級操作

引言 在國產數據庫蓬勃發展的今天&#xff0c;KingbaseES憑借其自主可控、高性能、高可用的特性&#xff0c;已成為政務、金融、能源等關鍵領域的首選數據庫。而作為其配套的命令行工具&#xff0c;Ksql更是DBA和開發人員的“瑞士軍刀”——它不僅能高效執行SQL查詢&#xff0c…

【LeetCode - 每日1題】可以輸入的最大單詞數

?? 個人主頁:(時光煮雨) ?? 高質量專欄:vulnhub靶機滲透測試 ?? 希望得到您的訂閱和支持~ ?? 創作高質量博文(平均質量分95+),分享更多關于網絡安全、Python領域的優質內容!(希望得到您的關注~) ??目錄?? 難度 ?? 題目回顧 ?解題思路 ??概述 ??核心…

3227. 字符串元音游戲

3227. 字符串元音游戲 題目鏈接&#xff1a;3227. 字符串元音游戲 代碼如下&#xff1a; class Solution { public:bool doesAliceWin(string s) {return ranges::any_of(s, [](char c) {return c a || c e || c i || c o || c u;});} };