目录
一.DHCP概念
DHCP——动态主机配置协议
是一个局域网的网络协议,使用UDP协议工作。
1.作用
- 为客户机分发IP地址,子网掩码,网关,DNS服务器,租期等参数
2.架构
C/S架构 C—Client 客户机 S—Server 服务器
DHCP服务对应传输层的UDP协议。因为是C/S架构,所以DHCP客户机占用UDP 67号端口,DHCP服务器占用UDP 68号端口。
3.优点
- 简化了客户机设置
- 防止用户误操作
- 避免IP地址的重复
4.租期
DHCP客户机使用IP地址是有期限的。一般DHCP客户机使用DHCP分配的IP地址到了租期的50%的时候,会主动的向DHCP服务器发出续约的请求,DHCP服务器接收到DHCP客户机的续约请求后,DHCP服务器会检查此IP地址有没有被别的DHCP客户机抢先占用。如果没有,就续约成功。如果此IP地址有被其他的DHCP客户机占用,那么续约就不成功,此时DHCP客户机将会重新发起DHCP discovery请求来获取新的IP地址。
二.DHCP原理
- DHCP客户机向局域网中所有的DHCP服务器发送DHCP discovery请求
(DHCP客户机向DHCP服务器发送DHCP请求,来请求IP地址)
- 局域网中的所有DHCP服务器都会回复DHCP offer,为客户机提供IP地址
- 客户机选择第一台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地址回收。
三.拓扑图
1.相同网段
二层交换机sw1
<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]sysname sw1
[sw1]user-interface console 0
[sw1-ui-console0]idle-timeout 0 0
[sw1-ui-console0]q
[sw1]vlan batch 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 link-type access
[sw1-Ethernet0/0/1]port default vlan 10
[sw1-Ethernet0/0/1]int e0/0/2
[sw1-Ethernet0/0/2]port link-type access
[sw1-Ethernet0/0/2]port default vlan 20
[sw1-Ethernet0/0/2]int g0/0/1
[sw1-GigabitEthernet0/0/1]port link-type trunk
[sw1-GigabitEthernet0/0/1]port trunk allow-pass vlan all
三层交换机
<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]sysname sw3
[sw3]user-interface console 0
[sw3-ui-console0]idle-timeout 0 0
[sw3-ui-console0]q
[sw3]vlan batch 10 20
Info: This operation may take a few seconds. Please wait for a moment...done.
[sw3]int g0/0/1
[sw3-GigabitEthernet0/0/1]port link-type trunk
[sw3-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[sw3-GigabitEthernet0/0/1]int g0/0/2
[sw3-GigabitEthernet0/0/2]port link-type trunk
[sw3-GigabitEthernet0/0/2]port trunk allow-pass vlan all
[sw3-GigabitEthernet0/0/2]q
[sw3]dhcp enable
Info: The operation may take a few seconds. Please wait for a moment.done.
[sw3]int vlan 10
[sw3-Vlanif10]ip add 192.168.10.1 24
[sw3-Vlanif10]dhcp select interface //第一种方法 指定DHCP接口
[sw3-Vlanif10]dhcp server dns-list 1.1.1.1 2.2.2.2
[sw3-Vlanif10]q
[sw3]ip pool dhcp2 //第二种方法 创建地址池
Info:It's successful to create an IP address pool.
[sw3-ip-pool-dhcp2]network 192.168.20.0 mask 24
[sw3-ip-pool-dhcp2]gateway-list 192.168.20.1
[sw3-ip-pool-dhcp2]dns-list 3.3.3.3 4.4.4.4
[sw3-ip-pool-dhcp2]lease day 2
[sw3-ip-pool-dhcp2]int vlan 20
[sw3-Vlanif20]ip add 192.168.20.1 24
[sw3-Vlanif20]dhcp select global
2.不同网段
三层交换机
<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]sysname sw3
[sw3]user-interface console 0
[sw3-ui-console0]idle-timeout 0 0
[sw3-ui-console0]q
[sw3]vlan batch 10 20 100
Info: This operation may take a few seconds. Please wait for a moment...done.
[sw3]int g0/0/1
[sw3-GigabitEthernet0/0/1]port link-type trunk
[sw3-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[sw3-GigabitEthernet0/0/1]int g0/0/2
[sw3-GigabitEthernet0/0/2]port link-type trunk
[sw3-GigabitEthernet0/0/2]port trunk allow-pass vlan all
[sw3-GigabitEthernet0/0/2]int vlan 10
[sw3-Vlanif10]ip add 192.168.10.1 24
[sw3-Vlanif10]int vlan 20
[sw3-Vlanif20]ip add 192.168.20.1 24
[sw3-Vlanif20]int g0/0/3
[sw3-GigabitEthernet0/0/3]port link-type access
[sw3-GigabitEthernet0/0/3]port default vlan 100
[sw3-GigabitEthernet0/0/3]int vlan 100
[sw3-Vlanif100]ip add 10.10.10.1 24
[sw3]dhcp enable
Info: The operation may take a few seconds. Please wait for a moment.done.
[sw3]int vlan 10
[sw3-Vlanif10]dhcp select relay
[sw3-Vlanif10]dhcp relay server-ip 10.10.10.2
[sw3-Vlanif10]int vlan 20
[sw3-Vlanif20]dhcp select relay
[sw3-Vlanif20]dhcp relay server-ip 10.10.10.2
DHCP服务器
<Huawei>u t m
Info: Current terminal monitor is off.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]sysname r1
[r1]user-interface console 0
[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 shutdown
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[r1-GigabitEthernet0/0/0]q
[r1]dhcp enable
Info: The operation may take a few seconds. Please wait for a moment.done.
[r1]ip pool dhcp1
Info:It's successful to create an IP address pool.
[r1-ip-pool-dhcp1]network 192.168.10.0 mask 24
[r1-ip-pool-dhcp1]gateway-list 192.168.10.1
[r1-ip-pool-dhcp1]dns-list 1.2.3.4 2.3.4.5
[r1-ip-pool-dhcp1]lease day 2
[r1-ip-pool-dhcp1]ip pool dhcp2
Info:It's successful to create an IP address pool.
[r1-ip-pool-dhcp2]network 192.168.20.0 mask 24
[r1-ip-pool-dhcp2]gateway-list 192.168.20.1
[r1-ip-pool-dhcp2]dns-list 3.4.5.6 5.6.7.8
[r1-ip-pool-dhcp2]lease day 2
[r1-ip-pool-dhcp2]q
[r1]int g0/0/0
[r1-GigabitEthernet0/0/0]dhcp select global
[r1-GigabitEthernet0/0/0]display this
#
interface GigabitEthernet0/0/0
ip address 10.10.10.2 255.255.255.0
dhcp select global
#
return
[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
PC1ping PC4ping通
总结
1.无论是相同网段还是不同网段都要开启DHCP功能 [ ]DHCP enable
2.DHCP服务器中要设置静态路由