凌峰创科服务平台

Cisco DHCP服务器配置步骤有哪些?

目录

  1. DHCP 基础概念
  2. 基本 DHCP 服务器配置
  3. 配置排除静态 IP 地址
  4. 配置多个地址池
  5. 配置 DHCP 中继 (Helper Address)
  6. 高级 DHCP 配置
    • 排除地址
    • 配置多个地址池
    • 配置 DHCP 中继
    • 高级 DHCP 配置
  7. 配置示例
  8. 验证和排错命令

DHCP 基础概念

在配置之前,了解 DHCP 的工作流程很重要:

Cisco DHCP服务器配置步骤有哪些?-图1
(图片来源网络,侵删)
  1. Discover (发现): 客户端在网络上广播一条消息,寻找可用的 DHCP 服务器。
  2. Offer (提供): DHCP 服务器收到请求后,从一个地址池中提供一个可用的 IP 地址、子网掩码、默认网关等信息,并广播给客户端。
  3. Request (请求): 客户端选择接收第一个服务器的 Offer,并广播一条消息正式请求该 IP 地址。
  4. ACK (确认): DHCP 服务器确认该 IP 地址已分配给客户端,并发送一个 ACK 消息,客户端配置网络并开始使用。

在 Cisco 路由器或交换机上,我们可以将其配置为 DHCP 服务器,为局域网内的设备自动分配 IP 地址。


基本 DHCP 服务器配置

这是最核心的配置,用于创建一个地址池并为特定网段提供服务。

配置步骤:

  1. 进入全局配置模式

    Cisco DHCP服务器配置步骤有哪些?-图2
    (图片来源网络,侵删)
    Router> enable
    Router# configure terminal
  2. 创建一个 DHCP 地址池 使用 ip dhcp pool <pool-name> 命令创建并进入地址池配置模式。<pool-name> 是一个自定义的名称,用于标识这个地址池。

    Router(config)# ip dhcp LAN-POOL-1
    Router(dhcp-config)#
  3. 定义网络地址和默认网关 在地址池配置模式下,使用 network 命令指定你要提供 DHCP 服务的网络地址和子网掩码。

    Router(dhcp-config)# network 192.168.1.0 255.255.255.0
  4. 定义默认网关 使用 default-router 命令指定客户端的默认网关地址(通常是路由器的 LAN 接口地址)。

    Router(dhcp-config)# default-router 192.168.1.1
  5. 定义 DNS 服务器 使用 dns-server 命令指定客户端要使用的 DNS 服务器地址,可以指定一个或多个。

    Cisco DHCP服务器配置步骤有哪些?-图3
    (图片来源网络,侵删)
    Router(dhcp-config)# dns-server 8.8.8.8 8.8.4.4
  6. (可选) 定义租期 使用 lease 命令定义 IP 地址的租用时间,默认是 24 小时。

    • lease 7 : 7 天
    • lease 8 12 : 8 天 12 小时
    • lease 0 0 2 : 2 小时
    • lease infinite : 无限租期(不推荐,可能导致地址耗尽)
    Router(dhcp-config)# lease 7
  7. (可选) 启用 DHCP 服务 默认情况下,只要配置了地址池,DHCP 服务就会在所有活动接口上启用,但你可以用 ip dhcp excluded-address 命令手动排除一些地址(见下一节)。


配置排除静态 IP 地址

在 DHCP 分配地址之前,你需要为网络设备(如服务器、打印机、路由器接口)保留一些静态 IP 地址。ip dhcp excluded-address 命令用于将这些地址从 DHCP 分配池中排除。

语法: ip dhcp excluded-address <start-ip> [<end-ip>]

  • 排除单个地址:

    Router(config)# ip dhcp excluded-address 192.168.1.1

    这会排除 168.1.1,使其不会被分配给客户端。

  • 排除一个地址范围:

    Router(config)# ip dhcp excluded-address 192.168.1.1 192.168.1.10

    这会排除 168.1.1168.1.10 的所有地址,可以用于静态分配给多个设备。


配置多个地址池

如果你的路由器连接了多个网络,你需要为每个网络配置一个独立的地址池。

示例:168.1.0/2410.20.0/24 两个网段提供 DHCP 服务。

! 为第一个网络配置
Router(config)# ip dhcp excluded-address 192.168.1.1
Router(config)# ip dhcp pool VLAN10
Router(dhcp-config)# network 192.168.1.0 255.255.255.0
Router(dhcp-config)# default-router 192.168.1.1
Router(dhcp-config)# dns-server 8.8.8.8
! 为第二个网络配置
Router(config)# ip dhcp excluded-address 10.10.20.1
Router(config)# ip dhcp pool VLAN20
Router(dhcp-config)# network 10.10.20.0 255.255.255.0
Router(dhcp-config)# default-router 10.10.20.1
Router(dhcp-config)# dns-server 1.1.1.1

配置 DHCP 中继

当 DHCP 服务器和客户端不在同一个广播域(在不同的 VLAN 或通过三层交换机连接)时,客户端的广播消息无法到达服务器,这时就需要在客户端的网关设备(通常是交换机或路由器)上配置 DHCP 中继,也称为 "Helper Address"。

配置步骤 (在客户端的 VLAN 接口上):

