IP动态获取过程中,客户端(DHCP Client)总是以广播(广播帧及广播IP报文)方式来发送DHCPDISCOVER和DHCPREQUEST消息的。如果服务器(DHCP Server)和 客户端不在同一个二层网络(二层广播域)中,服务器不可能接收到DHCPDISCOVER和DHCPREQUEST消息。因此,DHCP工作流程只适合于服务器和客户端位于同一个二层网络的场景。
如果一个网络包含了多个二层网络,是不是要在每个二层网络中都至少部署一个DHCP服务器 呢?理论上讲这样做未尝不可,但实际上这样做是没有必要的,也是很不经济的。而DHCP协议除了定义了 DHCP客户端和 DHCP 服务器这两种角色之外,还定义了DHCP中继代理(DHCP Relay Agent)。DHCP中继代理的基本用途就是在DHCP 客户端和DHCP服务器之间进行DHCP消息的中转。
DHCP客户端利用 DHCP中继代理来从DHCP服务器获取IP地址等配置参数时,DHCP中继代理必须与DHCP客户端位于同一个二层网络,但DHCP服务器可以与DHCP中继代理位于同一个二层网络,也可以与 DHCP中继代理位于不同的二层网络。DHCP客户端与DHCP中继代理之间是以广播方式交换DHCP消息的,但 DHCP中继代理与 DHCP服务器之间是以单播方式交换 DHCP消息的(DHCP中继代理必须事先知道 DHCP服务器的IP地址)。DHCP中继代理通常是部署在路由器上或三层交换机上。
DHCP中继可以使客户端通过它与其他网段的DHCP服务器通信,最终获取IP地址,解决了DHCP客户端不能跨网段向服务器动态获取IP地址的问题。这样,在多个不同网络上的DHCP客户端可以使用同一个DHCP服务器,既节省了成本,又便于进行集中管理和维护。路由器或三层交换机都可以充当 DHCP中继设备。
DHCP客户端首次向DHCP服务器请求IP地址时,通过以下四个阶段完成。
dhcp enable //开启DHCP功能
ip pool IP地址池名称 //创建DHCP的IP地址池
network IP地址 mask 子网掩码或子网掩码位数 //配置DHCP的IP地址池网段
gateway-list 网关IP地址 //配置全局地址池的网关
dhcp select global //关联端口和全局地址池
dhcp select relay //关联端口和DHCP中继代理
dhcp relay server-ip 需要中继的DHCP服务器IP地址 //在DHCP中继代理设备上配置DHCP服务器IP地址
[R2]dhcp server group dhcp-g //创建DHCP服务组
[R2-dhcp-server-group-dhcp-g]dhcp-server 192.168.6.252 //添加DHCP服务器IP地址
[R2-GigabitEthernet0/0/2]dhcp select relay //关联端口和DHCP中继代理
[R2-GigabitEthernet0/0/2]dhcp relay server-select dhcp-g //在DHCP中继代理设备上配置DHCP服务器组
把DHCP-R1和DHCP-R2路由器都配置为DHCP服务器,R2路由器配置为DHCP中继代理,PC1和PC2通过DHCP自动获取IP地址,DHCP-R1、DHCP-R2、R1和R2路由器上配置ospf路由协议,实现网络互联互通。
system-view
sysname R1
interface GigabitEthernet 0/0/0
ip address 123.123.10.1 28
quit
interface GigabitEthernet 0/0/1
ip address 192.168.6.254 24
quit
ospf 1 router-id 1.1.1.1
area 0
network 192.168.6.0 0.0.0.255
network 123.123.10.0 0.0.0.15
quit
system-view
sysname R2
interface GigabitEthernet 0/0/0
ip address 123.123.10.2 28
interface GigabitEthernet 0/0/1
ip address 192.168.7.254 24
interface GigabitEthernet 0/0/2
ip address 192.168.8.254 24
quit
ospf 1 router-id 2.2.2.2
area 0
network 192.168.7.0 0.0.0.255
network 192.168.8.0 0.0.0.255
network 123.123.10.0 0.0.0.15
quit
dhcp enable
interface GigabitEthernet 0/0/1
dhcp select relay
dhcp relay server-ip 192.168.6.253
quit
dhcp server group dhcp-g
dhcp-server 192.168.6.252
quit
interface GigabitEthernet 0/0/2
dhcp select relay
dhcp relay server-select dhcp-g
quit
system-view
sysname DHCP R1
interface GigabitEthernet 0/0/0
ip address 192.168.6.253 24
quit
ospf 1 router-id 3.3.3.3
area 0
network 192.168.6.0 0.0.0.255
quit
dhcp enable
ip pool dhcp-pool-1
network 192.168.7.0 mask 24
gateway-list 192.168.7.254
quit
interface GigabitEthernet 0/0/0
dhcp select global
quit
system-view
sysname DHCP R2
interface GigabitEthernet 0/0/0
ip address 192.168.6.252 24
quit
ospf 1 router-id 4.4.4.4
area 0
network 192.168.6.0 0.0.0.255
quit
dhcp enable
ip pool dhcp-pool-2
network 192.168.8.0 mask 24
gateway-list 192.168.8.254
quit
interface GigabitEthernet 0/0/0
dhcp select global
quit
[Huawei]sysname R1
[R1]interface GigabitEthernet 0/0/0
[R1-GigabitEthernet0/0/0]ip address 123.123.10.1 28
[R1-GigabitEthernet0/0/0]quit
[R1]interface GigabitEthernet 0/0/1
[R1-GigabitEthernet0/0/1]ip address 192.168.6.254 24
[R1-GigabitEthernet0/0/1]quit
[R1]ospf 1 router-id 1.1.1.1
[R1-ospf-1]area 0
[R1-ospf-1-area-0.0.0.0]network 192.168.6.0 0.0.0.255
[R1-ospf-1-area-0.0.0.0]network 123.123.10.0 0.0.0.15
[R1-ospf-1-area-0.0.0.0]quit
[Huawei]sysname R2
[R2]interface GigabitEthernet 0/0/0
[R2-GigabitEthernet0/0/0]ip address 123.123.10.2 28
[R2-GigabitEthernet0/0/0]interface GigabitEthernet 0/0/1
[R2-GigabitEthernet0/0/1]ip address 192.168.7.254 24
[R2-GigabitEthernet0/0/1]interface GigabitEthernet 0/0/2
[R2-GigabitEthernet0/0/2]ip address 192.168.8.254 24
[R2-GigabitEthernet0/0/2]quit
[R2]ospf 1 router-id 2.2.2.2
[R2-ospf-1]area 0
[R2-ospf-1-area-0.0.0.0]network 192.168.7.0 0.0.0.255
[R2-ospf-1-area-0.0.0.0]network 192.168.8.0 0.0.0.255
[R2-ospf-1-area-0.0.0.0]network 123.123.10.0 0.0.0.15
[R2-ospf-1-area-0.0.0.0]quit
[R2-ospf-1]quit
[R2]dhcp enable
[R2]interface GigabitEthernet 0/0/1
[R2-GigabitEthernet0/0/1]dhcp select relay
[R2-GigabitEthernet0/0/1]dhcp relay server-ip 192.168.6.253 //在DHCP中继代理设备上配置DHCP服务器IP地址为192.168.6.253
[R2-GigabitEthernet0/0/1]quit
[R2]dhcp server group dhcp-g //创建DHCP服务组
[R2-dhcp-server-group-dhcp-g]dhcp-server 192.168.6.252 //添加DHCP服务器IP地址为192.168.6.252
[R2-dhcp-server-group-dhcp-g]quit
[R2]interface GigabitEthernet 0/0/2
[R2-GigabitEthernet0/0/2]dhcp select relay
[R2-GigabitEthernet0/0/2]dhcp relay server-select dhcp-g //在DHCP中继代理设备上配置DHCP服务器组
[R2-GigabitEthernet0/0/2]quit
[Huawei]sysname DHCP R1
[DHCP R1]interface GigabitEthernet 0/0/0
[DHCP R1-GigabitEthernet0/0/0]ip address 192.168.6.253 24
[DHCP R1-GigabitEthernet0/0/0]quit
[DHCP R1]ospf 1 router-id 3.3.3.3
[DHCP R1-ospf-1]area 0
[DHCP R1-ospf-1-area-0.0.0.0]network 192.168.6.0 0.0.0.255
[DHCP R1-ospf-1-area-0.0.0.0]quit
[DHCP R1-ospf-1]dhcp enable
[DHCP R1]ip pool dhcp-pool-1
[DHCP R1-ip-pool-dhcp-pool-1]network 192.168.7.0 mask 24 //配置DHCP的IP地址池网段为192.168.7.0/24
[DHCP R1-ip-pool-dhcp-pool-1]gateway-list 192.168.7.254 //配置全局地址池的网关为192.168.7.254
[DHCP R1-ip-pool-dhcp-pool-1]quit
[DHCP R1-ip-pool-dhcp-pool-1]quit
[DHCP R1]interface GigabitEthernet 0/0/0
[DHCP R1-GigabitEthernet0/0/0]dhcp select global //关联端口和全局地址池
[DHCP R1-GigabitEthernet0/0/0]quit
[Huawei]sysname DHCP R2
[DHCP R2]interface GigabitEthernet 0/0/0
[DHCP R2-GigabitEthernet0/0/0]ip address 192.168.6.252 24
[DHCP R2]ospf 1 router-id 4.4.4.4
[DHCP R2-ospf-1]area 0
[DHCP R2-ospf-1-area-0.0.0.0]network 192.168.6.0 0.0.0.255
[DHCP R2-ospf-1-area-0.0.0.0]quit
[DHCP R2-ospf-1]quit
[DHCP R2]dhcp enable
[DHCP R2]ip pool dhcp-pool-2
[DHCP R2-ip-pool-dhcp-pool-2]network 192.168.8.0 mask 24
[DHCP R2-ip-pool-dhcp-pool-2]gateway-list 192.168.8.254
[DHCP R2-ip-pool-dhcp-pool-2]quit
[DHCP R2-ip-pool-dhcp-pool-2]quit
[DHCP R2]interface GigabitEthernet 0/0/0
[DHCP R2-GigabitEthernet0/0/0]dhcp select global
[DHCP R2-GigabitEthernet0/0/0]quit
上图说明PC1和PC2通过DHCP中继代理分别从各自的DHCP服务器成功获取了IP地址。