DHCP的用途

DHCP:动态主机配置协议,是一个局域网的网络协议,使用UDP协议工作。

用途

  • 给内部网络或网络服务供应商自动分配IP地址。
  • 给用户或者内部网络管理员作为对所有计算机作中央管管理的手段。

    使用DHCP的好处

    1. 减少管理员的工作量
    2. 避免输入错误的可能
    3. 避免IP地址冲突
    4. 当更改IP地址段时,不需要重新配置每个用户的IP地址
    5. 提高了IP地址的利用率
    6. 方便客户端的配置

      DHCP采用的传输协议

      DHCP协议采用UDP作为传输协议,主机发送请求消息到DHCP服务器的67号端口,DHCP服务器回应应答消息给客户端的68号端口。

      DHCP典型应用模式

      在网络中架设一台专用的DHCP服务器,负责集中分配各种网络地址参数(主要包括IP地址、子网掩码、广播地址、默认网关地址、DNS服务器地址);其他主机作为DHCP客户机,将网卡配置为自动获取地址,即可与DHCP服务器进行通信,完成自动配置过程。

      DHCP的IP地址自动自动获取原理

      第一步:客户端在网络中搜索服务器
      客户端通过广播发送DHCP Discover报文寻找服务器端

第二步:服务器向客户端响应服务
服务器端通过单播发送DHCP Offer报文向客户端提供IP地址等网络信息,从IP地址池中挑选一个尚未分配的IP分配给客户端

第三步:客户端向服务器发出服务请求
如果有多台DHCP服务器向该客户端发来DHCP-offer报文,客户端只接受第一个收到的DHCP-offer报文并提取IP地址,然后客户端通过广播发送DHCP Request报文告知服务器端本地选择使用该IP地址。

第四步:服务器向客户端提供服务
服务器通过单播发送DHCP Ack报文告知客户端IP地址是合法可用的,并在选项字段中增加IP地址的使用租期信息
(5)重新登录
DHCP客户机每次重新登陆陆网络时,不需要再发送DHCP Discover信息,而是直接发送包含前一次所分配的IP地址的DHCP Requset请求消息

(6)更新租约
当DHCP服务器向客户机出租的IP地址租期达到一半时,就需要更新租约,客户机直接向服务器发送DHCP Request包,要求更新现有的地址租约。

::: hljs-center

DHCP交互过程(工作原理).png

:::

:华为系统的DHCP服务器会按照由大到小的顺序从IP池里分配IP

DHCP有两种配置方法

1.接口模式
接口地址池可动态分配IP地址,范围就是接口的IP地址所在的网段,且只在此接口下生效,当DHCP服务器收到DHCP客户端的请求报文后,DHCP服务器将会使用该接口的地址网段给客户端分配地址。

[R1]dhcp enable   ###系统模式开启DHCP功能

[R1]interface GigabitEthernet0/0/0
[R1-GigabitEthernet0/0/0]dhcp select interface   ###基于接口配置DHCP
[R1-GigabitEthernet0/0/0]dhcp server lease day 7   ###配置基于接口的DHCP servser租期(默认租期为1天)
[R1-GigabitEthernet0/0/0]dhcp server excluded-ip-address 192.168.10.200 192.168.10.210   ###配置接口地址池中不参与自动分配的IP范围为192.168.10.200到192.168.10.210

[R1-GigabitEthernet0/0/0]dhcp server dns-list 8.8.8.8   ###给用户端自动分配DNS服务器地址

2.全局模式
全局方式下,设置多个全局地址池,并存在定义的地址池网段与端口IP是同一网段的对应关系,即多个不同网段地址池对应各自的端口,最终实现不同网段客户端自动获取相应地址。

[R1]dhcp enable   ###系统模式开启DHCP功能

[R1]ip pool 1   ###创建地址池名称
[R1-ip-pool-1]gateway-list 192.168.20.1   ###设置DHCP客户端的出口网关地址
[R1-ip-pool-1]network 192.168.20.0 mask 255.255.255.0   ###配置DHCP可分配地址网段,掩码也可用24表示

[R1-ip-pool-1]dns-list 114.114.114.114   ###配置DHCP分配DNS地址
[R1-ip-pool-1]excluded-ip-address 192.168.20.250 192.168.20.254   ###配置保留地址
[R1-ip-pool-1]static-bind ip-address 192.168.20.9 mac-address 5489-98c6-0CEE   ###绑定 IP192.168.20.9 固定分配给MAC 为28d2-4469-5a55的机器,需池中IP
[R1-ip-pool-1]lease day 10   ###租期10天

