仅供初学者提及。看博客就有义务给博客评论。写下你们学习此方面遇到的问题和学习经验。谢谢。

DHCP

Dynamic Host Configuration Protocol,动态主机配置协议

理解DHCP服务      使用DHCP时必须在网络上有一台DHCP服务器,而其他机器执行DHCP客户端。当DHCP客户端程序发出一个信息,要求一个动态的IP地址时,DHCP服务器会根据目前已经配置的地址,提供一个可供使用的IP地址和子网掩码给客户端。     

 1. 使用DHCP的优点      DHCP使服务器能够动态地为网络中的其他服务器提供IP地址,通过使用DHCP,就可以不给Intranet网中除DHCP、DNS和WINS服务器外的任何服务器设置和维护静态IP地址。使用DHCP可以大大简化配置客户机的TCP/IP的工作,尤其是当某些TCP/IP参数改变时,如网络的大规模重建而引起的IP地址和子网掩码的更改。  

软件包dhcp  yum install -y dhcp 

监听端口  67/udp

配置文件  /etc/dhcp/dhcp.conf  里面的样例 

cp usr/share/doc/dhcp*/dhcp.conf.sample /etc/dhcp/dhcp.conf

(2)相关配置文件 /etc/dhcpd.conf               

 主配置文件 /etc/sysconfig/dhcpd       指定开放dhcp服务的端口 

/var/lib/dhcpd/dhcpd.lease        服务器租约文件  主配置说明,默认为空,需要拷贝 

/usr/share/doc/dhcp*/dhcpd.conf.sample模版文件 

主要文件/etc/dhcpd.conf选项说明:

 ==================================

 192.168.0.0 netmask 255.255.255.0 {   定义一个可提供IP的网端,该网段必须存在于本机的某块网卡上。       

   option routers                  192.168.0.1;   分配给客户端的默认路由,即网关。          

 option subnet-mask              255.255.255.0;       网关掩码         

 option nis-domain               "domain.org";        NIS域信息,一般不使用         

 option domain-name              "domain.org";    域名信息         

 option domain-name-servers      192.168.1.1;   分配给主机的DNS         

 option time-offset              -18000;

 # Eastern Standard Time     同步时间,与格林威治的时间差 

 #       option ntp-servers              192.168.1.1;                 

 #       option netbios-name-servers     192.168.1.1;         

 range dynamic-bootp 192.168.0.128 192.168.0.254;      定义可分配给客户的IP范围         

 # we want the nameserver to appear at a fixed address        

 host ns {                           保留地址的主机,该主机对应指定Ip                 

 next-server marvin.redhat.com;            用于指定tftp服务器,网络引导时才需使用                 

 hardware ethernet 12:34:56:78:AB:CD;        指定主机的mac地址                 

 fixed-address 207.175.42.254;              指定分配的IP地址         

 } }

cat /etc/dhcp/dhcpd.conf

#

# DHCP Server Configuration file.

# dhcpd.conf

#

# Sample configuration file for ISC dhcpd

#

# option definitions common to all supported networks...在所有支持的网络中常见的选项定义

option domain-name "example.org";

option domain-name-servers ns1.example.org, ns2.example.org;


#default-lease-time 600;

default-lease-time 21600;         默认租约时间   

#max-lease-time 7200;

 max-lease-time 43200;              最大租约时间    

# Use this to enable / disable dynamic dns updates globally.

用这个来启用/禁用动态DNS更新全局。

#ddns-update-style none;

 #ddns-update-style interim;            全局配置,定义所支持的DNS动态更新类型,默认临时 

 ignore client-updates;    全局配置,是否忽略客户端DNS更新,使用默认 subnet 

# If this DHCP server is the official DHCP server for the local

# network, the authoritative directive should be uncommented.

如果DHCP服务器为本地网络官方的DHCP服务器,权威的指令应当取消注释。

#authoritative;

权威;

# Use this to send dhcp log messages to a different log file (you also

# have to hack syslog.conf to complete the redirection).

用它来发送DHCP消息记录到不同的日志文件(你也可以更改文件syslog.conf完成重定向)

log-facility local7;   日志设备 local7


# No service will be given on this subnet, but declaring it helps the 

# DHCP server to understand the network topology.

没有服务将给这个子网,但定义它有助于DHCP服务器了解网络的拓扑结构

subnet 10.152.187.0 netmask 255.255.255.0 {

}


# This is a very basic subnet declaration.


