8.1 背景
8.2 DHCP报文交互过程
- 客户端发送发现报文,初始情况下,网卡是没有IP地址的,所以第一条报文原地址为0.0.0.0,由于不知道DHCP服务器在哪,所以目标地址为广播地址255.255.255.255。
- DHCP服务器接收到广播报文后,应答报文,应答内容为分配的IP地址,但是这是DHCP服务器不知道客户端是否接受给分配的地址,需要二次确认。
- 客户端应答请求,服务端再次应答,确认接受了分配的IP地址。
当网络中多台DHCP服务器时,客户端广播,多台DHCP服务器都会给应答,客户端收到多个后,只选择一个回送广播报文。
报文1格式:UDP包,广播请求包
报文2格式:UDP包,广播应答包,包含准备分配给客户端的IP地址192.168.1.1,还包括DHCP服务器IP地址、租期、掩码、网关等内容。
报文3格式:UDP包,客户端依旧用0.0.0.0发广播,和DHCP服务器确认需要的地址192.168.1.1。
报文4格式:UDP包,最终确认。
8.3 配置
客户端有可能是网络设备,也有可能是路由器、手机等。3
- 在全局模式在,开启DHCP服务;
- 定义DHCP地址池,一个地址池对应一个网段;
- 进入地址池,配置所关联的网段;
- 除了分配地址,还需要分配网关地址。
全局模式下可以定义指定的部分IP地址不分配。
查询show命令
调试命令
8.4 试验
服务端配置
客户端配置
8.4.1 PC
接口上启动DHCP
PC(config)#no ip routing
PC(config)#interface fastEthernet 0/0
PC(config-if)#ip address dhcp
PC(config-if)#no shutdown
8.4.2 GW
创建VLAN 10
GW#vlan database
GW(vlan)#vla
GW(vlan)#vlan 10
VLAN 10 added:
Name: VLAN0010
将接口设置成ACCESS模式,划入VLAN10
GW(config)#interface fastEthernet 0/0
GW(config-if)#switchport mode access
GW(config-if)#switchport access vlan 10
设置VLAN10 地址
GW(config)#interface vlan 10
GW(config-if)#ip address 192.168.10.254 255.255.255.0
GW(config-if)#no shutdown
启动DHCP SERVER,配置不能分配地址、可用地址池、网关等
GW(config)#service dhcp
GW(config)#ip dhcp excluded-address 192.168.10.254
GW(config)#ip dhcp pool name001
GW(dhcp-config)#network 192.168.10.0 255.255.255.0
GW(dhcp-config)#default-router 192.168.10.254
GW(dhcp-config)#domain-name doname
查看VLAN 信息
GW#show ip interface brief
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES unset up up
FastEthernet0/1 unassigned YES unset up down
FastEthernet0/2 unassigned YES unset up down
FastEthernet0/3 unassigned YES unset up down
FastEthernet0/4 unassigned YES unset up down
FastEthernet0/5 unassigned YES unset up down
FastEthernet0/6 unassigned YES unset up down
FastEthernet0/7 unassigned YES unset up down
FastEthernet0/8 unassigned YES unset up down
FastEthernet0/9 unassigned YES unset up down
FastEthernet0/10 unassigned YES unset up down
FastEthernet0/11 unassigned YES unset up down
FastEthernet0/12 unassigned YES unset up down
FastEthernet0/13 unassigned YES unset up down
FastEthernet0/14 unassigned YES unset up down
FastEthernet0/15 unassigned YES unset up down
Vlan1 unassigned YES unset up down
Vlan10 192.168.10.254 YES manual up up
8.4.3 测试1
PC弹出,给分配了192.168.10.1地址
*Mar 1 00:05:45.383: %DHCP-6-ADDRESS_ASSIGN: Interface FastEthernet0/0 assigned DHCP address 192.168.10.1, mask 255.255.255.0, hostname PC
PING网关测试
PC#ping 192.168.10.254
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.10.254, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 20/220/1012 ms
8.4.4 测试2
关闭PC端口
PC(config)#interface fastEthernet 0/0
PC(config-if)#shutdown
GW清空服务器上所有的绑定
GW#clear ip dhcp binding *
GW开启DEBUG
GW#debug ip dhcp server packet
DHCP server packet debugging is on.
启动PC端口
PC(config)#interface fastEthernet 0/0
PC(config-if)#no shutdown
GW服务器上DEBUG显示的广播包:
GW#
*Mar 1 00:13:15.739: DHCPD: DHCPDISCOVER received from client 0063.6973.636f.2d63.6330.312e.3336.6130.2e30.3030.302d.4661.302f.30 on interface Vlan10.
*Mar 1 00:13:15.743: DHCPD: Allocate an address without class information (192.168.10.0)
GW#
*Mar 1 00:13:17.743: DHCPD: Sending DHCPOFFER to client 0063.6973.636f.2d63.6330.312e.3336.6130.2e30.3030.302d.4661.302f.30 (192.168.10.2).
*Mar 1 00:13:17.743: DHCPD: broadcasting BOOTREPLY to client cc01.36a0.0000.
*Mar 1 00:13:17.763: DHCPD: DHCPREQUEST received from client 0063.6973.636f.2d63.6330.312e.3336.6130.2e30.3030.302d.4661.302f.30.
*Mar 1 00:13:17.763: DHCPD: Appending default domain from pool
*Mar 1 00:13:17.763: DHCPD: Using hostname 'PC.doname.' for dynamic update (from hostname option)
*Mar 1 00:13:17.763: DHCPD: Sending DHCPACK to client 0063.6973.636f.2d63.6330.312e.3336.6130.2e30.3030.302d.4661.302f.30 (192.168.10.2).
GW#
*Mar 1 00:13:17.763: DHCPD: broadcasting BOOTREPLY to client cc01.36a0.0000.
- 从VLAN10收到客户端标识为0063.6973.636f.2d63.6330.312e.3336.6130.2e30.3030.302d.4661.302f.30的消息;
- 给CLIENT应答OFFER,分配为192.168.10.2
PC#show ip interface brief
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 192.168.10.2 YES DHCP up up
回应,应答
GW关闭DEBUG
GW#undebug all
All possible debugging has been turned off
实际上DHCP服务器再给客户端分配192.168.10.2地址之前,会尝试PING这个地址,确认这个地址是否被使用了。
查看绑定信息
GW#show ip dhcp binding
Bindings from all pools not associated with VRF:
IP address Client-ID/ Lease expiration Type
Hardware address/
User name
192.168.10.2 0063.6973.636f.2d63. Mar 02 2002 12:13 AM Automatic
6330.312e.3336.6130.
2e30.3030.302d.4661.
302f.30
查看服务统计信息
GW#show ip dhcp server statistics
Memory usage 23719
Address pools 1
Database agents 0
Automatic bindings 1
Manual bindings 0
Expired bindings 0
Malformed messages 0
Secure arp entries 0
Message Received
BOOTREQUEST 0
DHCPDISCOVER 2
DHCPREQUEST 2
DHCPDECLINE 0
DHCPRELEASE 3
DHCPINFORM 0
Message Sent
BOOTREPLY 0
DHCPOFFER 2
DHCPACK 2
DHCPNAK 0
8.5 中继配置
如果CLIENT和SERVER不在同一个网段,广播如何到达。
如上试验,物理机口收到后,SVI接口阻挡了广播,需要将DHCP RELAY部署在SVI接口上,而不是部署在物理接口上。到SVI后,变成单播包到DHCP Server,单播包的源地址为SVI地址,目标地址为DHCP地址。
Adress为DHCP SERVER的IP地址,此处为192.168.200.200。
接口为:上个试验为三层接口,设置为二层ACCESS,所以为SVI接口,但是如果是路由器,则是物理接口。
注意只需要在第一个阻挡的配,后续都不需要了,因为后续都是单播包。
左边配置在F0/0,右边配置在SVI接口。
在中继器上新建VLAN10 和 VLAN20 ,VLAN10和客户端一起,VLAN20和服务端一起。
中继器:
- 将和客户端接口划入VLAN10 ACCESS,将和服务端的接口划入VLAN20 ACCESS,同时设置两个VLAN的地址;
- 开启DHCP;
- 在VLAN10下配置HELPER ADDRESS,地址为服务器地址,即连接接口地址。
服务器:no ip routing
- 配置接口IP地址(no switchport),配置默认网关为VLAN 20地址;
- 启动DHCP,启动地址池为VLAN10范围,网关为VLAN10地址;
- 第一包,广播后,到中继变成单播包,是通过中继VLAN10的地址收到的,即源地址为VLAN10地址,目的地址为服务器地址;
- 第二包:源地址为服务器地址,目的地址为VLAN10地址,分配10.1地址。
8.5.1 试验
第一步,PC发广播包,在F0/0上单播,单播格式如下
当PC时多个服务的客户端时,发广播,需要设置多个。