[R1]int g0/0/0
[R1-GigabitEthernet0/0/0]dhcp select global   ###配置DHCP为global全局模式,interface即接口模式
[Huawei]display ip pool   ###查看IP地址池信息

DHCP中继

应用场景

在大型的网络中,可能会存在多个网段,DHCP客户机通过网络广播消息获得DHCP服务器的响应后得到IP地址。但广播消息是不能跨越网段的。因此,如果DHCP客户机和服务器在不同的网段内,客户机还能不能向服务器申请IP地址呢?这就要用到DHCP中继代理。DHCP中继代理实际上是一种软件技术,安装在DHCP中继代理的设备(路由器,交换机,服务器)称为DHCP中继代理服务器,它承担不同网段间的DHCP客户机和服务器的通信任务。

工作原理

  • 当DHCP客户端启动并进行DHCP初始化时,它在本地网络广播配置请求报文。
  • 如果本地网络存在DHCP服务器,则可以直接进行DHCP配置,不需要DHCP中继。
  • 如果本地网络没有DHCP服务器,则与本网络相连的且带DHCP中继功能的网络设备收到该广播报文后,进行适当的处理并转发给指定的在其他网络上的DHCP服务器。
  • DHCP服务器根据客户端提供的信息进行相应的配置,并通过DHCP中继将配置信息发送给客户端,完成对客户端的动态配置。

    DHCP中继服务器

    配置DHCP中继服务器

    
    [R1]dhcp enable
    [R1]interface GigabitEthernet0/0/1   ###与客户端相连的接口
    [R1-GigabitEthernet0/0/1]ip add 192.168.1.1 24
    [R1-GigabitEthernet0/0/1]dhcp select relay   ###开启DHCP中继功能
    [R1-GigabitEthernet0/0/1]dhcp relay server-ip 12.0.0.1   ###指向DHCP服务器的地址12.0.0.1请求DHCP服务

[R1]Ip route-static 0.0.0.0 0.0.0.0 12.0.0.1 ###如果DHCP中继服务器与DHCP服务器跨网段则要配置静态或动态路由

[R1]interface GigabitEthernet0/0/0
[R1-GigabitEthernet0/0/0]ip add 12.0.0.2 24

#### 配置DHCP服务器

[R2]dhcp enable
[R2]interface GigabitEthernet 0/0/0 ###与DHCP中继连接方向的接口
[R2-GigabitEthernet0/0/0]ip add 12.0.0.1 24
[R2-GigabitEthernet0/0/0]dhcp select global ###给接口配置为全局模式 请求DHCP服务

[R2]ip pool 1
[R2-ip-pool-1]gateway-list 192.168.1.1
[R2-ip-pool-1]network 192.168.1.0 mask 255.255.255.0
[R2-ip-pool-1]dns-list 114.114.114.114
[R2-ip-pool-1]lease day 3 ###租期3天
[Huawei]ip route-static 192.168.1.0 255.255.255.0 12.0.0.2 ###记得要配置静态/默认路由或者动态路

### 配置实验
::: hljs-center

![DHCP中继配置.png](https://s2.51cto.com/images/20211021/1634785072650890.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

:::

交换机配置
The device is running!

<Huawei>
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]use
[Huawei]user-in
[Huawei]user-interface con
[Huawei]user-interface console 0
[Huawei-ui-console0]i
[Huawei-ui-console0]idle-timeout 0 0
[Huawei-ui-console0]
Oct 21 2021 10:42:59-08:00 Huawei DS/4/DATASYNC_CFGCHANGE:OID 1.3.6.1.4.1.2011.5
.25.191.3.1 configurations have been changed. The current change number is 4, th
e change loop count is 0, and the maximum number of records is 4095.
[Huawei-ui-console0]q
[Huawei]un in en
Info: Information center is disabled.
[Huawei]u t m
[Huawei]sys SW1
[SW1]v b 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]p l a
[SW1-Ethernet0/0/1]p d v 10
[SW1-Ethernet0/0/1]int e0/0/2
[SW1-Ethernet0/0/2]p l a
[SW1-Ethernet0/0/2]p d v 20
[SW1-Ethernet0/0/2]int e0/0/3
[SW1-Ethernet0/0/3]p l a
[SW1-Ethernet0/0/3]p d v 10
[SW1-Ethernet0/0/3]int e0/0/4
[SW1-Ethernet0/0/4]p l a
[SW1-Ethernet0/0/4]p d v 20
[SW1-Ethernet0/0/4]int g0/0/1
[SW1-GigabitEthernet0/0/1]p l t
[SW1-GigabitEthernet0/0/1]p t a v 10 20
[SW1-GigabitEthernet0/0/1]q
[SW1]

