目录
一.DHCP概念
1.1、什么是DHCP
DHCP——动态主机配置协议
DHCP:(动态主机配置协议)是一个局域网的网络协议。指的是由服务器控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码。默认情况下,DHCP作为Windows Server的一个服务组件不会被系统自动安装,还需要管理员手动安装并进行必要的配置。
1.2、DHCP的作用
为客户机分发IP地址,子网掩码,网关,DNS服务,租期等参数。
1.3、DHCP的好处
1.简化了客户机设置
2.防止用户误操作
3.避免了IP地址重复
1.4、DHCP的架构
架构为C/S结构:C——Client客户机 S——Server服务器
DHCP服务对应传输层是UDP协议,因为是C/S架构,所以DHCP服务是分为DHCP客户机占用UDP67号端口,DHCP服务器占用UDP68号端口
二.DHCP工作原理
(1)DHCP客户机发送DHCP discovery请求。注:DHCP客户机向DHCP服务器发送DHCP请求,来请求IP
(2)局域网中的所以DHCP服务器都会回复DHCP offer,为客户机提供IP地址
(3)客户机选择第一台DHCP服务器回复的IP地址,并且要发送DHCP request通告给局域网内所有的DHCP服务器,它选择了哪个IP和哪个DHCP服务器
备注:客户机发送DHCP request通告的原因有两层,第一层是通告给所有的DHCP服务器,让其他没有被选中的DHCP服务器把未使用到的地址进行回收,第二次是通告给被选中的DHCP服务器,这个地址DHCP的客户机要使用了。
被选中的DHCP服务器收到DHCP request消息后,会给DHCP客户机回复一个DHCP ack,正式确认此IP可以被客户机使用,DHCP客户机将此IP与自己的MAC绑定起来以便于下次继续使用,而其他DHCP服务器将把分配给DHCP客户机的IP回收。
三.DHCP种的租期
DHCP客户机使用IP地址是有限的。一般来说DHCP客户机使用DHCP分配得到的IP地址到了租期的50%时,会主动的向DHCP服务器发出续约请求,DHCP服务器接受到请求后,DHCP服务器会检查此IP地址有没有被别的DHCP客户机抢先占用,如果没有就续约成功,如果被占用则续约失败,此时DHCP客户机将重新发起DHCP discovery请求来获得新的IP地址
四.DHCP服务的配置
DHCP服务实现分为两种情况
4.1、情况一
DHCP服务器和DHCP客户机在同一网段
4.2、情况二
DHCP服务器和DHCP客户机不在同一网段,使用DHCP中继,只能由路由器来实现,DHCP服务器无法单独实现。
五.同网段时DHCP的配置
本次测试使用的eNSP软件
5.1、项目拓扑图
5.2、PC设置
所有PC机都按此设置,点击DHCP和自动获取DNS服务器地址
5.3、二层交换机设置
SW1:
<Huawei>
<Huawei>undo ter
<Huawei>undo terminal mo
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname SW1
[SW1]user-i
[SW1]user-interface c
[SW1]user-interface co
[SW1]user-interface console 0
[SW1-ui-console0]idle-tim
[SW1-ui-console0]idle-timeout 0 0
[SW1-ui-console0]q
[SW1]vlan bat 10 20
[SW1]int e0/0/1
[SW1-Ethernet0/0/1]port li
[SW1-Ethernet0/0/1]port link-type access
[SW1-Ethernet0/0/1]port de
[SW1-Ethernet0/0/1]port default vlan 10
[SW1-Ethernet0/0/1]int e0/0/2
[SW1-Ethernet0/0/2]port li
[SW1-Ethernet0/0/2]port link-type access
[SW1-Ethernet0/0/2]port de
[SW1-Ethernet0/0/2]port default vlan 20
[SW1-Ethernet0/0/2]q
[SW1]int g0/0/1
[SW1-GigabitEthernet0/0/1]port li
[SW1-GigabitEthernet0/0/1]port link-type tr
[SW1-GigabitEthernet0/0/1]port link-type trunk
[SW1-GigabitEthernet0/0/1]port tr
[SW1-GigabitEthernet0/0/1]port trunk all
[SW1-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[SW1-GigabitEthernet0/0/1]q
SW2:
<Huawei>undo terminal mo
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname sw2
[sw2]user-in
[sw2]user-interface co
[sw2]user-interface console 0
[sw2-ui-console0]idle-ti
[sw2-ui-console0]idle-timeout 0 0
[sw2-ui-console0]q
[sw2]vlan bat 10 20
Info: This operation may take a few seconds. Please wait for a moment...done.
[sw2]int e0/0/1
[sw2-Ethernet0/0/1]port l
[sw2-Ethernet0/0/1]port link-type access
[sw2-Ethernet0/0/1]port de
[sw2-Ethernet0/0/1]port default vlan 10
[sw2-Ethernet0/0/1]int e0/0/2
[sw2-Ethernet0/0/2]port li
[sw2-Ethernet0/0/2]port link-type access
[sw2-Ethernet0/0/2]port de
[sw2-Ethernet0/0/2]port default vlan 20
[sw2-Ethernet0/0/2]q
[sw2]int g0/0/1
[sw2-GigabitEthernet0/0/1]port li
[sw2-GigabitEthernet0/0/1]port link-
[sw2-GigabitEthernet0/0/1]port link-type tr
[sw2-GigabitEthernet0/0/1]port link-type trunk
[sw2-GigabitEthernet0/0/1]port po
[sw2-GigabitEthernet0/0/1]port tr
[sw2-GigabitEthernet0/0/1]port trunk all
[sw2-GigabitEthernet0/0/1]port trunk allow-pass vlan all
5.4、三层交换机设置
LS1:
<Huawei>
<Huawei>undo ter
<Huawei>undo terminal mo
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname LS1
[LS1]user-in
[LS1]user-interface co
[LS1]user-interface console 0
[LS1-ui-console0]idle-tim
[LS1-ui-console0]idle-timeout 0 0
[LS1-ui-console0]q
[LS1]vlan bat 10 20
Info: This operation may take a few seconds. Please wait for a moment...done.
[LS1]int g0/0/1
[LS1-GigabitEthernet0/0/1]port l
[LS1-GigabitEthernet0/0/1]port link-t
[LS1-GigabitEthernet0/0/1]port link-type tr
[LS1-GigabitEthernet0/0/1]port link-type trunk
[LS1-GigabitEthernet0/0/1]port tr
[LS1-GigabitEthernet0/0/1]port trunk all
[LS1-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[LS1-GigabitEthernet0/0/1]int g0/0/2
[LS1-GigabitEthernet0/0/2]port l
[LS1-GigabitEthernet0/0/2]port link-ty
[LS1-GigabitEthernet0/0/2]port link-type tr
[LS1-GigabitEthernet0/0/2]port link-type trunk
[LS1-GigabitEthernet0/0/2]port tr
[LS1-GigabitEthernet0/0/2]port trunk all
[LS1-GigabitEthernet0/0/2]port trunk allow-pass vlan all
[LS1-GigabitEthernet0/0/2]q
[LS1]int vlan 10
[LS1-Vlanif10]q
[LS1]dhc
[LS1]dhcp en
[LS1]dhcp enable
Info: The operation may take a few seconds. Please wait for a moment.done.
[LS1]int vlan 10
[LS1-Vlanif10]ip add 192.168.10.1 24
[LS1-Vlanif10]dhcp
[LS1-Vlanif10]dhcp se
[LS1-Vlanif10]dhcp select int
[LS1-Vlanif10]dhcp se
[LS1-Vlanif10]dhcp ser
[LS1-Vlanif10]dhcp server dns
[LS1-Vlanif10]dhcp server dns-list 4.4.4.4 8.8.8.8
[LS1-Vlanif10]q
[LS1]ip poo
[LS1]ip pool dhcp 2
^
Error:Too many parameters found at '^' position.
[LS1]ip pool dhcp2
Info:It's successful to create an IP address pool.
[LS1-ip-pool-dhcp2]net
[LS1-ip-pool-dhcp2]network 192.168.20.0 ma
[LS1-ip-pool-dhcp2]network 192.168.20.0 mask 24
[LS1-ip-pool-dhcp2]gate
[LS1-ip-pool-dhcp2]gateway-l
[LS1-ip-pool-dhcp2]gateway-list 192.168.20.1
[LS1-ip-pool-dhcp2]dns-li
[LS1-ip-pool-dhcp2]dns-list 2.2.2.2 40.40.40.40
[LS1-ip-pool-dhcp2]lea
[LS1-ip-pool-dhcp2]lease
[LS1-ip-pool-dhcp2]lease day 9
[LS1-ip-pool-dhcp2]q
[LS1]int vlan 20
[LS1-Vlanif20]ip add 192.168.20.1 24
[LS1-Vlanif20]dhc
[LS1-Vlanif20]dhcp sr
[LS1-Vlanif20]dhcp se
[LS1-Vlanif20]dhcp select glo
[LS1-Vlanif20]dhcp select global
5.5、ipconfig检测
六.DHCP中继配置(不同网段)
6.1、项目拓扑图
6.2、PC设置
所有PC机都按此设置,点击DHCP和自动获取DNS服务器地址
6.3、二层交换机设置
SW1:
<Huawei>undo te
<Huawei>undo terminal mo
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname SW1
[SW1]user-in
[SW1]user-interface co
[SW1]user-interface console 0
[SW1-ui-console0]idle-t
[SW1-ui-console0]idle-timeout 0 0
[SW1-ui-console0]q
[SW1]vlan bat 10 20
Info: This operation may take a few seconds. Please wait for a moment...done.
[SW1]int e0/0/1
[SW1-Ethernet0/0/1]port li
[SW1-Ethernet0/0/1]port link-type acc
[SW1-Ethernet0/0/1]port link-type access
[SW1-Ethernet0/0/1]port de
[SW1-Ethernet0/0/1]port default vlan 10
[SW1-Ethernet0/0/1]port l
[SW1-Ethernet0/0/1]port link-type access
[SW1-Ethernet0/0/1]q
[SW1]int e0/0/2
[SW1-Ethernet0/0/2]port li
[SW1-Ethernet0/0/2]port link-type access
[SW1-Ethernet0/0/2]port de
[SW1-Ethernet0/0/2]port default vlan 20
[SW1-Ethernet0/0/2]q
[SW1]int g0/0/1
[SW1-GigabitEthernet0/0/1]port
[SW1-GigabitEthernet0/0/1]port l
[SW1-GigabitEthernet0/0/1]port link-type t
[SW1-GigabitEthernet0/0/1]port link-type trunk
[SW1-GigabitEthernet0/0/1]port tr
[SW1-GigabitEthernet0/0/1]port trunk al
[SW1-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[SW1-GigabitEthernet0/0/1]q
SW2:
<Huawei>undo shu
<Huawei>undo ter
<Huawei>undo terminal mo
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname SW2
[SW2]user-imn
[SW2]user-in
[SW2]user-interface co
[SW2]user-interface console 0
[SW2-ui-console0]idle-time
[SW2-ui-console0]idle-timeout 0 0
[SW2-ui-console0]q
[SW2]vlan bat 10 20
Info: This operation may take a few seconds. Please wait for a moment...done.
[SW2]int e0/0/1
[SW2-Ethernet0/0/1]port li
[SW2-Ethernet0/0/1]port link-type a
[SW2-Ethernet0/0/1]port link-type access
[SW2-Ethernet0/0/1]port de
[SW2-Ethernet0/0/1]port default vlan 10
[SW2-Ethernet0/0/1]int e0/0/2
[SW2-Ethernet0/0/2]port l
[SW2-Ethernet0/0/2]port link-type a
[SW2-Ethernet0/0/2]port link-type access
[SW2-Ethernet0/0/2]port de
[SW2-Ethernet0/0/2]port default vlan 20
[SW2-Ethernet0/0/2]q
[SW2]int g0/0/1
[SW2-GigabitEthernet0/0/1]port l
[SW2-GigabitEthernet0/0/1]port link-type t
[SW2-GigabitEthernet0/0/1]port link-type trunk
[SW2-GigabitEthernet0/0/1]port tr
[SW2-GigabitEthernet0/0/1]port trunk all
[SW2-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[SW2-GigabitEthernet0/0/1]q
6.4、三层交换机设置
LS1:
<Huawei>undo ter
<Huawei>undo terminal mon
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname LS1
[LS1]user-int
[LS1]user-interface co
[LS1]user-interface console 0
[LS1-ui-console0]idle-t
[LS1-ui-console0]idle-timeout 0 0
[LS1-ui-console0]q
[LS1]vlan 100
[LS1-vlan100]q
[LS1]int g0/0/3
[LS1-GigabitEthernet0/0/3]port l
[LS1-GigabitEthernet0/0/3]port link-ty
[LS1-GigabitEthernet0/0/3]port link-type acc
[LS1-GigabitEthernet0/0/3]port link-type access
[LS1-GigabitEthernet0/0/3]port de
[LS1-GigabitEthernet0/0/3]port default vlan 100
[LS1-GigabitEthernet0/0/3]q
[LS1]int vlan 100
[LS1-Vlanif100]ip add 10.10.10.1 24
[LS1-Vlanif100]q
[LS1]vlan bat 10 20
Info: This operation may take a few seconds. Please wait for a moment...done.
[LS1]dhcp e
[LS1]dhcp enable
Info: The operation may take a few seconds. Please wait for a moment.done.
[LS1]int vlan 10
[LS1-Vlanif10]dhcp se
[LS1-Vlanif10]dhcp select re
[LS1-Vlanif10]dhcp select relay
[LS1-Vlanif10]dhcp re
[LS1-Vlanif10]dhcp relay se
[LS1-Vlanif10]dhcp relay server-ip 10.10.10.2
[LS1-Vlanif10]int vlan 20
[LS1-Vlanif20]dhcp se
[LS1-Vlanif20]dhcp select re
[LS1-Vlanif20]dhcp select relay
[LS1-Vlanif20]dhcp re
[LS1-Vlanif20]dhcp relay ser
[LS1-Vlanif20]dhcp relay server-ip 10.10.10.2
[LS1]int vlan 10
[LS1-Vlanif10]ip add 192.168.10.1 24
[LS1-Vlanif10]int vlan 20
[LS1-Vlanif20]ip add 192.168.20.1 24
6.5、路由器设置
R1:
<Huawei>undo ter
<Huawei>undo terminal mo
<Huawei>undo terminal monitor
Info: Current terminal monitor is off.
<Huawei>sys
<Huawei>system-view
Enter system view, return user view with Ctrl+Z.
[Huawei]sys
[Huawei]sysname R1
[R1]user-int
[R1]user-interface co
[R1]user-interface console 0
[R1-ui-console0]idle-t
[R1-ui-console0]idle-timeout 0 0
[R1-ui-console0]q
[R1]int g0/0/0
[R1-GigabitEthernet0/0/0]ip add 10.10.10.2 24
[R1-GigabitEthernet0/0/0]undo shu
[R1-GigabitEthernet0/0/0]undo shutdown
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[R1-GigabitEthernet0/0/0]q
[R1]dhcp e
[R1]dhcp enable
Info: The operation may take a few seconds. Please wait for a moment.done.
[R1]int g0/0/0
[R1-GigabitEthernet0/0/0]dhcp se
[R1-GigabitEthernet0/0/0]dhcp select gl
[R1-GigabitEthernet0/0/0]dhcp select global
[R1-GigabitEthernet0/0/0]q
[R1]ip rou
[R1]ip route-st
[R1]ip route-static 192.168.10.0 24 10.10.10.1
[R1]ip route-static 192.168.20.0 24 10.10.10.1
[R1]ip pool de
[R1]ip pool dh
[R1]ip pool dhcp1
Info:It's successful to create an IP address pool.
[R1-ip-pool-dhcp1]net
[R1-ip-pool-dhcp1]network 192.168.10.0 ma
[R1-ip-pool-dhcp1]network 192.168.10.0 mask 24
[R1-ip-pool-dhcp1]ga
[R1-ip-pool-dhcp1]gateway-list 192.168.10.1
[R1-ip-pool-dhcp1]dns
[R1-ip-pool-dhcp1]dns-list 6.6.6.6 8.8.8.8
[R1-ip-pool-dhcp1]lea
[R1-ip-pool-dhcp1]lease da
[R1-ip-pool-dhcp1]lease day 9
[R1-ip-pool-dhcp1]q
[R1]ip pool dhcp2
Info:It's successful to create an IP address pool.
[R1-ip-pool-dhcp2]ne
[R1-ip-pool-dhcp2]net
[R1-ip-pool-dhcp2]network 192.168.20.0 ma
[R1-ip-pool-dhcp2]network 192.168.20.0 mask 24
[R1-ip-pool-dhcp2]gat
[R1-ip-pool-dhcp2]gateway-list 192.168.20.1
[R1-ip-pool-dhcp2]dns
[R1-ip-pool-dhcp2]dns-list 20.20.20.20 9.9.9.9
[R1-ip-pool-dhcp2]lea
[R1-ip-pool-dhcp2]lease day 9