DHCP 动态主机分配协议,在任何网络中都少不了DHCP的存在,它的主要用途主要有两个:
1.给内部网络或网络服务供应商自动分配IP地址2.给用户或者内部网络管理员作为对所有计算机作中央管理的手段
DHCP的过程原理:
1、client向server发送请求,发向广播地址
2、server向client回应一个IP,发向单播地址
3、client向server回应一个确认,发向广播地址,表示自已已得到IP地址,这样可以防止在网络上有多台DHCP服务器的情况下,其它服务器不会再给它分配IP
4、server再向client回应一个确认
在大的组网种中由于便于管理DHCP,所以只设置一台或两台DHCP服务器,但网络庞大需要有分支网络也需要DHCP的存在,所以也就有了DHCP中继的存在:
由于主机发出的DHCP请求广播报文,当DHCP服务器和申请主机不再同一广播域中时, DHCP请求无法到达服务器。通过在网络设备上启用DHCP中继代理,后者将收到的DHCP请求使用单播的方式转发给DHCP服务器,从而实现主机动态获取IP地址及配置参数。
以下两个DHCP的实验: CISCO实验使用路由器做DHCP服务器 华为实验使用三层交换机做DHCP服务器 ,在两个实验中分别左边用中继 右边直接分配地址来做
CISCO实验
R1:(DHCP路由器)
ip dhcp excluded-address 10.1.10.254 //DHCP不分配指定地址
ip dhcp excluded-address 10.1.20.254
ip dhcp excluded-address 10.1.30.254
ip dhcp pool vlan10 //设置dhcp地址池
network 10.1.10.0 255.255.255.0 //地址池范围
default-router 10.1.10.254 //地址池的网关地址
dns-server 114.114.114.114 8.8.8.8 // 指定DNS服务器
domain-name cisco.com //域名地址
lease 3 //租约时间
!
ip dhcp pool vlan20
network 10.1.20.0 255.255.255.0
default-router 10.1.20.254
dns-server 114.114.114.114 8.8.8.8
domain-name cisco.com
lease 3
!
ip dhcp pool vlan 30
network 10.1.30.0 255.255.255.0
default-router 10.1.30.254
dns-server 114.114.114.114 8.8.8.8
domain-name cisco.com
lease 3
interface Ethernet0/0
ip address 10.1.1.1 255.255.255.0
duplex auto
!
interface Ethernet0/1
ip address 10.1.30.254 255.255.255.0
duplex auto
!
ip route 0.0.0.0 0.0.0.0 10.1.10.254
ip route 0.0.0.0 0.0.0.0 10.1.20.254
SW4:(中继交换机)
interface Ethernet0/0
switchport trunk encapsulation dot1q
switchport trunk native vlan 9
switchport mode trunk
!
interface Ethernet0/1
switchport access vlan 10
switchport mode access
!
interface Ethernet0/2
switchport access vlan 20
switchport mode access
interface Vlan9
ip address 10.1.1.2 255.255.255.0
!
interface Vlan10
ip address 10.1.10.254 255.255.255.0
ip helper-address 10.1.1.1 (DHCP中继命令!!!此地址是dhcp服务器的接口地址)
!
interface Vlan20
ip address 10.1.20.254 255.255.255.0
ip helper-address 10.1.1.1
SW5:(可以当作傻瓜交换机,也可自行配置vlan30)
---------------------------------------------------------------------------------
VPC6
VPCS> dhcp
DORA IP 10.1.10.1/24 GW 10.1.10.254
VPC7
VPCS> dhcp
DORA IP 10.1.20.1/24 GW 10.1.20.254
VPC8
VPCS> dhcp
DORA IP 10.1.30.1/24 GW 10.1.30.254
-------------------------------------------------
VPC6 PING VPC8
VPCS> ping 10.1.30.1
84 bytes from 10.1.30.1 icmp_seq=1 ttl=62 time=2.959 ms
84 bytes from 10.1.30.1 icmp_seq=2 ttl=62 time=1.677 ms
84 bytes from 10.1.30.1 icmp_seq=3 ttl=62 time=0.902 ms
84 bytes from 10.1.30.1 icmp_seq=4 ttl=62 time=1.090 ms
84 bytes from 10.1.30.1 icmp_seq=5 ttl=62 time=1.322 ms
------------------------------------------------------------------------------------
华为实验
SW1:(DHCP服务器)
ip pool vlan10
gateway-list 10.1.10.254
network 10.1.10.0 mask 255.255.255.0
lease day 3 hour 0 minute 0
dns-list 8.8.8.8
domain-name huawei.com
#
ip pool vlan20
gateway-list 10.1.20.254
network 10.1.20.0 mask 255.255.255.0
lease day 3 hour 0 minute 0
dns-list 8.8.8.8
domain-name huawei.com
#
ip pool vlan30
gateway-list 10.1.30.254
network 10.1.30.0 mask 255.255.255.0
lease day 3 hour 0 minute 0
dns-list 8.8.8.8
domain-name huawei.com
interface Vlanif10
ip address 10.1.10.254 255.255.255.0
dhcp select global
interface Vlanif20
ip address 10.1.20.254 255.255.255.0
dhcp select global
interface Vlanif30
ip address 10.1.30.254 255.255.255.0
dhcp select global
interface GigabitEthernet0/0/1
port link-type trunk
port trunk allow-pass vlan 2 to 4094
interface GigabitEthernet0/0/2
port link-type trunk
port trunk allow-pass vlan 2 to 4094
ip route-static 0.0.0.0 0.0.0.0 GigabitEthernet0/0/1 10.1.1.2
SW2:(中继交换机)
interface Vlanif10
dhcp select relay
dhcp relay server-ip 10.1.10.254
interface Vlanif20
dhcp select relay
dhcp relay server-ip 10.1.20.254
interface GigabitEthernet0/0/1
port link-type trunk
port trunk allow-pass vlan 2 to 4094
interface GigabitEthernet0/0/10
port link-type access
port default vlan 10
interface GigabitEthernet0/0/11
port link-type access
port default vlan 20
ip route-static 0.0.0.0 0.0.0.0 GigabitEthernet0/0/1 10.1.1.1
(方案二:仅参考,不实用
SW1:(DHCP服务器)
ip pool vlan10
gateway-list 10.1.10.254
network 10.1.10.0 mask 255.255.255.0
lease day 3 hour 0 minute 0
dns-list 8.8.8.8
domain-name huawei.com
#
ip pool vlan20
gateway-list 10.1.20.254
network 10.1.20.0 mask 255.255.255.0
lease day 3 hour 0 minute 0
dns-list 8.8.8.8
domain-name huawei.com
#
ip pool vlan30
gateway-list 10.1.30.254
network 10.1.30.0 mask 255.255.255.0
lease day 3 hour 0 minute 0
dns-list 8.8.8.8
domain-name huawei.com
interface Vlanif9
ip address 10.1.1.1 255.255.255.0
dhcp select global //要在此接口设置global
interface Vlanif10
ip address 10.1.10.1 255.255.255.0
interface Vlanif20
ip address 10.1.20.1 255.255.255.0
interface Vlanif30 //vlan30 直接分配地址 此地址为网关地址
ip address 10.1.30.254 255.255.255.0
dhcp select global
interface GigabitEthernet0/0/1
port link-type trunk
port trunk allow-pass vlan 2 to 4094
interface GigabitEthernet0/0/2
port link-type trunk
port trunk allow-pass vlan 2 to 4094
ip route-static 0.0.0.0 0.0.0.0 GigabitEthernet0/0/1 10.1.1.2
SW2:(中继交换机)
interface Vlanif9
ip address 10.1.1.2 255.255.255.0
interface Vlanif10
ip address 10.1.10.254 255.255.255.0
dhcp select relay
dhcp relay server-ip 10.1.1.1 //华为中继代理命令
interface Vlanif20
ip address 10.1.20.254 255.255.255.0
dhcp select relay
dhcp relay server-ip 10.1.1.1
interface GigabitEthernet0/0/1
port link-type trunk
port trunk allow-pass vlan 2 to 4094
interface GigabitEthernet0/0/10
port link-type access
port default vlan 10
interface GigabitEthernet0/0/11
port link-type access
port default vlan 20
ip route-static 0.0.0.0 0.0.0.0 GigabitEthernet0/0/1 10.1.1.1
)
SW3:(可以当作傻瓜交换机,也可自行配置vlan30)
----------------------------------------------------------
PC1
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fe60:28d2
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 10.1.10.253
Subnet mask.......................: 255.255.255.0
Gateway...........................: 10.1.10.254
Physical address..................: 54-89-98-60-28-D2
DNS server........................: 8.8.8.8
PC3
PC>ipconfig
Link local IPv6 address...........: fe80::5689:98ff:fe3e:28b9
IPv6 address......................: :: / 128
IPv6 gateway......................: ::
IPv4 address......................: 10.1.30.253
Subnet mask.......................: 255.255.255.0
Gateway...........................: 10.1.30.254
Physical address..................: 54-89-98-3E-28-B9
DNS server........................: 8.8.8.8