409 lines
14 KiB
Markdown
409 lines
14 KiB
Markdown
|
# DHCP
|
|||
|
|
|||
|
### 一、DHCP概述
|
|||
|
|
|||
|
1. **DHCP是什么**
|
|||
|
|
|||
|
- DHCP:Dynamic Host Configuration Protocol :动态主机配置协议
|
|||
|
- DHCP是一种集中对用户IP地址进行动态管理和配置的技术
|
|||
|
|
|||
|
2. **DHCP的作用**
|
|||
|
|
|||
|
- 作用:实现IP地址的动态分配和集中管理
|
|||
|
- 优势:避免手工配置IP地址,提高工作效率,避免出错,且能快速适应网络的变化
|
|||
|
|
|||
|
3. **DHCP角色**
|
|||
|
|
|||
|
- DHCP客户端:需要获取IP地址的设备,如:计算机、服务器、手机、IP电话等
|
|||
|
|
|||
|
- DHCP服务器:负责为DHCP客户端分配IP地址等网络参数的设备
|
|||
|
|
|||
|
- DHCP 中继(中转站-可选):负责DHCP服务器和DHCP客户端之间的DHCP报文(房地产中介公司)
|
|||
|
|
|||
|
> 企业网络中一般不需要部署DHCP中继
|
|||
|
|
|||
|
### 二、DHCP服务器给首次接入网络的客户端分配网络参数的工作原理
|
|||
|
|
|||
|
1. **发现阶段**:客户端广播发送DHCP Discover报文(DHCP发现报文,客户端找DHCP服务器,请求IP地址)
|
|||
|
2. **提供阶段**:服务器收到客户端发的报文后,会单播回复DHCP Offer报文(DHCP回应报文,服务器给客户端发IP地址、网关、DNS等)
|
|||
|
3. **选择阶段**:客户端收到Offer报文后,会再次广播发送DHCP Request报文(DHCP请求报文,客户端广播告知所有的服务器,选择了某某IP地址)
|
|||
|
4. **确认阶段**:服务器收到客户端发报文后,会单播回复DHCP ACK报文(DHCP确认报文,确认分配结果,只有发了ACK报文,客户端才可以使用IP)
|
|||
|
|
|||
|
### 三、基于全局的DHCP实验
|
|||
|
|
|||
|
![image-20240828200710881](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240828200710881.png)
|
|||
|
|
|||
|
- **需求**
|
|||
|
|
|||
|
> PC1/PC2能够自动获取IP地址、网关、DNS
|
|||
|
|
|||
|
- **配置步骤**
|
|||
|
|
|||
|
- 第一步:在系统视图下开启DHCP功能
|
|||
|
- 第二步:配置IP地址池
|
|||
|
- 创建IP地址池
|
|||
|
- 配置可分配的IP地址段
|
|||
|
- 配置网关地址
|
|||
|
- 配置DNS地址
|
|||
|
- 配置租期
|
|||
|
- 第三步:配置R1路由器接口的IP地址
|
|||
|
- 这个接口接收客户端发的报文,这个接口的IP地址,即客户端的网关地址
|
|||
|
- 在这个接口下开启基于全局的DHCP功能
|
|||
|
- 第四步:验证结果
|
|||
|
|
|||
|
- **配置命令**
|
|||
|
|
|||
|
- R1的配置
|
|||
|
|
|||
|
```
|
|||
|
[R1]dhcp enable //开启DHCP功能
|
|||
|
[R1]ip pool tedu //创建IP地址池
|
|||
|
[R1-ip-pool-tedu]network 192.168.1.0 mask 24 //配置可分配的地址段
|
|||
|
[R1-ip-pool-tedu]gateway-list 192.168.1.254 //配置网关地址
|
|||
|
[R1-ip-pool-tedu]dns-list 8.8.8.8 //配置DNS地址
|
|||
|
[R1-ip-pool-tedu]lease day 1 //配置租期
|
|||
|
[R1-ip-pool-tedu]quit
|
|||
|
[R1]int g0/0/0
|
|||
|
[R1-G0/0/0]ip address 192.168.1.254 24 //此接口IP地址和网关地址一致
|
|||
|
[R1-G0/0/0]dhcp select global //在接口下开启基于全局的DHCP
|
|||
|
```
|
|||
|
|
|||
|
- 验证与测试
|
|||
|
|
|||
|
> PC1和PC2选择获取地址的方式为DHCP
|
|||
|
> 然后PC1和PC2在命令行中,使用ipconfig 命令查看IP地址
|
|||
|
|
|||
|
- 数据抓包
|
|||
|
|
|||
|
![image-20240828201014044](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240828201014044.png)
|
|||
|
|
|||
|
- 配置地址排除
|
|||
|
|
|||
|
```
|
|||
|
[R1]ip pool tedu
|
|||
|
[R1-ip-pool-tedu]excluded-ip-address 192.168.1.250 192.168.1.253 //在基于全局的IP地址池中做地址排除,排除的IP地址不做dhcp分发
|
|||
|
```
|
|||
|
|
|||
|
- 查看IP地址池
|
|||
|
|
|||
|
```
|
|||
|
<R1>display ip pool name tedu all //查看全局IP地址池中全部地址信息
|
|||
|
<R1>display ip pool name tedu used //查看全局IP地址池中已分发的IP地址
|
|||
|
```
|
|||
|
|
|||
|
### 四、基于接口的DHCP实验
|
|||
|
|
|||
|
![image-20240828201131434](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240828201131434.png)
|
|||
|
|
|||
|
- **需求**
|
|||
|
|
|||
|
> PC1/PC2能够自动获取IP地址、网关、DNS
|
|||
|
|
|||
|
- **配置步骤**
|
|||
|
|
|||
|
- 第一步:在系统视图下开启DHCP功能
|
|||
|
- 第二步:配置基于接口的DHCP
|
|||
|
- 配置接口的IP地址和掩码
|
|||
|
- 在接口下开启基于接口的DHCP功能
|
|||
|
- 在接口下配置DNS地址
|
|||
|
- 在接口下配置租期
|
|||
|
|
|||
|
- **配置命令**
|
|||
|
|
|||
|
- R1配置
|
|||
|
|
|||
|
```
|
|||
|
[R1]dhcp enable //开启dhcp功能
|
|||
|
[R1]int g0/0/0
|
|||
|
[R1-G0/0/0]ip address 192.168.1.254 24
|
|||
|
[R1-G0/0/0]dhcp select interface //开启基于接口的dhcp 功能
|
|||
|
[R1-G0/0/0]dhcp server dns-list 8.8.8.8
|
|||
|
[R1-G0/0/0]dhcp server lease day 1
|
|||
|
```
|
|||
|
|
|||
|
- **验证与测试**
|
|||
|
|
|||
|
> PC1和PC2选择获取地址的方式为DHCP
|
|||
|
> 然后PC1和PC2在命令行中,使用ipconfig 命令查看IP地址
|
|||
|
|
|||
|
### 五、DHCP中继代理原理
|
|||
|
|
|||
|
- **问题**
|
|||
|
|
|||
|
> 当客户机和DHCP服务器不在一个广播域时,DHCP服务器无法接收到客户机的DHCP discover广播数据包,客户机就无法获得IP地
|
|||
|
|
|||
|
- **解决**
|
|||
|
|
|||
|
> 在客户机所在的广播域中,寻找一台路由器,这台路由器一个端口在客户机所在的广播域,另外一个端口在DHCP服务器所在的广播域,让这台路由器主动接收客户机的DHCP discover数据包,然后由这台路由器代替客户机向DHCP服务器申请IP地址,得到地址后,再把这个地址交给客户机,这台服务器称之为DHCP中继代理服务器
|
|||
|
|
|||
|
- **DHCP中继实验**
|
|||
|
|
|||
|
![image-20240828201521120](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240828201521120.png)
|
|||
|
|
|||
|
- **需求**
|
|||
|
|
|||
|
- 希望PC1/PC2自动获取到IP地址,网关、DNS等网络参数
|
|||
|
- Server1 服务器手工配置一个静态IP地址,192.168.10.253,这个地址是专门给服务器使用的,所以这个地址不能通过DHCP分发,避免IP地址冲突
|
|||
|
|
|||
|
- **配置步骤**
|
|||
|
|
|||
|
- 第一步:配置R2-DHCP服务器
|
|||
|
1. 在系统视图下开启DHCP功能
|
|||
|
2. 在R2-DHCP服务器中配置基于全局的DHCP
|
|||
|
- 创建IP地址池:ntd
|
|||
|
- 定义网段:192.168.10.0/24
|
|||
|
- 定义网关:192.168.10.254
|
|||
|
- 定于DNS:8.8.8.8
|
|||
|
- 定义排除地址:192.168.10.253 这个IP地址不做DHCP分发
|
|||
|
3. 在接口上配置IP地址,开启基于全局的dhcp
|
|||
|
- 第二步:配置DHCP中继
|
|||
|
1. R1中继设备开启DHCP功能
|
|||
|
2. 配置R1中继设备的接口IP地址
|
|||
|
3. 在R1设备连接客户端的接口上开启DHCP中继,指向DHCP服务器:192.168.20.20
|
|||
|
- 第三步:配置静态路由
|
|||
|
- 在R2配置去往192.168.10.0网段的路由,下一跳为192.168.20.10
|
|||
|
- 第四步:验证与测试
|
|||
|
|
|||
|
- **配置命令**
|
|||
|
|
|||
|
- 第一步:配置DHCP服务器
|
|||
|
|
|||
|
- DHCP服务器配置
|
|||
|
|
|||
|
```
|
|||
|
[R2-DHCP]dhcp enable //开启dhcp 功能
|
|||
|
[R2-DHCP]ip pool ntd //创建IP地址池
|
|||
|
[R2-DHCP-ip-pool-ntd]network 192.168.10.0 mask 24
|
|||
|
[R2-DHCP-ip-pool-ntd]gateway-list 192.168.10.254
|
|||
|
[R2-DHCP-ip-pool-ntd]dns-list 8.8.8.8
|
|||
|
[R2-DHCP-ip-pool-ntd]excluded-ip-address 192.168.10.253
|
|||
|
[R2-DHCP-ip-pool-ntd]quit
|
|||
|
[R2-DHCP]int g0/0/0
|
|||
|
[R2-DHCP-GigabitEthernet0/0/0]ip add 192.168.20.20 24
|
|||
|
[R2-DHCP-GigabitEthernet0/0/0]dhcp select global
|
|||
|
```
|
|||
|
|
|||
|
- 第二步:配置DHCP中继
|
|||
|
|
|||
|
```
|
|||
|
[R1-zj]dhcp enable
|
|||
|
[R1-zj]int g0/0/0
|
|||
|
[R1-zj-G0/0/0]ip add 192.168.20.10 24
|
|||
|
|
|||
|
[R1-zj-G0/0/0]int g0/0/1
|
|||
|
[R1-zj-G0/0/1]ip add 192.168.10.254 24
|
|||
|
[R1-zj-G0/0/1]dhcp select relay //在接口上开启dhcp中继功能
|
|||
|
[R1-zj-G0/0/1]dhcp relay server-ip 192.168.20.2 //指定dhcp服务器的IP地址
|
|||
|
```
|
|||
|
|
|||
|
- 第三步:在DHCP服务器上配置去往中继设备的回程路由
|
|||
|
|
|||
|
```
|
|||
|
[R2-DHCP]ip route-static 192.168.10.0 24 192.168.20.10
|
|||
|
```
|
|||
|
|
|||
|
- 第四步:验证与测试
|
|||
|
|
|||
|
```
|
|||
|
在PC1和PC2中设置IP地址的获取方式为DHCP
|
|||
|
在PC1和PC2中命令行中输入命令
|
|||
|
```
|
|||
|
|
|||
|
- **经验证发现**
|
|||
|
|
|||
|
```
|
|||
|
PC1的IP地址是192.168.10.252
|
|||
|
PC2的IP地址是192.168.10.251
|
|||
|
192.168.10.253这个地址,已经被排除,没有被DHCP分发
|
|||
|
```
|
|||
|
|
|||
|
### 六、三层交换机部署DHCP
|
|||
|
|
|||
|
![image-20240829083619899](https://picgo-noriu.oss-cn-beijing.aliyuncs.com/Images/image-20240829083619899.png)
|
|||
|
|
|||
|
- **需求**
|
|||
|
|
|||
|
> vlan10和vlan20的pc自动获取IP地址
|
|||
|
|
|||
|
- **配置步骤**
|
|||
|
|
|||
|
- 第一步:在sw1部署dhcp
|
|||
|
1. 创建vlan
|
|||
|
2. 配置trunk链路-交换机互联接口做trunk
|
|||
|
3. 配置vlanif 虚接口地址--每个vlan的网关地址
|
|||
|
4. 开启dhcp 功能
|
|||
|
5. 创建IP地址池
|
|||
|
- 定义网段
|
|||
|
- 定义网关
|
|||
|
- 定义dns
|
|||
|
6. 在vlanif 虚接口下,开启dhcp select global
|
|||
|
- 第二步:配置sw2和sw3接入层交换机
|
|||
|
1. 创建vlan
|
|||
|
2. 接口加入vlan,交换机和pc互联的接口做access
|
|||
|
3. 交换机和交换机互联的接口做trunk
|
|||
|
- 第三步:验证与测试
|
|||
|
|
|||
|
- **配置命令**
|
|||
|
|
|||
|
- 第一步:在sw1部署dhcp
|
|||
|
|
|||
|
- DHCP服务器配置
|
|||
|
|
|||
|
```
|
|||
|
[SW1-DHCP]vlan batch 10 20
|
|||
|
[SW1-DHCP]port-group group-member g0/0/1 g0/0/2
|
|||
|
[SW1-DHCP-port-group]port link-type trunk
|
|||
|
[SW1-DHCP-port-group]port trunk allow-pass vlan all
|
|||
|
[SW1-DHCP-port-group]quit
|
|||
|
[SW1-DHCP]ip pool vlan10
|
|||
|
[SW1-DHCP-ip-pool-vlan10]network 192.168.10.0 mask 24
|
|||
|
[SW1-DHCP-ip-pool-vlan10]gateway-list 192.168.10.254
|
|||
|
[SW1-DHCP-ip-pool-vlan10]dns-list 8.8.8.8
|
|||
|
[SW1-DHCP-ip-pool-vlan10]quit
|
|||
|
[SW1-DHCP]ip pool vlan20
|
|||
|
[SW1-DHCP-ip-pool-vlan20]network 192.168.20.0 mask 24
|
|||
|
[SW1-DHCP-ip-pool-vlan20]gateway-list 192.168.20.254
|
|||
|
[SW1-DHCP-ip-pool-vlan20]dns-list 8.8.8.8
|
|||
|
[SW1-DHCP-ip-pool-vlan20]quit
|
|||
|
[SW1-DHCP]dhcp enable
|
|||
|
[SW1-DHCP]int vlanif 10
|
|||
|
[SW1-DHCP-Vlanif10]ip address 192.168.10.254 24
|
|||
|
[SW1-DHCP-Vlanif10]dhcp select global
|
|||
|
[SW1-DHCP-Vlanif10]int vlanif 20
|
|||
|
[SW1-DHCP-Vlanif20]ip address 192.168.20.254 24
|
|||
|
[SW1-DHCP-Vlanif20]dhcp select global
|
|||
|
```
|
|||
|
|
|||
|
- 第二步:配置sw2和sw3接入层交换机
|
|||
|
|
|||
|
- SW2配置
|
|||
|
|
|||
|
```
|
|||
|
[SW2]vlan batch 10 20
|
|||
|
[SW2]port-group group-member g0/0/2 g0/0/3
|
|||
|
[SW2-port-group]port link-type access
|
|||
|
[SW2-port-group]port default vlan 10
|
|||
|
[SW2-port-group]quit
|
|||
|
[SW2]int g0/0/1
|
|||
|
[SW2-G0/0/1]port link-type trunk
|
|||
|
[SW2-G0/0/1]port trunk allow-pass vlan all
|
|||
|
```
|
|||
|
|
|||
|
- SW3配置
|
|||
|
|
|||
|
```
|
|||
|
[SW3]vlan batch 10 20
|
|||
|
[SW3]port-group group-member g0/0/2 g0/0/3
|
|||
|
[SW3-port-group]port link-type access
|
|||
|
[SW3-port-group]port default vlan 20
|
|||
|
[SW3-port-group]quit
|
|||
|
[SW3]int g0/0/1
|
|||
|
[SW3-G0/0/1]port link-type trunk
|
|||
|
[SW3-G0/0/1]port trunk allow-pass vlan all
|
|||
|
```
|
|||
|
|
|||
|
- 第三步:验证与测试
|
|||
|
|
|||
|
- 在PC1和PC2中设置IP地址的获取方式为DHCP
|
|||
|
|
|||
|
- 在PC1和PC2中命令行中输入命令
|
|||
|
|
|||
|
```
|
|||
|
ipconfig /release //释放当前IP地址
|
|||
|
ipconfig /renew //更新IP地址(获取IP地址)
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
<SW1-DHCP>display ip pool name vlan10 all //查看地址池中全部IP信息
|
|||
|
<SW1-DHCP>display ip pool name vlan10 used //查看地址中已使用的IP地址
|
|||
|
<SW1-DHCP>display ip pool name vlan20 all //查看地址池中全部IP信息
|
|||
|
<SW1-DHCP>display ip pool name vlan20 used //查看地址中已使用的IP地址
|
|||
|
```
|
|||
|
|
|||
|
```
|
|||
|
<S3>reset ip pool name vlan10 192.168.10.253回收已分配的IP地址
|
|||
|
<S3>reset ip pool name vlan10 all
|
|||
|
```
|
|||
|
|
|||
|
### 七、租期
|
|||
|
|
|||
|
> 1)租期T1:
|
|||
|
> 当IP地址的租用时间到达50%时,客户机会以单播方式向服务器发送Request 请求报文,请求更新IP地址租期。
|
|||
|
> DHCP 服务器如果回复DHCP ACK确认报文给客户机,代表租期更新成功,从0开始
|
|||
|
|
|||
|
> 2)租期T2
|
|||
|
> 如果DHCP服务器一直没有回应,那么在IP地址的租用时间到达87.5%时,客户机会以广播方式向服务器发送Request 请求报文,请求更新IP地址租期
|
|||
|
> DHCP服务器如果回复DHCP ACK 确认报文给客户机,代表租期更新成功,从0开始
|
|||
|
|
|||
|
1. **access接口恢复默认配置(缺省配置)**
|
|||
|
|
|||
|
```
|
|||
|
[SW1]vlan batch 10 20
|
|||
|
[SW1]interface g0/0/1
|
|||
|
[SW1-GigabitEthernet0/0/1]port link-type access
|
|||
|
[SW1-GigabitEthernet0/0/1]port default vlan 10
|
|||
|
[SW1-GigabitEthernet0/0/1]port link-type trunk //将接口模式修改为trunk(失败)
|
|||
|
Error: Please renew the default configurations. :错误:请更新默认配置
|
|||
|
```
|
|||
|
|
|||
|
- 如何解决:如何恢复接口上VLAN的缺省配置
|
|||
|
|
|||
|
- 第一种方法
|
|||
|
|
|||
|
```
|
|||
|
[SW1-GigabitEthernet0/0/1]port default vlan 1 //接口pvid恢复为vlan1
|
|||
|
[SW1-GigabitEthernet0/0/1]port link-type trunk //将接口模式修改为trunk(成功)
|
|||
|
```
|
|||
|
|
|||
|
- 第二种方法
|
|||
|
|
|||
|
```
|
|||
|
[SW1-GigabitEthernet0/0/1]undo port default vlan //恢复接口上VLAN的缺省配置
|
|||
|
[SW1-GigabitEthernet0/0/1]port link-type trunk //将接口模式修改为trunk(成功)
|
|||
|
```
|
|||
|
|
|||
|
2. **trunk接口恢复默认配置(缺省配置)**
|
|||
|
|
|||
|
```
|
|||
|
[SW1]vlan batch 10 20
|
|||
|
[SW1]interface g0/0/1
|
|||
|
[SW1-G0/0/1]port link-type trunk
|
|||
|
[SW1-G0/0/1]port trunk allow-pass vlan 10 20
|
|||
|
[SW1-G0/0/1]port link-type access //将接口模式修改为access(失败)
|
|||
|
Error: Please renew the default configurations. :错误:请更新默认配置
|
|||
|
```
|
|||
|
|
|||
|
- 如何解决:如何恢复接口上VLAN的缺省配置
|
|||
|
|
|||
|
```
|
|||
|
[SW1-G0/0/1]undo port trunk allow-pass vlan 10 20 //恢复默认
|
|||
|
[SW1-G0/0/1]port link-type access //将接口模式修改为access(成功)
|
|||
|
```
|
|||
|
|
|||
|
- 备注:
|
|||
|
|
|||
|
```
|
|||
|
[SW1-G0/0/1]undo port trunk allow-pass vlan all //删除所有vlan
|
|||
|
[SW1-G0/0/1]port link-type access //无法修改报错
|
|||
|
Error: Please renew the default configurations.
|
|||
|
```
|
|||
|
|
|||
|
- 如何解决:恢复默认配置: undo port trunk allow-pass vlan all 的时候把所有vlan 都干掉了
|
|||
|
所以我们要恢复vlan1
|
|||
|
|
|||
|
- 第一种方法
|
|||
|
|
|||
|
```
|
|||
|
[SW1-G0/0/1]undo port trunk allow-pass vlan 2 to 4094 //只删除2 to 4094 不删除vlan1
|
|||
|
[SW1-G0/0/1]port link-type access //修改成功
|
|||
|
```
|
|||
|
|
|||
|
- 第二种方法
|
|||
|
|
|||
|
```
|
|||
|
[SW1-G0/0/1]undo port trunk allow-pass vlan all //删除所有vlan
|
|||
|
[SW1-G0/0/1]port trunk allow-pass vlan 1 //补上vlan1,恢复默认
|
|||
|
[SW1-G0/0/1]port link-type access //修改成功
|
|||
|
```
|
|||
|
|
|||
|
|