假设 168.1.0/24 网段的客户端要通过 VLAN 10 访问位于 168.100.1 的 DHCP 服务器。

  1. 进入 VLAN 接口配置模式

    Switch(config)# interface vlan 10
  2. 配置 Helper Address 使用 ip helper-address 命令指定 DHCP 服务器的 IP 地址,这个命令会将客户端的 DHCP 广播包转换为单播包,发送到指定的服务器。

    Switch(config-if)# ip helper-address 192.168.100.1
  3. (可选) 指定其他 UDP 服务 ip helper-address 默认会转发多种 UDP 广播服务(端口 67/68 for DHCP, 37 for Time, 49 for TFTP, 69 for TFTP, 123 for NTP, 137/138 for NetBIOS, 514/515 for Printers),如果只需要转发 DHCP,可以使用 ip forward-protocol 命令进行限制。

    Switch(config)# ip forward-protocol udp 67 68

完整配置示例

假设一台 Cisco 2960 交换机连接了两个 VLAN:

  • VLAN 10: 员工网段 168.10.0/24
  • VLAN 20: 访客网段 168.20.0/24

交换机自身将作为这两个 VLAN 的 DHCP 服务器。

! 进入全局配置模式
Switch> enable
Switch# configure terminal
! --- VLAN 10 配置 ---
! 创建 VLAN 10
Switch(config)# vlan 10
Switch(config-vlan)# name Employees
Switch(config-vlan)# exit
! 配置连接员工的接口 (GigabitEthernet0/1)
Switch(config)# interface range gigabitethernet0/1-10
Switch(config-if-range)# switchport mode access
Switch(config-if-range)# switchport access vlan 10
Switch(config-if-range)# exit
! 排除 VLAN 10 的网关地址和服务器地址
Switch(config)# ip dhcp excluded-address 192.168.10.1 192.168.10.10
! 创建 VLAN 10 的 DHCP 地址池
Switch(config)# ip dhcp pool EMP-POOL
Switch(dhcp-config)# network 192.168.10.0 255.255.255.0
Switch(dhcp-config)# default-router 192.168.10.1
Switch(dhcp-config)# dns-server 8.8.8.8
Switch(dhcp-config)# lease 7
! --- VLAN 20 配置 ---
! 创建 VLAN 20
Switch(config)# vlan 20
Switch(config-vlan)# name Guests
Switch(config-vlan)# exit
! 配置连接访客的接口 (GigabitEthernet0/11-20)
Switch(config)# interface range gigabitethernet0/11-20
Switch(config-if-range)# switchport mode access
Switch(config-if-range)# switchport access vlan 20
Switch(config-if-range)# exit
! 排除 VLAN 20 的网关地址
Switch(config)# ip dhcp excluded-address 192.168.20.1
! 创建 VLAN 20 的 DHCP 地址池
Switch(config)# ip dhcp pool GUEST-POOL
Switch(dhcp-config)# network 192.168.20.0 255.255.255.0
Switch(dhcp-config)# default-router 192.168.20.1
Switch(dhcp-config)# dns-server 1.1.1.1
Switch(dhcp-config)# lease 1 ! 访客租期较短,例如1天
! --- SVI (交换虚拟接口) 配置 ---
! 为每个 VLAN 创建 SVI 作为网关
Switch(config)# interface vlan 10
Switch(config-if)# ip address 192.168.10.1 255.255.255.0
Switch(config-if)# no shutdown
Switch(config)# interface vlan 20
Switch(config-if)# ip address 192.168.20.1 255.255.255.0
Switch(config-if)# no shutdown

验证和排错命令

配置完成后,使用以下命令来验证和排查问题。

命令 描述
show ip dhcp conflict 显示所有 DHCP 地址冲突,如果客户端和静态设备 IP 相同,就会出现冲突。
show ip dhcp binding 显示所有已分配的 DHCP 地址租约。
show ip dhcp binding <ip-address> 显示特定 IP 地址的租约详情。
show ip dhcp pool 显示所有 DHCP 地址池的配置信息。
show ip dhcp pool <pool-name> 显示特定地址池的配置和地址使用情况。
show ip dhcp conflict 检测并显示地址冲突。
debug ip server packet 调试模式:显示 DHCP 服务器收到的所有数据包。(慎用,会产生大量日志)
debug ip server packet detail 调试模式:显示更详细的 DHCP 服务器数据包信息。
undebug all 关闭所有调试功能。

排错思路:

  1. 客户端无法获取 IP:

    • 检查客户端是否设置为自动获取 IP (DHCP)。
    • 在客户端使用 ipconfig /renew (Windows) 或 dhclient (Linux)。
    • 在服务器上使用 show ip dhcp binding,看是否有租约。
    • 使用 debug ip server packet 观察是否收到客户端的 Discover 包。
  2. 地址冲突:

    • 客户端可能收到一个 NACK (Negative Acknowledgment) 消息。
    • 立即使用 show ip dhcp conflict 查看冲突的 IP 地址。
    • 找到冲突设备,修改其 IP 为静态或排除该地址。
  3. DHCP 中继不工作:

    • 确认客户端网关接口上已正确配置 ip helper-address <server-ip>
    • 确认服务器上配置了正确的地址池。
    • 使用 debug ip udp 检查 UDP 端口 67/68 的流量是否被转发。

希望这份详细的指南能帮助你成功配置 Cisco DHCP 服务器!

分享:
扫描分享到社交APP
上一篇
下一篇