DHCP服务在企业中的应用

本文详细介绍DHCP服务的基础概念、配置及应用场景,包括单个作用域、超级作用域配置实例及DHCP中继服务的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

DHCP基础部分

DHCP全称 Dynamic Host Configuration Protocol (动态主机配置协议)。
提供主机 IP 地址的动态租用配置、并将其他配置参数分配给合法网络客户的 TCP/IP 服务协议。
DHCP 提供既安全、可靠,又简便的 TCP/IP 网络配置,可避免地址冲突,而且还可以帮助保留网络上的客户 IP 地址的使用。DHCP 使用客户/服务器模型,通过这种模式,DHCP 服务器维持对网络上使用的 IP 地址的集中管理。
这样,支持 DHCP 的客户机就可以向 DHCP 服务器请求和租用 IP 地址,作为它们网络启动过程的一部分。“动态主机配置协议”也被称为 DHCP 。


严格来说,在DHCP的正式标准中支持3种不同方式来指定客户端的IP地址信息:
(1)手动指定IP地(Manual):管理者必须为每个客户端指定一个固定的IP地址,然后在客户端计算机要求租用IP地址时,通过DHCP服务器将此IP地址分配给客户端;
(2)自动指定IP地址(Automatic):IP地址的指定工作完全交给DHCP服务器来决定,而不需要经过管理员的手动输入;
(3)动态指定IP地址(Dynamic):管理员在DHCP服务器中设置一个IP地址范围,然后在客户端提出IP地址租用的要求后,自动指派一个IP地址给客户端,
但与前面两种方式不同的是指派给客户端的IP地址并非是无限制的使用,而客户端使用的时间完全由管理者自行决定。


DHCP的优点:
(1)可以避免在计算机上手动设置状态时所造成的错误,可有效防止地址冲突;
(2)可以大大的节省用于设置ip状态的时间;
(3)机器在子网间移动时,旧的ip地址释放以便再次使用,再次启动客户端时,自动获得新的地址;


DHCP协议的工作原理
 

195417682.png

 

 

根据客户端是否第一次登录网络,DHCP的工作形式会有所不同。 第一次登录的时候:

  寻找 Server

  当 DHCP客户端第一次登录网络的时候,也就是客户发现本机上没有任何IP数据设定,它会向网络发出一个 DHCPDISCOVER 封包。因为客户端还不知道自己属于哪一个网络,所以封包的来源地址会为 0.0.0.0 ,而目的地址则为 255.255.255.255 ,然后再附上 DHCPdiscover 的信息,向网络进行广播。

  在 Windows 的预设情形下,DHCPdiscover 的等待时间预设为 1 秒,也就是当客户端将第一个 DHCPdiscover 封包送出去之后,在 1 秒之内没有得到响应的话,就会进行第二次 DHCPdiscover 广播。若一直得不到响应的情况下,客户端一共会有四次 DHCPdiscover 广播(包括第一次在内),除了第一次会等待 1 秒之外,其余三次的等待时间分别是 9、13、16 秒。如果都没有得到 DHCP服务器的响应,客户端则会显示错误信息,宣告 DHCPdiscover 的失败。之后,基于使用者的选择,系统会继续在 5 分钟之后再重复一次 DHCPdiscover 的过程。

  提供IP租用地址

  当DHCP协议服务器监听到客户端发出的 DHCPdiscover 广播后,它会从那些还没有租出的地址范围内,选择最前面的空置IP,连同其它TCP/IP设定,响应给客户端一个DHCPOFFER封包。

  由于客户端在开始的时候还没有IP地址,所以在其 DHCPdiscover 封包内会带有其 MAC 地址信息,并且有一个 XID 编号来辨别该封包,DHCP服务器响应的 DHCPoffer 封包则会根据这些资料传递给要求租约的客户。根据服务器端的设定,DHCPoffer 封包会包含一个租约期限的信息。

  接受IP租约

  如果客户端收到网络上多台DHCP协议服务器的响应,只会挑选其中一个 DHCPoffer 而已(通常是最先抵达的那个),并且会向网络发送一个DHCPrequest广播封包,告诉所有 DHCP服务器它将指定接受哪一台服务器提供的IP地址。

  同时,客户端还会向网络发送一个 ARP 封包,查询网络上面有没有其它机器使用该IP地址;如果发现该IP已经被占用,客户端则会送出一个 DHCPDECLIENT 封包给 DHCP服务器,拒绝接受其 DHCPoffer ,并重新发送 DHCPdiscover 信息。


