MHuiG

「看庭前花开花落,望天上云卷云舒」

DHCP

# DHCP

DHCP: 动态主机配置协议，可以在网络为终端设备配置上网参数 (IP 地址，掩码，网关，DNS 等等), 可以减少管理员的工作量.

应用层协议.

## DHCP 工作原理

DHCP 采用 `C/S` 架构 (客户端 / 服务端)

- 1. 路由器，三层交换机，防火墙，AC.
- 2. DHCP Server 的 IP 地址需要管理员手工配置.

## DHCP 报文

`DHCP Discover`: 用于发现网络中的 DHCP 服务器.

`DHCP offer`: 用于提供终端设备的上网参数.

`DHCP Request`: 客户端请求配置确认，或续借租期.

`DHCP ACK`: 服务器对 DHCP Request 进行确认回复.

`DHCP NAK`: 服务器对 DHCP Request 进行拒绝.

`DHCP Release`: 客户端要释放地址时通知 DHCP 服务器的报文.

`DHCP Decline`: 当客户端发现服务器分配的地址发现冲突，会通过此报文通知 DHCP 服务器，并重新向服务器申请地址.

## DHCP 工作过程

    PC                       Server
    ===     Discover(广播)     ==>
    <==     Offer(单播/广播)   ===
    ===     Request (广播)     ==>
    <==    ACK/NAK(单播/广播)  ===

当一个刚接入网络的终端设备还没分配 IP 地址 (0.0.0.0/32) 的时候，会通过广播来发送 DHCP Discover 报文来寻找网络中的服务器.

当网络内存在多台 DHCP 服务器接收到客户端发来的 Discover, 会从尚未分配的 IP 地址中选一个分配给设备，然后通过单播形式发送 offer 报文给终端设备，该报文中包含上网参数信息.

终端设备收到了 DHCP 服务器发来的 DHCP offer 报文，选择优先到达的一个。随后广播一个 Request 报文，目的是告诉所有的 DHCP 服务器，自己将使用哪一个 DHCP 服务器所提供的地址，以便于其他的服务器撤销自己的 offer 报文.

DHCP 服务器收到终端设备发来的 DHCP Request, 返回给客户端一个 ACK, 表示确认，并将参数信息放入到报文中发送给终端设备.

当终端设备收到 DHCP 服务器发来的 ACK 后，会向网络内发送一个免费 ARP 来确认网络内是否存在地址冲突，如果没有冲突，则使用该地址，如果发生冲突，则向 DHCP 服务器发送一个 Decline 报文，拒绝此 IP 地址，并向服务器重新申请地址.

DHCP 客户端在确定不使用 IP 地址信息时会发送 Release 消息来释放当前地址.

- 系统关机或重启
- 重置网卡
- 管理员通过命令手工释放

    ipconfig /renew      // 重新向DHCP服务器申请上网参数
    ipconfig /release    // 释放当前PC的上网参数

DHCP Discover 消息的目的地址通常是广播地址 (Broadcast Address). 广播地址是一个特殊的 IP 地址，用于将消息发送到网络中的所有设备。在 IPv4 网络中，广播地址通常是目标 IP 地址为 `255.255.255.255` 的消息.

## DHCP 续租过程

1. 当客户端的租期到达 `50%` 时会主动发送一个单播的 Request 进行 DHCP 续租.

2. 如果 Server 检查 Request 消息没有任何问题，直接回复 ACK 确认，并刷新租期.

3. 如果 50% 没有回应，会继续使用该 IP 地址，直到租期的 `87.5%`, 客户端广播发送 Request 进行续租，如果有 DHCP Server 回应可以进行续租，如果没有回应，将地址租期结束后重新进行租借过程.

