了解DHCP服务器路由器配置
网络管理员通过使用DHCP服务器及其协议自动配置和分配IP地址给网络设备。专用服务器是可扩展的,易于管理,但对于一个组织来说,在网络中的每一个点都有一个服务器是很昂贵的。在这种情况下,我们可以在网络上配置路由器来提供DHCP服务,而不需要购买服务器。
本文将介绍如何使用网络上的路由器作为DHCP服务器,为各种DHCP客户分配和管理IP地址。它还将使读者了解如何配置路由器,使其能够描述DHCP服务器的功能。
先决条件
读者必须考虑到以下几点。
- 读者应该知道基本的网络概念。
- 对如何使用任何一种网络模拟工具配置网络有良好的了解。本文适合想知道如何使用Cisco packet tracer网络模拟器工具的读者。
术语
-
DHCP Server是一个网络服务器,用于在广泛的网络上动态租赁IPv4地址给主机,减少网络工作人员的负担,并几乎消除了输入错误。它依靠动态主机配置协议(DHCP)来响应DHCP客户端的广播查询。 -
DHCP Clients- 这些是网络上的主机/设备,通过DHCP协议从服务器获得IP地址。 -
Excluded IP Addresses- 这些是专门保留给关键主机的地址,如默认网关、DNS-服务器。排除一些IP地址可以防止DHCP客户被分配一个已经被网络上其他关键主机使用的IP地址,防止IP地址重复。 -
DHCP POOL- DHCP池作为网络子网的内置服务器,在其中为客户分配地址。这些池子最多可以支持2048个地址。
配置Cisco IOS DHCPv4服务器的步骤
考虑一下下面的本地网络连接。

第1步:路由器配置
我们通过静态分配IPv4地址给各个接口来配置作为DHCP服务器使用的路由器。
Router>en
Router#config terminal
Router(config)#hostname R1 !changing our hostname
R1(config)#interface g0/0 !specifying which interface to assign ip address
R1(config-if)#ip address 10.0.0.1 255.0.0.0 !assigning ip address to interface g0/0
R1(config-if)#no shutdown !activating the interface
R1(config-if)#exit
R1(config)#interface g0/1
R1(config-if)#ip address 192.168.1.100 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#
R1(config-if)#exit
R1(config)#do write
Router>en
Router#config terminal
Router(config)#hostname R2
R2(config)#interface g0/0
R2(config-if)#ip address 10.0.0.2 255.0.0.0
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#interface g0/1
R2(config-if)#ip address 193.168.2.1 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#do write
Building configuration...
我们现在在R1和R2上都配置了带有IP地址的接口。
R1#show ip interface brief !shows interface status
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 10.0.0.1 YES manual up up
GigabitEthernet0/1 192.168.1.100 YES manual up up
Vlan1 unassigned YES unset administratively down down
R2#show ip interface brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 10.0.0.2 YES manual up up
GigabitEthernet0/1 193.168.2.1 YES manual up up
Vlan1 unassigned YES unset administratively down down
第2步:创建一个DHCP服务器池
使用ip dhcp pool 命令和池的名称,在路由器R1上创建两个池POOL-A,在路由器R2上创建两个池POOL-2。这包括配置网络地址、默认网关和DNS服务器的IP地址。
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip dhcp pool POOL-A !pool creation
R1(dhcp-config)#? !get more details about pools
default-router Default routers
dns-server Set name server
domain-name Domain name
exit Exit from DHCP pool configuration mode
network Network number and mask
no Negate a command or set its defaults
option Raw DHCP options
R1(dhcp-config)#default-router 192.168.1.100 !ip address of default router
R1(dhcp-config)#dns-server 192.168.1.50 !ip address of dns-server
R1(dhcp-config)#network 192.168.1.0 255.255.255.0 !ip address of the default gateway/network
R1(dhcp-config)#exit
R1(config)#
R1(config)#do write !saving the configurations
Building configuration...
[OK]
R2(config)#
R2(config)#ip dhcp pool POOL-B
R2(dhcp-config)#?
default-router Default routers
dns-server Set name server
domain-name Domain name
exit Exit from DHCP pool configuration mode
network Network number and mask
no Negate a command or set its defaults
option Raw DHCP options
R2(dhcp-config)#default-router 192.168.2.100
R2(dhcp-config)#network 192.168.2.0 255.255.255.0
R2(dhcp-config)#dns-server 192.168.2.50
R2(dhcp-config)#exit
R2(config)#do write
Building configuration...
第3步:排除IP地址
在我们的网络中,我们必须排除R1和R2接口上的IP地址,连同其他9个地址,用于静态分配给其他设备,如服务器和设备管理接口。
R1(config)#ip dhcp excluded-address 192.168.1.100
R1(config)#ip dhcp excluded-address 192.168.1.1 192.168.1.10
R1(config)#ip dhcp excluded-address 192.168.1.50
R1(config)#do write
R2(config)#
R2(config)#ip dhcp excluded-address 192.168.2.100
R2(config)#ip dhcp excluded-address 192.168.2.1 192.168.2.10
R2(config)#ip dhcp excluded-address 192.168.2.50
R2(config)#exit
DHCP服务器验证
为了确认我们的DHCP服务器是在两台路由器上配置的,我们使用了show running-config | section DHCP 命令。输出如下,证实了路由器现在可以执行动态分配IP地址给网络客户端的功能。
R2#show running-config | section dhcp
ip dhcp excluded-address 192.168.2.100
ip dhcp excluded-address 192.168.2.1 192.168.2.10
ip dhcp excluded-address 192.168.2.50
ip dhcp pool POOL-B
network 192.168.2.0 255.255.255.0
default-router 192.168.2.100
dns-server 192.168.2.50
注意:可以在全局配置模式下用
no service dhcp命令禁用dhcp服务器,用service dhcp命令重新启用。
DHCP服务器路由器测试
一旦我们确认路由器已经配置为DHCP服务器,我们就可以通过尝试给客户分配IP地址来测试其功能。在我们的网络中,我们有4台PC作为客户。在配置之前,被配置为DHCP服务器的路由器不能为PC动态分配地址,DHCP失败,如图所示。

配置完成后,我们再次尝试在IP配置界面窗口中选择DHCP选项,为每个客户机分配一个IP地址。DHCP成功运行。这表明我们的路由器现在已经启用了DHCP。

我们还可以尝试用ping 命令来测试同一子网中两个客户端之间的通信。我们的网络,PC5可以使用分配的IP地址与PC4通信,如图所示。

注意:从分配的IP地址可以看出,DHCP服务器分配的地址从.11开始,不包括.1到.10和.50的IP地址,这些地址是被排除在外的。
结论
正如我们所看到的,网络路由器在正确配置的情况下可以通过提供DHCP服务器功能为组织节省购买专用服务器的费用。这些信息至关重要,将使学习者掌握如何配置路由器以消除静态分配IP地址的负担。
总结一下。
-
我们了解了什么是DHCP服务器。
-
我们探讨了如何使用路由器充当DHCP服务器。
-
我们配置了一个网络,验证并配置了路由器来执行DHCP服务器的功能。
-
我们学习了如何启用和禁用网络上的DHCP服务器。