subnet 10.254.239.0 netmask 255.255.255.224 {

  range 10.254.239.10 10.254.239.20;

  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;

}


# This declaration allows BOOTP clients to get dynamic addresses,

# which we don't really recommend.

这一声明允许BOOTP客户获取动态地址,我们真的不建议。

(BOOTP(Bootstrap Protocol,引导程序协议)是一种引导协议,基于IP/UDP协议,也称自举协议,是DHCP协议的前身。BOOTP用于无盘工作站的局域网中,可以让无盘工作站从一个中心服务器上获得IP地址。通过BOOTP协议可以为局域网中的无盘工作站分配动态IP地址,这样就不需要管理员去为每个用户去设置静态IP地址。)

(BOOTP客户端会根据该回应帧来获得自己的IP地址并通过专用文件服务器(如TFTP服务器)下载启动镜像文件,模拟成磁盘来完成启动。)

subnet 10.254.239.32 netmask 255.255.255.224 {

  range dynamic-bootp 10.254.239.40 10.254.239.60;

  option broadcast-address 10.254.239.31;

  option routers rtr-239-32-1.example.org;

}


# A slightly different configuration for an internal subnet.

一个稍微为内部子网的不同的配置

subnet 10.5.5.0 netmask 255.255.255.224 {

  range 10.5.5.26 10.5.5.30;

  option domain-name-servers ns1.internal.example.org;

  option domain-name "internal.example.org";

  option routers 10.5.5.1;

  option broadcast-address 10.5.5.31;

  default-lease-time 600;

  max-lease-time 7200;

}


# Hosts which require special configuration options can be listed in

# host statements.   If no address is specified, the address will be

# allocated dynamically (if possible), but the host-specific information

# will still come from the host declaration.

需要特殊配置选项的主机可以在主机语句中列出.。如果没有指定地址,地址将动态分配(如果可能的话),但主机特定的信息仍然来自主机声明.。

host passacaglia {

  hardware ethernet 0:0:c0:5d:bd:95;

  filename "vmunix.passacaglia";

  server-name "toccata.fugue.com";

}


# Fixed IP addresses can also be specified for hosts.   These addresses

# should not also be listed as being available for dynamic assignment.

# Hosts for which fixed IP addresses have been specified can boot using

# BOOTP or DHCP.   Hosts for which no fixed address is specified can only

# be booted with DHCP, unless there is an address range on the subnet

# to which a BOOTP client is connected which has the dynamic-bootp flag

# set.

固定IP地址也可以为主机指定。这些地址不应该被列为动态分配.。主机的固定IP地址指定了可以使用BOOTP或者DHCP引导。它没有固定的地址是指定只能启动DHCP主机,除非有一个地址范围的子网,BOOTP客户端连接具有动态BOOTP旗标。

host fantasia {

  hardware ethernet 08:00:07:26:c0:a5;

  fixed-address fantasia.fugue.com;

}


# You can declare a class of clients and then do address allocation

# based on that.   The example below shows a case where all clients

# in a certain class get addresses on the 10.17.224/24 subnet, and all

# other clients get addresses on the 10.0.29/24 subnet.

您可以声明一个类的客户,然后做地址分配的基础上。下面的例子显示的情况下,在某一类的所有客户获得在10.17.224/24子网地址,和所有其他的客户得到的10.0.29/24子网地址。

class "foo" {

  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";

}


shared-network 224-29 {

  subnet 10.17.224.0 netmask 255.255.255.0 {

    option routers rtr-224.example.org;

  }

  subnet 10.0.29.0 netmask 255.255.255.0 {

    option routers rtr-29.example.org;

}

  pool {

    allow members of "foo";

    range 10.17.224.10 10.17.224.250;

  }

  pool {

    deny members of "foo";

    range 10.0.29.10 10.0.29.230;

  }

}

#   see /usr/share/doc/dhcp*/dhcpd.conf.sample

#   see 'man 5 dhcpd.conf'

#

        完成配置文件开启服务       service dhcpd start       

DHCP语法检查工具:dhcpd

错误1: Not configured to listen on any interfaces!    

网卡网段必须和DHCP分配的网段匹配


DHCP客户端获取IP的四个过程:

 1. DHCPDISCOVER    client ----广播-----> ALL

 2. DHCPOFFERserver--------------> client 

 3. DHCPREQUESTclient ----广播-----> ALL 

 #第二次依然使用广播,目的是为告诉其他DHCP服务器,自己已经得到DHCP回应。

 4. DHCPACK         server---------> client