## DHCP 全局地址池

    <Huawei>
    <Huawei>
    <Huawei>sy
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sy R1
    [R1]dhcp enable
    Info: The operation may take a few seconds. Please wait for a moment.done.
    [R1]ip pool ?
      STRING<1-64>  Pool name
    [R1]ip pool PC
    Info: It's successful to create an IP address pool.
    [R1-ip-pool-PC]network ?
      IP_ADDR<X.X.X.X>  IP address
    [R1-ip-pool-PC]network 192.168.1.0 mask 24
    [R1-ip-pool-PC]display this
    [V200R003C00]
    #
    ip pool PC
     network 192.168.1.0 mask 255.255.255.0
    #
    return
    [R1-ip-pool-PC]q
    [R1]int g0/0/0
    [R1-GigabitEthernet0/0/0]ip address 192.168.1.254 24
    Dec 19 2023 09:50:57-08:00 R1 %%01IFNET/4/LINK_STATE(l)[1]:The line protocol IP
    on the interface GigabitEthernet0/0/0 has entered the UP state.
    [R1-GigabitEthernet0/0/0]display this
    [V200R003C00]
    #
    interface GigabitEthernet0/0/0
     ip address 192.168.1.254 255.255.255.0
    #
    return
    [R1-GigabitEthernet0/0/0]ip pool PC
    [R1-ip-pool-PC]ga
    [R1-ip-pool-PC]gateway-list  ?
      IP_ADDR<X.X.X.X>  Gateway's IP address
    [R1-ip-pool-PC]gateway-list  192.168.1.254
    [R1-ip-pool-PC]dns-list ?
      IP_ADDR<X.X.X.X>  IP address
    [R1-ip-pool-PC]dns-list 114.114.114.114
    [R1-ip-pool-PC]dis th
    [V200R003C00]
    #
    ip pool PC
     gateway-list 192.168.1.254
     network 192.168.1.0 mask 255.255.255.0
     dns-list 114.114.114.114
    #
    return
    [R1-ip-pool-PC]lease day 0 hour 0 minute 30
    [R1-ip-pool-PC]dis this
    [V200R003C00]
    #
    ip pool PC
     gateway-list 192.168.1.254
     network 192.168.1.0 mask 255.255.255.0
     lease day 0 hour 0 minute 30
     dns-list 114.114.114.114
    #
    return
    [R1-ip-pool-PC]q
    [R1]int g0/0/0
    [R1-GigabitEthernet0/0/0]dhcp select global

    <Huawei>system-view
    [Huawei]sysname R1
    [R1]dhcp enable // 开启 DHCP 功能
    [R1]ip pool PC // 创建地址池
    [R1-ip-pool-PC]gateway-list 192.168.1.254  // 设置网关
    [R1-ip-pool-PC]network 192.168.1.0 mask 255.255.255.0 // 设置地址范围
    [R1-ip-pool-PC]lease day 0 hour 0 minute 30 // 设置租期
    [R1-ip-pool-PC]dns-list 114.114.114.114 // 设置DNS地址
    [R1-ip-pool-PC]excluded-ip-address 192.168.1.200 192.168.1.253  // 排除地址池中的地址
    [R1-ip-pool-PC]static-bind ip-address IP地址 mac-address xxxx-xxxx-xxxx(mac地址)  // 静态绑定某个主机使用某个IP地址

    [R1]interface GigabitEthernet0/0/0  // 进入接口视图
    [R1-GigabitEthernet0/0/0]ip address 192.168.1.254 255.255.255.0 // 配置 IP 地址
    [R1-GigabitEthernet0/0/0]dhcp select global // 接口下使能全局地址池

    [R1]display current-configuration // 查看配置