DHCP服务部分

 
查询是否安装DHCP服务
[root@localhost ~]# rpm -qa |grep dhcp
dhcpv6-client-1.0.10-17.el5
dhcp-3.0.5-21.el5
 
如果已经安装了,那就可以直接编辑使用dhcp服务。否则先安装

 
安装DHCP服务
[root@localhost ~]# mkdir /mnt/cdrom              //建立光盘挂载点

[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/    //挂载光盘
mount: block device /dev/cdrom is write-protected, mounting read-only

[root@localhost Server]# rpm -ivh dhcp-3.0.5-21.el5.i386.rpm  //安装


 
安装成功后,我们可以查看dhcp包的信息

因为安装后,在查询包的信息时。安装包的名字只要前端的版本号码,后面的平台部分去掉,否则查询是会提示说没有安装该服务
[root@localhost Server]# rpm -qi dhcp-3.0.5 
Name        : dhcp                         Relocations: (not relocatable)
Version     : 3.0.5                             Vendor: Red Hat, Inc.
Release     : 21.el5                        Build Date: Wed Apr 29 07:53:04 2009
Install Date: Wed Aug  7 16:09:17 2013         Build Host: ls20-bc2-13.build.redhat.com
Group       : System Environment/Daemons    Source RPM: dhcp-3.0.5-21.el5.src.rpm
Size        : 2230146                          License: distributable
Signature   : DSA/SHA1, Thu Jul 16 18:03:37 2009, Key ID 5326810137017186
Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
URL         :http://isc.org/products/DHCP/   //下载最新版dhcp的地址
Summary     : DHCP (Dynamic Host Configuration Protocol) server and relay agent.

。。。。。。。。

 

 
 
查询dhcp的安装路径和手册等信息
 
[root@localhost ~]# rpm -ql dhcp
/etc/dhcpd.conf                  //dhcp服务配置文件
/etc/rc.d/init.d/dhcpd           //dhcp服务进程文件
/etc/rc.d/init.d/dhcrelay        //dhcp中继服务文件
。。。
/var/lib/dhcpd                   //dhcp服务库文件
/var/lib/dhcpd/dhcpd.leases      //dhcp服务提供出去的ip记录文件



案例1   DHCP单个作用域

某公司因发展需要,新增加了200台设备,为了使用新设备,构建成一个新的局域网,ip地址自动从dhcp服务器上获取
 

195420454.png

 

 

实验环境:
Red Hat Enterprise Linux 5.4 32-bit
dhcp-3.0.5-21.el5.i386.rpm


 
首先我们安装dhcp服务器。

 
编辑dhcp配置文件 /etc/dhcpd.conf ,说明dhcp服务的参数
 
[root@localhost ~]# vim /etc/dhcpd.conf 


打开/etc/dhcpd.conf 文件,我们发现该文件内只有三行,缺少配置的内容,此时我们需要读取配置的内容
在末行模式下输入 
:r /usr/share/doc/dhclient-3.0.5/dhclient.conf.sample

然后进行配置,结果如下

# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample  
#
ddns-update-style interim;
ignore client-updates;

subnet 192.168.1.0 netmask 255.255.255.0 {    //提供的网段和子网掩码

# --- default gateway
        option routers                  192.168.1.254; //网关
        option subnet-mask              255.255.255.0; //子网掩码
option domain-name              "tec.com"      //所属域
        option domain-name-servers      222.88.88.88;  //DNS服务器
        #如果需要申明多个DNS地址是,在每两个地之间使用逗号“,”分离即可,如 option domain-name-servers      222.85.85.85,222.88.88.88;


        option time-offset              -18000; # Eastern Standard Time
option ntp-servers              210.72.145.44;
        //ntp 时间自动校准服务器 这个ip是中国的ntp服务器,电脑联网后自动从ntp服务器上获得中国标准时间
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this u
nless        
# -- you understand Netbios very well
#       option netbios-node-type 2;

range dynamic-bootp 192.168.1.2 192.168.1.253;
        // ducp服务提供的具体地址范围 
        default-lease-time 21600;    //默认的ip地址租用时间
        max-lease-time 43200;        //最大的ip地址租用时间

        # we want the nameserver to appear at a fixed address
host ns {     //可以对某个MAC和IP进行绑定,避免再次申请
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}


在RHEL上查看本机ip地址
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2D:64:68  
inet addr:192.168.1.123  Bcast:192.168.1.255  Mask:255.255.255.0



打开一台机器,使用dhcp自动获取ip地址并查看

C:\Documents and Settings\Administrator>ipconfig /all

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :  tec.com
        Description . . . . . . . . . . . : VMware Accelerated AMD PCNet Adapter

        Physical Address. . . . . . . . . : 00-0C-29-3E-17-29
        Dhcp Enabled. . . . . . . . . . . : Yes
        Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 192.168.1.147
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.254
  DHCP Server . . . . . . . . . . . : 192.168.1.123
        DNS Servers . . . . . . . . . . . : 222.88.88.88
        Lease Obtained. . . . . . . . . . : 2013年8月17日 11:10:29
        Lease Expires . . . . . . . . . . : 2013年8月17日 17:10:29




 

案例2   DHCP超级作用域

某公司因发展需要,新增加了500台设备,为了使新设备构建成一个新的局域网,ip地址自动从dhcp服务器上获取,此时一个网段的ip地址就不够用了,此时需要两个ip段,dhcp也需要做一个超级作用域才能满足

 

195425843.png

 

 

实验环境:
Red Hat Enterprise Linux 5.4 32-bit
dhcp-3.0.5-21.el5.i386.rpm



首先我们安装dhcp服务器。

编辑dhcp配置文件 /etc/dhcpd.conf ,说明dhcp服务的参数
[root@localhost ~]# vim /etc/dhcpd.conf 
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample  
#
ddns-update-style interim;
ignore client-updates;

shared-network dhcp {   //构建超级作用域,dhcp为域名,可以自由修改
subnet 192.168.1.0 netmask 255.255.255.0 {   

# --- default gateway
        option routers                  192.168.1.254; 
        option subnet-mask              255.255.255.0; 
        option domain-name              "tec.com"      
        option domain-name-servers      222.88.88.88;  
        option time-offset              -18000; # Eastern Standard Time
        option ntp-servers              210.72.145.44; 
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this u
nless        
# -- you understand Netbios very well
#       option netbios-node-type 2;

        range dynamic-bootp 192.168.1.2 192.168.1.253;
        default-lease-time 21600;    
        max-lease-time 43200;        

        # we want the nameserver to appear at a fixed address
        host ns {     
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
                }
        }
subnet 192.168.2.0 netmask 255.255.255.0 {   

# --- default gateway
        option routers                  192.168.2.254; 
        option subnet-mask              255.255.255.0; 
        option domain-name              "mkt.com"      
        option domain-name-servers      222.88.88.88;  
        option time-offset              -18000; # Eastern Standard Time
        option ntp-servers              210.72.145.44; 
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this u
nless        
# -- you understand Netbios very well
#       option netbios-node-type 2;

        range dynamic-bootp 192.168.2.2 192.168.2.253;
        // ducp服务提供的具体地址范围 
        default-lease-time 21600;    //默认的ip地址租用时间
        max-lease-time 43200;        //最大的ip地址租用时间

        # we want the nameserver to appear at a fixed address
        host ns {     //可以对某个MAC和IP进行绑定,避免再次申请
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
                }
        }
}


在RHEL上查看本机ip地址
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2D:64:68  
 inet addr:192.168.1.123  Bcast:192.168.1.255  Mask:255.255.255.0


正常的作用域如上所示,实验为了说明超级作用域的成功创建,将192.168.1.0网段的地址次修改为192.168.1.2 192.168.1.2 ,这样tec.con无法提供ip,只能由mkt.com来提供ip


打开一台机器,使用dhcp自动获取ip地址并查看

C:\Documents and Settings\Administrator>ipconfig /all

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . : mkt .com
        Description . . . . . . . . . . . : VMware Accelerated AMD PCNet Adapter

        Physical Address. . . . . . . . . : 00-0C-29-3E-17-29
        Dhcp Enabled. . . . . . . . . . . : Yes
        Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 192.168.2.100
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.2.254
   DHCP Server . . . . . . . . . . . : 192.168.1.123
        DNS Servers . . . . . . . . . . . : 222.88.88.88
        Lease Obtained. . . . . . . . . . : 2013年8月17日 11:30:32
        Lease Expires . . . . . . . . . . : 2013年8月17日 17:30:32





案例3   DHCP中继服务


某公司为了保护局域网的安全,对内部的不同部门的机器划分到不同的vlan,但是所有的机器的ip都是从dhcp服务器动态获得,为了满足条件,该公司做了dhcp中继

 

195429945.png

 

实验环境:
Red Hat Enterprise Linux 5.4 32-bit
dhcp-3.0.5-21.el5.i386.rpm
防火墙:H3C SecPath F100-C
交换机:华为 3526


1.编辑dhcp配置文件 /etc/dhcpd.conf ,说明dhcp服务的参数
添加了三个dhcp作用域,其中有tec.com ,mkt.com 和 本机所处的网段(192.168.30.*),如果不添加本网的,在启动dhcp服务时,会报错,无法使用

# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample  
ddns-update-style interim;
ignore client-updates;

subnet 192.168.10.0 netmask 255.255.255.0 {

        option routers                  192.168.10.1;
        option subnet-mask              255.255.255.0;

        option domain-name              "tec.com";
        option domain-name-servers      222.88.88.88;

        option time-offset              -18000; # Eastern Standard Time

        range dynamic-bootp 192.168.10.2 192.168.10.253;
        default-lease-time 21600;
        max-lease-time 43200;
        # we want the nameserver to appear at a fixed address
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}
#
subnet 192.168.20.0 netmask 255.255.255.0{
        option routers                  192.168.20.1;
        option subnet-mask              255.255.255.0;
        option domain-name              "mkt.com";
        option domain-name-servers      222.88.88.88;

        option time-offset              -18000; # Eastern Standard Time

        range dynamic-bootp 192.168.20.2 192.168.20.253;
        default-lease-time 21600;
        max-lease-time 43200;

        # we want the nameserver to appear at a fixed address
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}

subnet 192.168.10.0 netmask 255.255.255.0{
}


2.交换机配置

<Quidway>sys
Enter system view , return user view with Ctrl+Z.
[Quidway]vlan 10
[Quidway-vlan10]port e0/10
[Quidway-vlan10]vlan 20
[Quidway-vlan20]port e0/20
[Quidway-vlan20]vlan 30   
[Quidway-vlan30]port e0/22
[Quidway-vlan30]int e 0/24
[Quidway-Ethernet0/24]port link-type trunk 
[Quidway-Ethernet0/24]port trunk permit vlan all



 
3.防火墙配置
 
<H3C>sys
System View: return to User View with Ctrl+Z.
[H3C]undo insulate 
[H3C]int eth0/0.1 
[H3C-Ethernet0/0.1]
[H3C-Ethernet0/0.1]vlan-type dot1q vid 10
[H3C-Ethernet0/0.1]ip add 192.168.10.1 24
[H3C-Ethernet0/0.1]int eth0/0.2          
[H3C-Ethernet0/0.2]vlan-type dot1q vid 20
[H3C-Ethernet0/0.2]ip add 192.168.20.1 24
[H3C-Ethernet0/0.2]int eth0/0.3          
[H3C-Ethernet0/0.3]vlan-type dot1q vid 30
[H3C-Ethernet0/0.3]ip add 192.168.30.1 24
[H3C-Ethernet0/0.3]q
[H3C]firewall zone trust 
[H3C-zone-trust]add int eth0/0.1
[H3C-zone-trust]add int eth0/0.2
[H3C-zone-trust]add int eth0/0.3
[H3C-zone-trust]q
[H3C]int eth 0/0.1
[H3C-Ethernet0/0.1]dhcp select relay 
[H3C-Ethernet0/0.1]ip relay add 192.168.30.100
[H3C-Ethernet0/0.1]int eth 0/0.2              
[H3C-Ethernet0/0.2]dhcp select relay          
[H3C-Ethernet0/0.2]ip relay add 192.168.30.100             
[H3C-Ethernet0/0.3]q

 
 
 
4.测试
使用其他的机器分别接在vlan10和vlan20的端口。看是否获得ip地址

 

195432436.png

 

195435562.png

 

 

END!!!

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值