DHCP配置;DHCP Relay配置
DHCP配置;DHCP Relay配置
DHCP 配置
实验拓扑
实验需求
- 基于全局地址池的DHCP服务器给客户端分配IP地址
- 基于接口的DCHP服务器给客户端分配IP地址
实验步骤
1. 基于全局地址池的DHCP服务器给客户端分配IP地址
DHCP server 上配置如下
[DHCP server]dhcp enable
[DHCP server] ip pool huawei //设置一个名为 huawei 的全局地址池
[DHCP server-ip-pool-huawei] network 192.168.1.0 mask 24 //设置分配的地址网段
[DHCP server-ip-pool-huawei] gateway-list 192.168.1.1 //设置分配的网关IP
[DHCP server-ip-pool-huawei] lease day 10 hour 12 minute 59 //设置地址池IP租用有效期
[DHCP server-ip-pool-huawei] excluded-ip-address 192.168.1.2
//不参与自动分配的地址,留给DNS使用
[DHCP server-ip-pool-huawei] dns-list 8.8.8.8 192.168.1.2 //主DNS和备用DNS
[DHCP server]int g0/0/0
[DHCP server-GigabitEthernet0/0/0] ip address 192.168.1.1 24
[DHCP server-GigabitEthernet0/0/0] dhcp select global //接口下使能全局方式分配地址
[DHCP server-ip-pool-huawei]static-bind ip-address 192.168.1.3 mac-address 5489-
98e1-76a8 //IP地址和MAC地址进行绑定
2. 在PC1上设置为DHCP自动获取方式,ipconfig 命令查看本机IP信息
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fefb:3545
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 192.168.1.254
Subnet mask.......................: 255.255.255.0
Gateway...........................: 192.168.1.1
Physical address..................: 54-89-98-FB-35-45
DNS server........................: 8.8.8.8
192.168.1.2
3.在PC2上设置为DHCP自动获取方式,ipconfig 命令查看本机IP信息
分析:IP地址和MAC地址做绑定,已生效
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fee1:76a8
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 192.168.1.3
Subnet mask.......................: 255.255.255.0
Gateway...........................: 192.168.1.1
Physical address..................: 54-89-98-E1-76-A8
DNS server........................: 8.8.8.8
192.168.1.2
4.PC1上PING 测试如下:
PC>ping 192.168.1.1
Ping 192.168.1.1: 32 data bytes, Press Ctrl_C to break
From 192.168.1.1: bytes=32 seq=1 ttl=255 time=47 ms
From 192.168.1.1: bytes=32 seq=2 ttl=255 time=31 ms
From 192.168.1.1: bytes=32 seq=3 ttl=255 time=47 ms
From 192.168.1.1: bytes=32 seq=4 ttl=255 time=16 ms
From 192.168.1.1: bytes=32 seq=5 ttl=255 time=47 ms
--- 192.168.1.1 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 16/37/47 ms
5.查看全局地址池的分配信息
[DHCP server]dis ip pool name huawei all
Pool-name : huawei
Pool-No : 0
Lease : 10 Days 12 Hours 59 Minutes
Domain-name : -
DNS-server0 : 8.8.8.8
DNS-server1 : 192.168.1.2
NBNS-server0 : -
Netbios-type : -
Position : Local Status : Unlocked
Gateway-0 : 192.168.1.1
Mask : 255.255.255.0
VPN instance : --
-----------------------------------------------------------------------------
Start End Total Used Idle(Expired) Conflict Disable
-----------------------------------------------------------------------------
192.168.1.1 192.168.1.254 253 2 250(0) 0 1
-----------------------------------------------------------------------------
Network section :
--------------------------------------------------------------------------
Index IP MAC Lease Status
--------------------------------------------------------------------------
1 192.168.1.2 - - Disable
2 192.168.1.3 5489-98e1-76a8 - Static-bind used
2.基于接口的DCHP服务器给客户端分配IP地址
DHCP server 上配置如下
[DHCP server]int g0/0/0
[DHCP server-GigabitEthernet0/0/0] ip address 192.168.1.1 24
[DHCP server-GigabitEthernet0/0/0] dhcp select interface //使能接口分配地址方式且关联接口地址池
[DHCP server-GigabitEthernet0/0/0] dhcp server dns-list 192.168.1.3 8.8.8.8
[DHCP server-GigabitEthernet0/0/0] dhcp server lease day 10 hour 23 minute 59
[DHCP server-GigabitEthernet0/0/0] dhcp server excluded-ip-address 192.168.1.3
在PC1上设置为DHCP自动获取方式,ipconfig 命令查看本机IP信息
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fefb:3545
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 192.168.1.254
Subnet mask.......................: 255.255.255.0
Gateway...........................: 192.168.1.1
Physical address..................: 54-89-98-FB-35-45
DNS server........................: 192.168.1.3
8.8.8.8
DHCP Relay配置
实验拓扑
实验需求
1.在DHCP 服务器上为两个业务网段配置全局地址池;
2.R1设备为DHCP中继,配置DHC服务器组;
3.在DHCP服务器上配置相应网段的静态路由
实验步骤
1.在DHCP server 上配置如下
[Huawei]dhcp enable
[Huawei]ip pool csy
[Huawei-ip-pool-csy] network 192.168.1.0 mask 24
[Huawei-ip-pool-csy] gateway-list 192.168.1.1
[Huawei-ip-pool-csy] excluded-ip-address 192.168.1.3
[Huawei-ip-pool-csy] dns-list 192.168.1.3
[Huawei]ip pool huawei
[Huawei-ip-pool-huawei] network 172.16.1.0 mask 24
[Huawei-ip-pool-huawei] gateway-list 172.16.1.1
[Huawei-ip-pool-huawei] excluded-ip-address 172.16.1.3
[Huawei-ip-pool-huawei] dns-list 172.16.1.3
[Huawei-GigabitEthernet0/0/0]ip address 10.1.12.2 24
2.在DHCP 中继上配置DHCP服务器组
[Huawei]dhcp enble
[Huawei]dhcp server group csy //配置DHCP服务器组名
[Huawei-dhcp-server-group-csy]dhcp-server 10.1.12.2 设定DHCP服务器地址
[Huawei-GigabitEthernet0/0/0] dhcp select relay //启动DHCP Relay功能
[Huawei-GigabitEthernet0/0/0] dhcp relay server-select csy //设定DHCP Relay要使用的服务器组
[Huawei-GigabitEthernet0/0/0]ip address 192.168.1.1 24
[Huawei-GigabitEthernet0/0/2] dhcp select relay
[Huawei-GigabitEthernet0/0/2] dhcp relay server-select csy
[Huawei-GigabitEthernet0/0/2]ip address 172.16.1.1 24
[Huawei-GigabitEthernet0/0/1]ip address 10.1.12.1 24 //配置连接DHCP服务器的接口地址
3.在DHCP 服务器上配置静态回程路由
[Huawei]ip route-static 172.16.1.0 24 10.1.12.1
[Huawei]ip route-static 192.168.1.0 24 10.1.12.1
4.在PC1上查看本机IP信息
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fefb:3545
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 192.168.1.254
Subnet mask.......................: 255.255.255.0
Gateway...........................: 192.168.1.1
Physical address..................: 54-89-98-FB-35-45
DNS server........................: 192.168.1.3
5.在PC4上查看本机IP信息
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fe5a:642e
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 172.16.1.254
Subnet mask.......................: 255.255.255.0
Gateway...........................: 172.16.1.2
Physical address..................: 54-89-98-5A-64-2E
DNS server........................: 172.16.1.3
6.查看中继器配置信息
[Huawei]dis dhcp relay all
DHCP relay agent running information of interface GigabitEthernet0/0/0 :
Server group name : csy
Gateway address in use : 192.168.1.1
DHCP relay agent running information of interface GigabitEthernet0/0/2 :
Server group name : csy
Gateway address in use : 172.16.1.1
7.在DHCP服务器上查看地址池曾经分配过的地址
[Huawei]dis ip pool name csy used
Pool-name : csy
Pool-No : 0
Lease : 1 Days 0 Hours 0 Minutes
Domain-name : -
DNS-server0 : 192.168.1.3
NBNS-server0 : -
Netbios-type : -
Position : Local Status : Unlocked
Gateway-0 : 192.168.1.1
Mask : 255.255.255.0
VPN instance : --
-----------------------------------------------------------------------------
Start End Total Used Idle(Expired) Conflict Disable
-----------------------------------------------------------------------------
192.168.1.1 192.168.1.254 253 1 251(0) 0 1
-----------------------------------------------------------------------------
Network section :
--------------------------------------------------------------------------
Index IP MAC Lease Status
--------------------------------------------------------------------------
253 192.168.1.254 5489-98fb-3545 4860 Used
--------------------------------------------------------------------------
如果DHCP 客户机与DHCP服务器在同一个物理网段,则客户机可以正确地获得动态分配的 ip地址。如果不在同一个物理网段,则需要DCHP Relay Agent(中继代理)。
DHCP报文分析:
① 发现阶段
我们可以看到,刚开始的主机没有地址,也不知道网络中DHCP 服务器是谁,所以主机发送了一个DHCP discover 报文,这个discover 报文源地址是0.0.0.0(我没有地址),目的地址是一个广播地址(共享网络内的所有设备),它是用来发现网络中的DHCP服务器的。
② 提供阶段
是当网络中的DHCP服务器收到discover 的时候会回复offer 提供报文,DHCP Server向DHCP Client提供IP地址的阶段,会从自己维护的地址池中选择一个合适的IP地址,并通过DHCP Offer消息将这个IP地址发送给DHCP Client。DHCP Server是以单播的方式来发送DHCP Offer消息的。下图是offer报文的格式,我们可以看到 DHCP server 给主机 分配的一个地址并且在your ip address 字段进行了填充。
③ 请求阶段
PC上的DHCP Client会在若干个收到的Offer(即若干个收到的DHCP Offer消息)中根据某种原则来确定出自己将要接受哪一个Offer。通常情况下,DHCP Client会接受它所收到的第一个Offer(即最先收到的那个DHCP Offer消息)
这个DHCP Request消息中携带有Server Identifier,是DHCP服务器标识,表示PC上的DHCP 客户机 只愿意接受此DHCP Server所给出的Offer。
其他的DHCP Server收到并分析了该DHCP Request消息后,会明白PC拒绝了自己的Offer。于是,这些DHCP Server就会收回自己当初给予PC的Offer。也就是说,当初准备提供给PC使用的IP地址现在可以用来分配给别的设备使用了。
④ 确认阶段
在确认阶段,DHCP Server会向PC上的DHCP Client发送一个DHCP Ack消息。注意,由于种种原因,DHCP Server也可能会向PC上的DHCP Client发送一个DHCP Nak消息。如果PC接收到了DHCP Nak消息,就说明这次获取IP地址的尝试失败了。在这种情况下,PC只能重新回到发现阶段来开始新一轮的IP地址申请过程。
下图报文分析:当DHCP server 收到request 后,服务器就把地址分配给了主机,并在ack 报文里面的 Your ip address 字段进行了填充,当主机收到ack 后,就获取到了该地址了,并且server 会在option 字段告诉主机一些其他的信息,比如掩码,网关,租期等信息。
通过这四种报文的交互,主机就获取到了一个地址;抓包的时候抓到了一个叫做gratuitous arp 的报文,此报文是为了检查IP地址是否冲突
- DHCP RELEASE :一般出现在客户端关机、下线等状况。这个报文将会使DHCP服务器释放发出此报文的客户端的IP地址
- DHCP INFORM :客户端发出的向服务器请求一些信息的报文
- DHCP DECLINE :当客户端发现服务器分配的IP地址无法使用(如IP地址冲突时),将发出此报文,通知服务器禁止
使用该IP地址。