## DHCP 接口地址池

    <Huawei>sy
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sy R1
    [R1]int g0/0/0
    [R1-GigabitEthernet0/0/0]ip add 192.168.1.254 24
    [R1-GigabitEthernet0/0/0]
    Dec 19 2023 11:00:18-08:00 R1 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP
    on the interface GigabitEthernet0/0/0 has entered the UP state.
    [R1-GigabitEthernet0/0/0]q
    [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 select interface
    [R1-GigabitEthernet0/0/0]dis th
    [V200R003C00]
    #
    interface GigabitEthernet0/0/0
     ip address 192.168.1.254 255.255.255.0
     dhcp select interface
    #
    return
    [R1-GigabitEthernet0/0/0]dhcp server dns-list 114.114.114.114
    [R1-GigabitEthernet0/0/0]dhcp server lease day 0 hour 0 min 30

    <R1>sy
    Enter system view, return user view with Ctrl+Z.
    [R1]int g0/0/0
    [R1-GigabitEthernet0/0/0]dhcp server ?
      dns-list             Configure DNS servers
      domain-name          Configure domain name
      excluded-ip-address  Mark disable IP addresses
      import               Imports the following network configuration parameters
                           from a central server into local ip pool database: domain
                           name, dns server and netbios server.
      lease                Configure the lease of the IP pool
      nbns-list            Configure the windows's netbios name servers
      netbios-type         Netbios node type
      next-server          The address of the server to use in the next step of the
                           client's bootstrap process.
      option               Configure the DHCP options
      option121            DHCP option 121
      option184            DHCP option 184
      recycle              Recycle IP address
      static-bind          Static bind
    [R1-GigabitEthernet0/0/0]dhcp server st
    [R1-GigabitEthernet0/0/0]dhcp server static-bind ?
      ip-address  IP address for static bind
    [R1-GigabitEthernet0/0/0]dhcp server static-bind ip-address 192.168.1.51 ?
      mac-address  MAC address for static bind
    [R1-GigabitEthernet0/0/0]dhcp server static-bind ip-address 192.168.1.51 mac-add
    ress ?
      MAC_ADDR<XXXX-XXXX-XXXX>  MAC address
    [R1-GigabitEthernet0/0/0]dhcp server static-bind ip-address 192.168.1.51 mac-add
    ress 5489-981A-18A3
    [R1-GigabitEthernet0/0/0]

    <Huawei>sy
    [Huawei]sy R1
    [R1]dhcp enable // 开启 DHCP 功能
    [R1]int g0/0/0 // 进入接口视图
    [R1-GigabitEthernet0/0/0]ip add 192.168.1.254 24 // 配置 IP 地址的掩码
    [R1-GigabitEthernet0/0/0]dhcp select interface // 接口下使能接口地址池
    [R1-GigabitEthernet0/0/0]dhcp server dns-list 114.114.114.114 // 设置 DNS
    [R1-GigabitEthernet0/0/0]dhcp server lease day 0 hour 0 min 30 // 设置租期
    [R1-GigabitEthernet0/0/0]dhcp server excluded-ip-address  192.168.1.10 192.168.1.50 // 排除地址池中的地址
    [R1-GigabitEthernet0/0/0]dhcp server static-bind ip-address 192.168.1.51 mac-address 5489-981A-18A3 // 静态绑定某个主机使用某个IP地址

## DHCP 中继

PC 的 DHCP 广播报文单播发给 DHCP 服务器.

R1

    <Huawei>sy
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sy R1
    [R1]int g0/0/1
    [R1-GigabitEthernet0/0/1]ip add 192.168.1.254 24
    [R1-GigabitEthernet0/0/1]int g0/0/0
    [R1-GigabitEthernet0/0/0]ip add 12.1.1.1 24
    [R1-GigabitEthernet0/0/0]
    [R1]dhcp enable
    Info: The operation may take a few seconds. Please wait for a moment.done.
    [R1]int g0/0/1
    [R1-GigabitEthernet0/0/1]dhcp select ?
      global     Local server
      interface  Interface server pool
      relay      DHCP relay
    [R1-GigabitEthernet0/0/1]dhcp select relay
    [R1-GigabitEthernet0/0/1]dhcp relay server-ip 12.1.1.2
    [R1-GigabitEthernet0/0/1]

R2

    <Huawei>sy
    Enter system view, return user view with Ctrl+Z.
    [Huawei]sy R2
    [R2]int g0/0/0
    [R2-GigabitEthernet0/0/0]ip add 12.1.1.2 24
    [R2-GigabitEthernet0/0/0]q
    [R2]ping 192.168.1.254
      PING 192.168.1.254: 56  data bytes, press CTRL_C to break
        Request time out
        Request time out
        Request time out

      --- 192.168.1.254 ping statistics ---
        3 packet(s) transmitted
        0 packet(s) received
        100.00% packet loss

    [R2]ip route-static 192.168.1.0 24 12.1.1.1
    [R2]ping 192.168.1.254
      PING 192.168.1.254: 56  data bytes, press CTRL_C to break
        Reply from 192.168.1.254: bytes=56 Sequence=1 ttl=255 time=110 ms
        Reply from 192.168.1.254: bytes=56 Sequence=2 ttl=255 time=20 ms
        Reply from 192.168.1.254: bytes=56 Sequence=3 ttl=255 time=30 ms
        Reply from 192.168.1.254: bytes=56 Sequence=4 ttl=255 time=40 ms
        Reply from 192.168.1.254: bytes=56 Sequence=5 ttl=255 time=10 ms

      --- 192.168.1.254 ping statistics ---
        5 packet(s) transmitted
        5 packet(s) received
        0.00% packet loss
        round-trip min/avg/max = 10/42/110 ms

    [R2]ip pool PC
    [R2-ip-pool-PC]network 192.168.1.0 mask 24
    [R2-ip-pool-PC]gateway-list 192.168.1.254
    [R2-ip-pool-PC]dns-list 114.114.114.114
    [R2-ip-pool-PC]lease day 0 hour 0 min 10
    [R2-ip-pool-PC]excluded-ip-address 192.168.1.100 192.168.1.253

    [R2-ip-pool-PC]dis th
    [V200R003C00]
    #
    ip pool PC
     gateway-list 192.168.1.254
     network 192.168.1.0 mask 255.255.255.0
     excluded-ip-address 192.168.1.100 192.168.1.253
     lease day 0 hour 0 minute 10
     dns-list 114.114.114.114
    #
    return
    [R2-ip-pool-PC]q
    [R2]dhcp enable
    Info: The operation may take a few seconds. Please wait for a moment.done.
    [R2]int g0/0/0
    [R2-GigabitEthernet0/0/0]dhcp select global
    [R2-GigabitEthernet0/0/0]

R1

    #
     sysname R1
    #
    dhcp enable
    #
    interface GigabitEthernet0/0/0
     ip address 12.1.1.1 255.255.255.0
    #
    interface GigabitEthernet0/0/1
     ip address 192.168.1.254 255.255.255.0
     dhcp select relay // 接口下开启 DHCP 中继功能
     dhcp relay server-ip 12.1.1.2 // 告知中继中的DHCP服务器的IP地址
    #

R2

    #
     sysname R2
    #
    dhcp enable
    #
    ip pool PC
     gateway-list 192.168.1.254
     network 192.168.1.0 mask 255.255.255.0
     excluded-ip-address 192.168.1.100 192.168.1.253
     lease day 0 hour 0 minute 10
     dns-list 114.114.114.114
    #
    interface GigabitEthernet0/0/0
     ip address 12.1.1.2 255.255.255.0
     dhcp select global
    #
    #
    ip route-static 192.168.1.0 255.255.255.0 12.1.1.1
    #

传输层协议

传输层协议 传输层协议 什么是端到端端到端是网络连接。网络要通信，必须建立连接，不管多远，中间有多少设备，都必须在源和目的之间建立连接. 端到端是逻辑连接，这条路可能经过了很复杂的物理...

数据转发过程

数据转发过程 数据转发过程 数据的封装和解封装传输层设备使用传输层协议进行报文封装，填充源端口和目的端口，标识位，序列号等等，之后交给网络层协议进行后续的封装. 网络层在网络层进行封...

推荐阅读
实现VLAN间通信 数据转发过程 IP路由基础 远程登录 ACL原理和配置 OSPF基础

留言区

Notes
数据通信网络
Are You A Robot?