目錄
- 一、DHCP服務(wù)(動態(tài)主機(jī)配置協(xié)議)
- 1.背景
- 2.概述
- 3.優(yōu)點
- 4.DHCP報文類型
- 5.DHCP 的分配方式
- 二、安裝 DHCP 服務(wù)器
- 三、配置步驟
- 1.使用 DHCP 動態(tài)的給 PC 機(jī)分配 IP 地址
- ① eNSP
- ②虛擬機(jī)
- ③驗證
- ④進(jìn)入命令行“ipconfig”測試
一、DHCP服務(wù)(動態(tài)主機(jī)配置協(xié)議)
1.背景
1.手動設(shè)置工作量大且容易沖突
2.用DHCP可以減少工作量和避免地址沖突
2.概述
- 作用:為局域網(wǎng)內(nèi)的電腦分配IP地址
- 架構(gòu) C/S;客戶端/服務(wù)器模式
- 實現(xiàn)方式:
1.為相同網(wǎng)段的電腦分配IP地址—DHCP
2.跨網(wǎng)段為另一網(wǎng)段的電腦分配IP地址—DHCP中繼
- DHCP使用UDP協(xié)議,67號是客戶機(jī)使用的,68號是服務(wù)器端的端口號
3.優(yōu)點
1.減少管理員的工作量
2.避免輸入錯誤的可能
3.避免IP地址沖突
4.當(dāng)更改IP地址段時,不需要重新配置每個用戶的IP地址
5.提高了IP地址的利用率
6.方便客戶端的配置
4.DHCP報文類型
以找工作為例:
DHCP Discover——客戶端用來尋找DHCP服務(wù)器的報文
我向公司投簡歷來找工作
DHCP Offer——DHCP服務(wù)器用來響應(yīng)DHCP Discover報文的報文。該報文攜帶了各種配置信息,例如IP地址,DNS等
公司收到我的求職簡歷后給我發(fā)了一份offer
DHCP Request——客戶端請求配置確認(rèn),或者續(xù)借租期
我收到offer之后跟公司確認(rèn)
DHCP ACK——服務(wù)器對Request報文的確認(rèn)響應(yīng)
公司說我ok沒問題,可以過來上班 ,我找到工作了
5.DHCP 的分配方式
①自動分配:
當(dāng)DHCP客戶機(jī)第一次成功地從DHCP服務(wù)器獲取到一個IP地址后,就永久的使用這個IP地址
②手動分配:
由DHCP服務(wù)器管理員專門指定IP地址
③動態(tài)分配:
當(dāng)DHCP客戶機(jī)第一次從DHCP服務(wù)器獲得IP地址后,并非永久的使用該地址,而是在每次使用完后,DHCP客戶機(jī)就會釋放這個IP地址,供其他客戶機(jī)使用
二、安裝 DHCP 服務(wù)器
1.DHCP 服務(wù)軟件
- CentOS光盤中的 dhcp-4.2.5-47.el7.centos.x86_64.rpm
- 一般都有,沒有的話 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; #配置選項
......
default-lease-time 21600; #配置參數(shù)
host ns {
......
fixed-address 207.175.42.254; #主機(jī)聲明(固定一個IP給主機(jī)用的)
}
}
- Subnet網(wǎng)段聲明,作用于整個子網(wǎng)段
range參數(shù):設(shè)置用于分配的IP地址池
option subnet-mask參數(shù):設(shè)置客戶機(jī)的子網(wǎng)掩碼
option routers參數(shù):設(shè)置客戶機(jī)的默認(rèn)網(wǎng)關(guān)地址
三、配置步驟
1.使用 DHCP 動態(tài)的給 PC 機(jī)分配 IP 地址
① eNSP
實驗拓?fù)鋱D如下
注:配置完“Cloud”之后才能用串口線連接
<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 ##開啟DHCP功能
[L3-SW2]interface Vlanif 10 ##配置虛接口ip,選擇dhcp中繼模式,請求指向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ī)
[root@xjj ~]# yum -y install dhcp
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
軟件包 12:dhcp-4.2.5-58.el7.centos.x86_64 已安裝并且是最新版本
無須任何處理
[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)行驗證網(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
...略
③驗證
- 到eNSP中驗證一下
- 首先開啟兩臺PC的DHCP來自動獲取一下IP地址
④進(jìn)入命令行“ipconfig”測試
pc1和pc2成功!?。?/p>
以上就是超詳細(xì)講解Linux DHCP服務(wù)的詳細(xì)內(nèi)容,更多關(guān)于Linux DHCP服務(wù)的資料請關(guān)注腳本之家其它相關(guān)文章!