R1配置
The device is running!

<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]un in en
Info: Information center is disabled.
[Huawei]use
[Huawei]user-in
[Huawei]user-interface con
[Huawei]user-interface console 0
[Huawei-ui-console0]i
[Huawei-ui-console0]idle-timeout 0 0
[Huawei-ui-console0]q
[Huawei]sys R1
[R1]dhcp en
Info: The operation may take a few seconds. Please wait for a moment.done.
[R1]int g0/0/1.10
[R1-GigabitEthernet0/0/1.10]p t v 10
^
Error: Wrong parameter found at '^' position.
[R1-GigabitEthernet0/0/1.10]d t v 10
[R1-GigabitEthernet0/0/1.10]ip add 192.168.10.1 24
[R1-GigabitEthernet0/0/1.10]a b e
[R1-GigabitEthernet0/0/1.10]un shu
Info: Interface GigabitEthernet0/0/1.10 is not shutdown.
[R1-GigabitEthernet0/0/1.10]dh
[R1-GigabitEthernet0/0/1.10]dhcp se
[R1-GigabitEthernet0/0/1.10]dhcp select re
[R1-GigabitEthernet0/0/1.10]dhcp select relay
[R1-GigabitEthernet0/0/1.10]dh
[R1-GigabitEthernet0/0/1.10]dhcp re
[R1-GigabitEthernet0/0/1.10]dhcp relay se
[R1-GigabitEthernet0/0/1.10]dhcp relay server-ip 14.0.0.2
[R1-GigabitEthernet0/0/1.10]int g0/0/1.20
[R1-GigabitEthernet0/0/1.20]d t v 20
[R1-GigabitEthernet0/0/1.20]ip add 192.168.20.1 24
[R1-GigabitEthernet0/0/1.20]a b e
[R1-GigabitEthernet0/0/1.20]un shu
Info: Interface GigabitEthernet0/0/1.20 is not shutdown.
[R1-GigabitEthernet0/0/1.20]dh
[R1-GigabitEthernet0/0/1.20]dhcp sel
[R1-GigabitEthernet0/0/1.20]dhcp select re
[R1-GigabitEthernet0/0/1.20]dhcp select relay
[R1-GigabitEthernet0/0/1.20]dh
[R1-GigabitEthernet0/0/1.20]dhcp re
[R1-GigabitEthernet0/0/1.20]dhcp relay se
[R1-GigabitEthernet0/0/1.20]dhcp relay server-ip 14.0.0.2
[R1-GigabitEthernet0/0/1.20]int g0/0/2
[R1-GigabitEthernet0/0/2]ip add 14.0.0.1 24
[R1-GigabitEthernet0/0/2]un shu
Info: Interface GigabitEthernet0/0/2 is not shutdown.
[R1-GigabitEthernet0/0/2]int g0/0/0
[R1-GigabitEthernet0/0/0]ip add 12.0.0.1 24
[R1-GigabitEthernet0/0/0]un shu
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[R1-GigabitEthernet0/0/0]q
[R1]ip rou
[R1]ip route-static 15.0.0.0 24 12.0.0.2
[R1]

R2配置
The device is running!

