浅谈对DHCP的理解

来源:互联网 发布:蓝果网络 编辑:程序博客网 时间:2024/06/05 14:09
DHCP——动态主机配置协议,主要目的是为了方便我们的主机IP地址的配置,如果网络中存在大量的主机时,可通过部署DHCP协议,由DHCP服务器分配可用地址给主机。
现如今,DHCP在我们的网络中使用的非常广泛,最常见的便是家用的无线路由器,不管是通过无线信号接入还是通过有线网络接入,基本上主机等终端都不需要手动配置IP,均由无线路由器下发。
DHCP的配置场景也如上所述,适用于网络中主机地址自动配置,配置方式也非常简单。主要由DHCP Server以及DHCP Client(主机)两部分构成。重点在于DHCP服务器的配置,在我们的组网中,普遍使用网关作为DHCP Server,因为这样流量出口就在网关上,可以非常方便有效的控制数据流量。对于DHCP Server端的配置主要如下:(以思科设备为例)

DHCPserver(config)#int f0/0
DHCPserver(config-if)#ip add 192.168.1.254 255.255.255.0
DHCPserver(config-if)#no shut
DHCPserver(config-if)#exit
DHCPserver(config)#ip dhcp pool pc
DHCPserver(dhcp-config)#network 192.168.1.0 255.255.255.0
DHCPserver(dhcp-config)#default-router 192.168.1.254
DHCPserver(dhcp-config)#dns-server 114.114.114.114
DHCPserver(dhcp-config)#lease 0 8
DHCPserver(dhcp-config)#exit
DHCPserver(config)#ip dhcp excluded-address 192.168.1.250 192.168.1.254
DHCPserver(config)#exit
DHCPserver#
主机端基本不需要任何配置,直接打开接口,然后开启DHCP自动获取地址即可:
Host(config)#int f0/0
Host(config-if)#no shut
Host(config-if)#ip add dhcp
与此同时,打开Server端的debug功能以及使用wireshark简单分析一下DHCP的过程。
DHCPserver#debug ip dhcp server events
DHCPserver#debug ip dhcp server packet
以下是debug的信息:
DHCPserver#
*Mar  1 00:03:25.355: DHCPD: Sending notification of DISCOVER:
*Mar  1 00:03:25.359:   DHCPD: htype 1 chaddr cc03.333c.0000
*Mar  1 00:03:25.359:   DHCPD: remote id 020a0000c0a801fe00000000
*Mar  1 00:03:25.359:   DHCPD: circuit id 00000000
*Mar  1 00:03:25.359: DHCPD: DHCPDISCOVER received from client 0063.6973.636f.2d63.6330.332e.3333.3363.2e30.3030.302d.4661.302f.30 on interface FastEthernet0/0.
*Mar  1 00:03:25.359: DHCPD: Seeing if there is an internally specified pool class:
*Mar  1 00:03:25.363:   DHCPD: htype 1 chaddr cc03.333c.0000
*Mar  1 00:03:25.363:   DHCPD: remote id 020a0000c0a801fe00000000
*Mar  1 00:03:25.363:   DHCPD: circuit id 00000000
DHCPserver#
*Mar  1 00:03:25.363: DHCPD: Allocate an address without class information (192.168.1.0)
DHCPserver#
*Mar  1 00:03:27.363: DHCPD: Adding binding to radix tree (192.168.1.1)
*Mar  1 00:03:27.363: DHCPD: Adding binding to hash tree
*Mar  1 00:03:27.363: DHCPD: assigned IP address 192.168.1.1 to client 0063.6973.636f.2d63.6330.332e.3333.3363.2e30.3030.302d.4661.302f.30.
*Mar  1 00:03:27.363: DHCPD: Sending DHCPOFFER to client 0063.6973.636f.2d63.6330.332e.3333.3363.2e30.3030.302d.4661.302f.30 (192.168.1.1).
*Mar  1 00:03:27.367: DHCPD: broadcasting BOOTREPLY to client cc03.333c.0000.
*Mar  1 00:03:27.419: DHCPD: DHCPREQUEST received from client 0063.6973.636f.2d63.6330.332e.3333.3363.2e30.3030.302d.4661.302f.30.
*Mar  1 00:03:27.423: DHCPD: Sending notification of ASSIGNMENT:
DHCPserver#
*Mar  1 00:03:27.423:  DHCPD: address 192.168.1.1 mask 255.255.255.0
*Mar  1 00:03:27.423:   DHCPD: htype 1 chaddr cc03.333c.0000
*Mar  1 00:03:27.423:   DHCPD: lease time remaining (secs) = 28800
*Mar  1 00:03:27.427: DHCPD: No default domain to append - abort update
*Mar  1 00:03:27.427: DHCPD: Sending DHCPACK to client 0063.6973.636f.2d63.6330.332e.3333.3363.2e30.3030.302d.4661.302f.30 (192.168.1.1).
*Mar  1 00:03:27.427: DHCPD: broadcasting BOOTREPLY to client cc03.333c.0000.
DHCPserver#
从上面的debug信息上可以大致看出,整个过程主要分为四种数据包的发送:DHCPDISCOVER/DHCPOFFER/DHCPREQUEST/DHCPACK。同时我们再通过抓包信息来看看具体的交互过程:

1. 首先主机广播发送DHCPDISCOVER,去寻找DHCP服务器;
2. DHCPserver收到这个DHCPDISCOVER后,会去查一下自己可分配的网段,同时按照顺序选择一个IP地址,先测试网络中是否该地址已经被使用,发送ARP包去测试,若没有reply包则证明此IP无人使用,可以分配,于是DHCPserver发送DHCPOFFER告知Host,注意此时还是以广播包的形式;
3. 主机收到后同样回复一个DHCPREQUEST给DHCPserver,因为如果存在多台DHCPserver的情况时,会存在Host收到多个OFFER的情况,所以此时会选择最先收到的回复REQUEST;
4.DHCPserver收到后发送最终的DHCPACK给这台主机,告知可以使用以及租期等信息,主机收到后,正常使用该IP地址,同时还会发送一个无故ARP来测试一下这个地址是否是唯一的。
*Mar  1 00:01:45.875: %DHCP-6-ADDRESS_ASSIGN: Interface FastEthernet0/0 assigned DHCP address 192.168.1.1, mask 255.255.255.0, hostname Host
以上就是整个DHCP的数据包交互流程,当然DHCP的用法也非常的多,比如如何跨网段中继动态获取地址等,这些都需要DHCP数据包中的字段去支持,来标识主机不同的需求,以分配合适可用的IP地址。