centos系统下配置dhcp服务器|Centos系统下配置DHCP服务步骤详解

更新时间:2020-05-05    来源:php基础    手机版     字体:

【www.bbyears.com--php基础】

基础环境:

[root@PXE ~]# uname -a
Linux PXE 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@PXE ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:70:00:DA 
          inet addr:1.1.1.13  Bcast:1.1.1.255  Mask:255.255.255.0
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0

安装过程:

[root@PXE ~]# rpm -q dhcp
package dhcp is not installed
[root@PXE ~]# yum install dhcp

=====================================================================================================================================
 Package                        Arch                      Version                                      Repository               Size
=====================================================================================================================================
Installing:
 dhcp                           x86_64                    12:4.1.1-49.P1.el6.centos                    base                    822 k
Installing for dependencies:
 portreserve                    x86_64                    0.0.4-9.el6                                  base                     23 k
Updating for dependencies:
 dhclient                       x86_64                    12:4.1.1-49.P1.el6.centos                    base                    319 k
 dhcp-common                    x86_64                    12:4.1.1-49.P1.el6.centos                    base                    143 k

Transaction Summary
=====================================================================================================================================
Install       2 Package(s)
Upgrade       2 Package(s)
[root@PXE ~]# rpm -q dhcp    #检查dhcp是否安装   
dhcp-4.1.1-49.P1.el6.centos.x86_64
[root@PXE ~]# cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.$(date +%F)    #备份,其实没有内容,养成习惯很重要
################################默认################################
[root@PXE ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf    #把模板拷贝过来
[root@PXE ~]# egrep -v "#" /etc/dhcp/dhcpd.conf
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;

 

log-facility local7;


subnet 10.152.187.0 netmask 255.255.255.0 {
}


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;
}


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;
}

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;
}


host passacaglia {
  hardware ethernet 0:0:c0:5d:bd:95;
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}

host fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;
}


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;
  }
}
################################默认################################
[root@PXE ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#   see "man 5 dhcpd.conf"
#
default-lease-time 600;    #缺省租用时间
max-lease-time 7200;    #最大租用时间
ddns-update-style none;    #配置不使用DHCP-DNS动态更新模式,ddns-update-style interim;
    subnet 1.1.1.0 netmask 255.255.255.0 {    #作用域
        range dynamic-bootp 1.1.1.50 1.1.1.120;   #租用IP地址的范围
        option routers 1.1.1.2;     #网关地址
        option subnet-mask 255.255.255.0;     #子网掩码
        filename "pxelinux.0";    #如果不是在TFTP根目录下,要写上目录
        next-server 1.1.1.13;    #tftp服务器地址
    } 
[root@PXE ~]# /etc/init.d/dhcpd start
Starting dhcpd:                                            [FAILED]
[root@PXE ~]# setenforce 0    #临时关闭selinux
[root@PXE ~]# /etc/init.d/dhcpd start
Starting dhcpd:                                            [  OK  ]
[root@PXE ~]# netstat -panu|grep dhc* 
udp        0      0 0.0.0.0:67                  0.0.0.0:*                               1445/dhcpd         
[root@PXE ~]# netstat -tunlp|grep dhc*
udp        0      0 0.0.0.0:67                  0.0.0.0:*                               1445/dhcpd
[root@PXE ~]# /etc/init.d/iptables stop
or
[root@PXE ~]# -A INPUT -m state –state NEW -m udp -p udp –dport 67 -j ACCEPT
[root@PXE ~]# -A INPUT -m state –state NEW -m udp -p udp –dport 68 -j ACCEPT

[root@PXE ~]# cat /var/lib/dhcpd/dhcpd.leases
[root@PXE ~]# grep DHCPOFFER /var/log/messages
Jan 22 20:10:37 PXE dhcpd: DHCPOFFER on 1.1.1.50 to 00:0c:29:92:de:53 via eth0

[root@PXE ~]# cat /etc/sysconfig/dhcpd    #这个参数貌似没有关系。可以不修改
# Command line options here


客户端:

[root@DHCP-client ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
[root@DHCP-client ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:92:DE:53 
          inet addr:1.1.1.50  Bcast:1.1.1.255  Mask:255.255.255.0

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
[root@DHCP-client ~]# cat /var/lib/dhclient/dhclient-eth0.leases
lease {
  interface "eth0";
  fixed-address 1.1.1.50;
  filename "pxelinux.0";
  option subnet-mask 255.255.255.0;
  option routers 1.1.1.2;
  option dhcp-lease-time 600;
  option dhcp-message-type 5;
  option dhcp-server-identifier 1.1.1.13;
  renew 5 2016/01/22 12:15:36;
  rebind 5 2016/01/22 12:19:22;
  expire 5 2016/01/22 12:20:37;
}
lease {
  interface "eth0";
  fixed-address 1.1.1.50;
  filename "pxelinux.0";
  option subnet-mask 255.255.255.0;
  option routers 1.1.1.2;
  option dhcp-lease-time 600;
  option dhcp-message-type 5;
  option dhcp-server-identifier 1.1.1.13;
  renew 5 2016/01/22 12:20:30;
  rebind 5 2016/01/22 12:24:21;
  expire 5 2016/01/22 12:25:36;
}
[root@DHCP-client ~]#
dhcpd.conf配置文件的详细说明:

全局设置:

    ddns-update-style参数:设置DHCP服务器与DNS服务器的动态信息更新模式,一般的DHCP服务器可以不考虑设置该项,但是全局设置中一定要包括ddns-update-style的设置dhcpd才可以正常启动。设置为:interim 表示与DNS互动更新,设置为:none 表示不自动更新。
    default-lease-time参数:设置默认租约时间,参数值单位是秒。默认租约时间表示客户端从服务器租用一个IP地址的默认时间,到大时间后客户端会向服务器提出继续租用该IP地址的请求。
    max-lease-time参数:设置最大租约时间,参数值单位是秒。当客户端租约的地址到大这个时间,就不能继续租用该IP地址。
    option domain-name参数:设置主机所在域的名称,设置的域名和主机名称一起组成主机全名。
    option domain-name-servers参数:设置DNS服务器地址,设置后客户机DNS会使用该地址,如果要设置多个DNS地址,用逗号隔开。
使用subnet声明设置子网属性:

    subnet后面设置网络地址,netmask后面设置子网掩码。
    range参数:是设置subnet中可供动态分配的IP地址范围,参数值一定要是subnet设置的子网之内,否则不能启动。
    subnet-mask参数:设置子网掩码,设置子网掩码之后一般不需要再设置网络地址和广播地址。
    routers参数:是设置默认网关。
使用host声明设置主机属性,通常用于为服务器分配好固定使用的IP地址:

    host后面需要为DHCP客户端主机设置主机名称。
    hardware参数:是设置客户端主机的物理地址(MAC地址),因为通常使用的都是以太网,所以hardware参数类型设置为ethernet。
    fixed-address参数:设置在host声明中指定的客户端分配固定使用的IP地址。

本文来源:http://www.bbyears.com/jiaocheng/93365.html