<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]un in en
Info: Information center is disabled.
[Huawei]us
[Huawei]user-in
[Huawei]user-interface con
[Huawei]user-interface console 0
[Huawei-ui-console0]i
[Huawei-ui-console0]idle-timeout 0 0
[Huawei-ui-console0]q
[Huawei]sys R2
[R2]dh
[R2]dhcp en
Info: The operation may take a few seconds. Please wait for a moment.done.
[R2]int g0/0/0
[R2-GigabitEthernet0/0/0]ip add 12.0.0.2 24
[R2-GigabitEthernet0/0/0]un shu
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[R2-GigabitEthernet0/0/0]int g0/0/1
[R2-GigabitEthernet0/0/1]ip add 15.0.0.1 24
[R2-GigabitEthernet0/0/1]un shu
Info: Interface GigabitEthernet0/0/1 is not shutdown.
[R2-GigabitEthernet0/0/1]dh
[R2-GigabitEthernet0/0/1]dhcp se
[R2-GigabitEthernet0/0/1]dhcp select re
[R2-GigabitEthernet0/0/1]dhcp select relay
[R2-GigabitEthernet0/0/1]dh
[R2-GigabitEthernet0/0/1]dhcp re
[R2-GigabitEthernet0/0/1]dhcp relay se
[R2-GigabitEthernet0/0/1]dhcp relay server-ip 14.0.0.2
[R2-GigabitEthernet0/0/1]q
[R2]ip rou
[R2]ip route-static 0.0.0.0 0.0.0.0 12.0.0.1
[R2]

R3配置
The device is running!

<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]un in en
Info: Information center is disabled.
[Huawei]us
[Huawei]user-in
[Huawei]user-interface con
[Huawei]user-interface console 0
[Huawei-ui-console0]i
[Huawei-ui-console0]idle-timeout 0 0
[Huawei-ui-console0]
[Huawei-ui-console0]q
[Huawei]sys R3
[R3]
[R3]dh
[R3]dhcp en
Info: The operation may take a few seconds. Please wait for a moment.done.
[R3]int g0/0/0
[R3-GigabitEthernet0/0/0]ip add 14.0.0.2 24
[R3-GigabitEthernet0/0/0]un shu
Info: Interface GigabitEthernet0/0/0 is not shutdown.
[R3-GigabitEthernet0/0/0]q
[R3]ip pool dhcp15
Info: It's successful to create an IP address pool.
[R3-ip-pool-dhcp15]netw
[R3-ip-pool-dhcp15]network 15.0.0.0 ma
[R3-ip-pool-dhcp15]network 15.0.0.0 mask 24
[R3-ip-pool-dhcp15]ga
[R3-ip-pool-dhcp15]gateway-list 15.0.0.1
[R3-ip-pool-dhcp15]dn
[R3-ip-pool-dhcp15]dns-list 8.8.8.8 2.2.2.2
[R3-ip-pool-dhcp15]q
[R3]ip pool dhcp10
Info: It's successful to create an IP address pool.
[R3-ip-pool-dhcp10]netw
[R3-ip-pool-dhcp10]network 192.168.10.0 ma
[R3-ip-pool-dhcp10]network 192.168.10.0 mask 24
[R3-ip-pool-dhcp10]gat
[R3-ip-pool-dhcp10]gateway-list 192.168.10.1
[R3-ip-pool-dhcp10]dns
[R3-ip-pool-dhcp10]dns-list 8.8.8.8 2.2.2.2
[R3-ip-pool-dhcp10]q
[R3]ip pool dhcp20
Info: It's successful to create an IP address pool.
[R3-ip-pool-dhcp20]net
[R3-ip-pool-dhcp20]netw
[R3-ip-pool-dhcp20]network 192.168.20.0 ma
[R3-ip-pool-dhcp20]network 192.168.20.0 mask 24
[R3-ip-pool-dhcp20]gat
[R3-ip-pool-dhcp20]gateway-list 192.168.20.1
[R3-ip-pool-dhcp20]dns
[R3-ip-pool-dhcp20]dns-list 8.8.8.8 2.2.2.2
[R3-ip-pool-dhcp20]int g0/0/0
[R3-GigabitEthernet0/0/0]dh
[R3-GigabitEthernet0/0/0]dhcp se
[R3-GigabitEthernet0/0/0]dhcp selectgl
[R3-GigabitEthernet0/0/0]dhcp select gl
[R3-GigabitEthernet0/0/0]dhcp select global
[R3-GigabitEthernet0/0/0]q
[R3]ip rou
[R3]ip route-
[R3]ip route-static 0.0.0.0 0.0.0.0 14.0.0.1
[R3]


**实验结果截图**
::: hljs-center

![DHCP1.png](https://s2.51cto.com/images/20211021/1634785282144052.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

:::
::: hljs-center

![DHCP2.png](https://s2.51cto.com/images/20211021/1634785290326968.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

:::
::: hljs-center

![DHCP3.png](https://s2.51cto.com/images/20211021/1634785297702990.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

:::
::: hljs-center

![DHCP4.png](https://s2.51cto.com/images/20211021/1634785304199976.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

:::