目錄
- 一、DHCP服務(wù)(動(dòng)態(tài)主機(jī)配置協(xié)議)
- 1.背景
- 2.概述
- 3.優(yōu)點(diǎn)
- 4.DHCP報(bào)文類(lèi)型
- 5.DHCP 的分配方式
- 二、安裝 DHCP 服務(wù)器
- 三、配置步驟
- 1.使用 DHCP 動(dòng)態(tài)的給 PC 機(jī)分配 IP 地址
- ① eNSP
- ②虛擬機(jī)
- ③驗(yàn)證
- ④進(jìn)入命令行“ipconfig”測(cè)試
一、DHCP服務(wù)(動(dòng)態(tài)主機(jī)配置協(xié)議)
1.背景
1.手動(dòng)設(shè)置工作量大且容易沖突
2.用DHCP可以減少工作量和避免地址沖突
2.概述
- 作用:為局域網(wǎng)內(nèi)的電腦分配IP地址
- 架構(gòu) C/S;客戶(hù)端/服務(wù)器模式
- 實(shí)現(xiàn)方式:
1.為相同網(wǎng)段的電腦分配IP地址—DHCP
2.跨網(wǎng)段為另一網(wǎng)段的電腦分配IP地址—DHCP中繼
- DHCP使用UDP協(xié)議,67號(hào)是客戶(hù)機(jī)使用的,68號(hào)是服務(wù)器端的端口號(hào)
3.優(yōu)點(diǎn)
1.減少管理員的工作量
2.避免輸入錯(cuò)誤的可能
3.避免IP地址沖突
4.當(dāng)更改IP地址段時(shí),不需要重新配置每個(gè)用戶(hù)的IP地址
5.提高了IP地址的利用率
6.方便客戶(hù)端的配置
4.DHCP報(bào)文類(lèi)型
以找工作為例:
DHCP Discover——客戶(hù)端用來(lái)尋找DHCP服務(wù)器的報(bào)文
我向公司投簡(jiǎn)歷來(lái)找工作
DHCP Offer——DHCP服務(wù)器用來(lái)響應(yīng)DHCP Discover報(bào)文的報(bào)文。該報(bào)文攜帶了各種配置信息,例如IP地址,DNS等
公司收到我的求職簡(jiǎn)歷后給我發(fā)了一份offer
DHCP Request——客戶(hù)端請(qǐng)求配置確認(rèn),或者續(xù)借租期
我收到offer之后跟公司確認(rèn)
DHCP ACK——服務(wù)器對(duì)Request報(bào)文的確認(rèn)響應(yīng)
公司說(shuō)我ok沒(méi)問(wèn)題,可以過(guò)來(lái)上班 ,我找到工作了
5.DHCP 的分配方式
①自動(dòng)分配:
當(dāng)DHCP客戶(hù)機(jī)第一次成功地從DHCP服務(wù)器獲取到一個(gè)IP地址后,就永久的使用這個(gè)IP地址
②手動(dòng)分配:
由DHCP服務(wù)器管理員專(zhuān)門(mén)指定IP地址
③動(dòng)態(tài)分配:
當(dāng)DHCP客戶(hù)機(jī)第一次從DHCP服務(wù)器獲得IP地址后,并非永久的使用該地址,而是在每次使用完后,DHCP客戶(hù)機(jī)就會(huì)釋放這個(gè)IP地址,供其他客戶(hù)機(jī)使用
二、安裝 DHCP 服務(wù)器
1.DHCP 服務(wù)軟件
- CentOS光盤(pán)中的 dhcp-4.2.5-47.el7.centos.x86_64.rpm
- 一般都有,沒(méi)有的話(huà) RPM 去安裝一下

主配置文件:/etc/dhcpd.conf
執(zhí)行程序:/usr/sbin/dhcpd、usr/sbin/dhcrelay
2.主配置文件
- dhcpd.conf 的內(nèi)容構(gòu)成
ddns-update-style interim; #全局配置參數(shù)
......
subnet 192.168.0.0 netmask 255.255.255.0 { #subnet以上的都是全局配置參數(shù);網(wǎng)段聲明(網(wǎng)段、子網(wǎng)掩碼、默認(rèn)網(wǎng)關(guān)地址)
option routers 192.168.0.1; #配置選項(xiàng)
......
default-lease-time 21600; #配置參數(shù)
host ns {
......
fixed-address 207.175.42.254; #主機(jī)聲明(固定一個(gè)IP給主機(jī)用的)
}
}
- Subnet網(wǎng)段聲明,作用于整個(gè)子網(wǎng)段
range參數(shù):設(shè)置用于分配的IP地址池
option subnet-mask參數(shù):設(shè)置客戶(hù)機(jī)的子網(wǎng)掩碼
option routers參數(shù):設(shè)置客戶(hù)機(jī)的默認(rèn)網(wǎng)關(guān)地址
三、配置步驟
1.使用 DHCP 動(dòng)態(tài)的給 PC 機(jī)分配 IP 地址
① eNSP
實(shí)驗(yàn)拓?fù)鋱D如下
注:配置完“Cloud”之后才能用串口線(xiàn)連接


<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
<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
[L3-SW2]dhcp enable ##開(kāi)啟DHCP功能
[L3-SW2]interface Vlanif 10 ##配置虛接口ip,選擇dhcp中繼模式,請(qǐng)求指向dhcp服務(wù)器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
②虛擬機(jī)
- 選擇虛擬機(jī)網(wǎng)絡(luò)類(lèi)型


[root@xjj ~]# yum -y install dhcp
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
軟件包 12:dhcp-4.2.5-58.el7.centos.x86_64 已安裝并且是最新版本
無(wú)須任何處理
[root@xjj ~]# cd /etc/dhcp/ ##dhcp的配置文件是/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 ##查看一下內(nèi)容
[root@xjj ~]# cd /usr/share/doc/dhcp-4.2.5/ 到該目錄下去尋找dhcp配置模板
[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 ##將模板覆蓋到/etc/dhcp/dhcpd.conf中
cp:是否覆蓋"/etc/dhcp/dhcpd.conf"? y
[root@xjj ~]# cd /etc/dhcp/ ##回到/etc/dhcp目錄下
[root@xjj dhcp]# ls
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
[root@xjj dhcp]# less dhcpd.conf ##查看配置文件并進(jìn)行下一步編輯
編輯dhcp配置文件
- [root@xjj dhcp]# vim dhcpd.conf

- 關(guān)閉防火墻,重啟網(wǎng)卡,再去“ping”進(jìn)行驗(yàn)證網(wǎng)絡(luò)
[root@xjj dhcp]# systemctl stop firewalld
se[root@jj dhcp]# setenforce 0
##記得重啟一下網(wǎng)卡!
[root@xjj dhcp]# systemctl restart dhcpd
[root@xjj dhcp]# ping 192.168.100.1
...略
[root@xjj dhcp]# ping 192.168.10.1
...略
[root@xjj dhcp]# ping 192.168.20.1
...略
③驗(yàn)證
- 到eNSP中驗(yàn)證一下
- 首先開(kāi)啟兩臺(tái)PC的DHCP來(lái)自動(dòng)獲取一下IP地址


④進(jìn)入命令行“ipconfig”測(cè)試


pc1和pc2成功!??!
以上就是超詳細(xì)講解Linux DHCP服務(wù)的詳細(xì)內(nèi)容,更多關(guān)于Linux DHCP服務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!