DHCP的工作原理
关于DHCP工作过程中的数据包,
DHCPDISCOVER      DHCP发现包
DHCPOFFER             DHCP提供包
DHCPREQUEST        DHCP请求包
DHCPACK                   DHCP确认包

简单概述:
首先,DHCP客户端会发送一个全网广播的DHCP发现包,寻找DHCP服务器。当网络中的DHCP服务器收到DHCP客户端的请求,就会在自己的地址池中随即分发一个ip地址给DHCP客户端。当DHCP客户端确定使用这个ip地址的时候,就会发送一个DHCP的确认包给DHCP服务器。

下面是关于DHCP这个服务的属性,

DHCP的相关软件包     dhcp-3.0.5-21.el5
DHCP的守护进程        /usr/sbin/dhcpd
DHCP的脚本                /etc/init.d/dhcpd
DHCP的端口               67(bootps)   68(bootpc)
DHCP的配置文件       /etc/dhcp.conf   配置文件  、  /var/lib/dhcpd/dhcpd.leases 租约文件;

安装过程:
[root@mysqlndb02 yum.repos.d]# rpm -qa |grep dhcp
dhcpv6-client-1.0.10-17.el5
[root@mysqlndb02 yum.repos.d]# yum -y install dhcp
[root@mysqlndb02 yum.repos.d]# rpm -qa |grep dhcp
dhcpv6-client-1.0.10-17.el5
dhcp-3.0.5-21.el5
编辑DHCP的主配置文件
vim /etc/dhcpd.conf
================================
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#
===================================================

[root@mysqlndb02 dhcp-3.0.5]# pwd
/usr/share/doc/dhcp-3.0.5
[root@mysqlndb02 dhcp-3.0.5]# ls
api+protocol                          draft-ietf-dhc-failover-07.txt  RELNOTES     rfc2485.txt
dhcpd.conf.sample                     IANA-arp-parameters             rfc1542.txt  rfc2489.txt
draft-ietf-dhc-authentication-14.txt  ja_JP.eucJP                     rfc2131.txt  rfc951.txt
draft-ietf-dhc-dhcp-dns-12.txt        README                          rfc2132.txt
[root@mysqlndb02 dhcp-3.0.5]#vim /etc/dhcpd.conf
我们把这个文件内容导进来,在末行模式运行以下命令
末行模式使用 :r  文件名
:r /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample

修改如下

#定义所支持的DNS的动态更新类型(必选)
ddns-update-style interim;
#忽略客户端更新DNS记录
ignore client-updates;
#定义作用域是192.168.0.0/24
subnet 192.168.0.0 netmask 255.255.255.0 {

# --- default gateway
#为DHCP客户设置网关
    option routers            192.168.0.1;
#为DHCP客户设自豪子网掩码
    option subnet-mask        255.255.255.0;
#为DHCP客户设置NIS的域名,没有定义;
#    option nis-domain        "domain.org";
#为DHCP客户设置DNS服务器的域名
#    option domain-name        "domain.org";
#为DHCP客户设置DNS服务器的IP地址
    option domain-name-servers    192.168.0.1;
#指定时区的设置为格林威治时间
    option time-offset        -18000;    # Eastern Standard Time
#定义DHCP的地址池
    range dynamic-bootp 192.168.0.40 192.168.0.50;
#定义最小租约期是21600秒,6个小时
    default-lease-time 21600;
#定义最大的租约期12个小时
    max-lease-time 43200;
#设置主机声明
    host ns {
#设置用于定义服务器从引导文件中装入的主机名,用于无盘工作站
        next-server marvin.redhat.com;
#指定DHCP客户的MAC地址
        hardware ethernet 12:34:56:78:AB:CD;
#对指定的MAC地址分配固定的IP地址
        fixed-address 207.175.42.254;
    }
}
[root@mysqlndb02 etc]# service dhcpd restart
启动 dhcpd:                                               [确定]
启动另外一台机器使用DHCP获取

[root@mysql51 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:3E:8D:DF
          inet addr:192.168.0.50  Bcast:192.168.0.255  Mask:255.255.255.0
[root@mysql51 ~]# tail -f /var/log/messages     查看日志信息;

[root@mysqlndb02 dhcpd]# cat /var/lib/dhcpd/dhcpd.leases
# All times in this file are in UTC (GMT), not your local timezone.   This is
# not a bug, so please don't ask about it.   There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature.   If this is inconvenient or confusing to you, we sincerely
# apologize.   Seriously, though - don't ask.
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-V3.0.5-RedHat

lease 192.168.0.50 {
  starts 5 2010/08/06 07:59:25;
  ends 5 2010/08/06 13:59:25;
  tstp 5 2010/08/06 13:59:25;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:29:3e:8d:df;
}


  当计算机的IP地址不能够及时释放出去,就必须先停止DHCP服务,然后删除/var/lib/dhcpd目录下面的租约文件,然后启动DHCP服务,那么DHCP服务就可以正常工作了。