【记录】搭建DHCP服务
   
   
    参考链接:https://blog.csdn.net/qq_41636653/article/details/81751060
   
| 版本:centos7 写作时间:2020-06-11 | 
    
    
    一、DHCP概要
   
    
    
    1.1 定义:
   
DHCP ( Dynamic Host Configuration Protocal )动态主机配置协议。
    
    
    1.2 作用:
   
    基于
    
     UDP协议
    
    ,在
    
     局域网内
    
    ,为主机自动分配IP地址,子网掩码,默认网关,DNS等网络信息
   
    
    
    1.3 工作过程图解:
   
DHCP服务器可以为客户端主机自动分配TCP/IP参数信息(IP地址,子网掩码,网关,DNS),DHCP服务器可以选择固定分配特定的参数给一指定的主机,也可以设置多台主机共享这些参数信息,所有客户端竞争获得这些参数信息(先到先得)。
     
   
    
    
    1.4 DHCP协议的工作过程
   
    
    
    1.4.1.发现阶段:DHCP discover
   
     **即DHCP客户机寻找DHCP服务器的阶段。**DHCP客户机以
    
     广播
    
    的方式发送
    
     DHCP discover 发现信息
    
    来寻找DHCP服务器(因为DHCP服务器的IP地址对客户机来说是未知的),即向255.255.255.255发送特定的广播信息,网络上每一台安装了TCP/IP协议的主机都会接收到这种广播信息,但只有DHCP服务器才会作出响应。
   
    
    
    1.4.2.提供阶段:DHCP offer
   
     **即DHCP服务器提供IP地址的阶段。**在网络中收到DHCP discover发现信息的DHCP服务器都会作出响应,它从尚未出租的IP地址中挑选一个分配给DHCP客户机,向DHCP客户机以
    
     单播
    
    的形式发送一个包含出租的IP地址、相应的租约期限和其他配置信息(如网关、DNS服务器等) DHCP offer提供信息。
   
    
    
    1.4.3.选择阶段:DHCP request
   
    
     DHCP客户机选择某台DHCP服务器提供的IP地址的阶段。
    
    如果有多台DHCP服务器向DHCP客户机发来的DHCP offer,客户机只接收第一个收到的DHCP offer,然后它以
    
     广播
    
    的方式回答一个DHCP request请求信息。该信息中包含它所选定的DHCP服务器请求IP地址的内容。之所以要以广播的方式回答,是为了通知所有的DHCP服务器,它将选择某台DHCP服务器所提供的IP地址。
   
    
    
    1.4.4 确认阶段:DHCP ACK
   
    
    
     即DHCP服务器确认所提供的IP地址的阶段。
     
      当DHCP服务器收到DHCP客户机回答的DHCP resquest请求后,根据Request报文中携带的用户
     
     MAC
    
    来查找有没有相应的租约记录(即之前的预分配过程中登记的那个MAC),如果有则发送ACK报文作为回应,通知用户可以使用分配的IP地址,然后DHCP客户机便将其TCP/IP协议与
    
     网卡
    
    绑定,除了DHCP客户机所选择的服务器IP外,其他的DHCP服务器都将
    
     收回
    
    曾提供的IP地址
   
    
    
    1.4.5 重新登陆:
   
     以后DHCP客户机每次登陆网络时,就不需要再发送DHCP discover发现信息了。而是直接发送包含前一次所分配IP地址的
    
     DHCP resquest
    
    请求。当DHCP服务器收到这一信息后,它会尝试让客户机继续使用原来的IP并回答一个DHCP ACK确认信息,如果此IP地址无法分配个原来的DHCP客户机时(比如IP分配给其他DHCP客户机使用) ,则DHCP服务器给DHCP客户机回答一个
    
     DHCP NACK
    
    否认消息,当原来的DHCP客户机收到此消息后,它就必须重新发送DHCP discover发现信息重新请求新的IP地址。
   
    
    
    1.4.6更新租约:
   
 DHCP服务器向DHCP客户机出租的IP地址一般都由一个租借期限,期满后DHCP服务器会收回出租的IP地址。如果DHCP客户机要延长其IP租约,则必须更新其租约。DHCP客户机启动时和IP租约期限过一半时,DHCP客户机都会自动向DHCP服务器发送其更新租约的信息。
原文链接:https://blog.csdn.net/qq_41636653/article/details/81751060
    
    
    二、搭建DHCP服务器
   
    
    
    2.1 流程
   
安装dhcp软件包—》编写 /etc/dhcp/dhcpd.conf文件—》服务器开启DHCP服务,加入开机自启选项—》客户端 重启网络服务。
    
    
    2.2 配置文件列表
   
| /etc/dhcp/dhcpd.conf | 主配置文件,几乎为空 | 
|---|---|
| /usr/share/doc/dhcp-4.2.5/dhcpd.conf.sample | dhcp主配置文件的参考模板 | 
| /var/lib/dhcpd/dhcpd.leases | 记录所有服务器已经分配出去的IP信息以及相关租期信息 | 
| /var/log/messsages | dhcpd服务的日志信息 | 
    
    
    2.3 demo 栗子
   
某公司需要搭建DHCP服务,其中有几台文件服务器需要配置固定的IP地址,其他主机配置动态IP地址即可
配置参数:
| 默认租约时间 | 21600秒 | 
|---|---|
| 最大租约时间 | 43200秒 | 
| IP地址范围 | 192.168.10.50~192.168.10.150 | 
| 子网掩码 | 255.255.255.0 | 
| 网关地址 | 192.168.10.1 | 
| DNS 服务器地址 | 192.168.10.1 | 
| 搜索域(相当于DNS 的域名吧!) | linuxprobe.com | 
| 配置主机linuxprobe 为固定IP 192.168.10.88 | 其MAC地址为:00:0c:29:27:c6:12 | 
    
    
    2.3.1 第一步:安装dhcp软件
   
yum -y   install  dhcp
    
    
    2.3.2 第二步:将dhcp配置文件的模板copy到 /etc/dhcp/dhcpd.conf,查看配置文件里写了啥
   
cp  /usr/share/doc/dhcp*/dhcpd.conf.example  /etc/dhcp/dhcpd.conf 
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y
###将主配置文件的参考模板复制过来
cat    /etc/dhcp/dhcpd.conf    #查看dhcp主配置文件,以下是主配置文件模板的内容
# dhcpd.conf
# Sample configuration file for ISC dhcpd
# option definitions common to all supported networks...
option domain-name "example.org";  ### DNS 的域名
option domain-name-servers ns1.example.org, ns2.example.org; ### DNS 的IP地址
default-lease-time 600;  ###默认租约时间
max-lease-time 7200;	###最大租约时间
### 是否允许DNS动态更新
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
# authoritative;
### 修改日志信息位置
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.
subnet 10.152.187.0 netmask 255.255.255.0 {#为主机分配IP地址时的子网及子网掩码
}
# 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;  ##IP地址池
  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.
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";
}
### 配置某台服务器为固定IP,将MAC地址与IP地址绑定
# 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.
host fantasia {#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.
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;
  }
}
    
    
    2.3.3 第三步:按要求来配置dhcp主配置文件,修改后的/etc/dhcp/dhcpd.conf文件
   
ddns-update-style=none;
ignore  client--updates;
subnet 192.168.10.0  netmask 255.255.255.0 {
range  192.168.10.50  192.168.10.150;#IP地址范围
option  subnet-mask  255.255.255.0;  #子网掩码
option  routers  192.168.10.1;		# 默认网关
option  domain-name "linuxprobe.com"; #域名名称
option  domain-name-servers  192.168.10.1; #域名IP地址
default-lease-time  21600; #默认租约时间
max-lease-time 43200; #最长租约时间
host   linuxprobe{ # 配置linuxprobe 主机为固定IP地址,将MAC地址与IP地址绑定
hardware  ethernet 00:0c:29:27:c6:12;
fixed-address 192.168.10.88;
}
}
    
    
    2.3.4 第四步:开启服务器端的DHCP服务,并加入开机自启
   
systemctl   start  dhcp
systemctl   enable  dhcp
    
    
    2.3.5 第五步:重启客户端的网络服务,以获得动态
   
systemctl   restart  network
 
