搭建dhcp服务器

1.准备三台虚拟机,一台当DHCP服务器,另外两台当客户端,三台机器必须都属于同一个网段,因为DHCP服务基于广播机制,要求客户端与服务器必须在同一个网段(这里三台机器的网络全部改为仅主机模式)

把红框中的对勾去掉,使用DHCP服务器分配的地址而不是本地的DHCP服务。
搭建dhcp服务器_第1张图片

2.将服务器的IP地址固定,因为要当服务器使用,所以地址最好固定,两台客户端的IP地址改为动态获取

服务器IP地址
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=e1eb6903-f46b-4490-bb2b-74b0021957f1
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.147.129
NETMASK=255.255.255.0

客户端IP地址
搭建dhcp服务器_第2张图片
搭建dhcp服务器_第3张图片

3.在服务器端安装DHCP

[root@centos7 ~]# yum install dhcp
[root@centos7 ~]# yum info dhcp
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
已安装的软件包
名称    :dhcp
架构    :x86_64
时期       :12
版本    :4.2.5
发布    :58.el7.centos
大小    :1.4 M
源    :installed
来自源:base
简介    : Dynamic host configuration protocol software
网址    :http://isc.org/products/DHCP/
协议    : ISC
描述    : DHCP (Dynamic Host Configuration Protocol) is a protocol which allows
         : individual devices on an IP network to get their own network
         : configuration information (IP address, subnetmask, broadcast address,
         : etc.) from a DHCP server. The overall purpose of DHCP is to make it
         : easier to administer a large network.
         : 
         : To use DHCP on your network, install a DHCP service (or relay agent),
         : and on clients run a DHCP client daemon.  The dhcp package provides
         : the ISC DHCP service and relay agent.

[root@centos7 ~]# rpm -ql dhcp
/etc/NetworkManager
/etc/NetworkManager/dispatcher.d
/etc/NetworkManager/dispatcher.d/12-dhcpd
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/dhcp/scripts
/etc/dhcp/scripts/README.scripts
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/usr/bin/omshell
/usr/lib/systemd/system/dhcpd.service
/usr/lib/systemd/system/dhcpd6.service
/usr/lib/systemd/system/dhcrelay.service
/usr/sbin/dhcpd
/usr/sbin/dhcrelay
/usr/share/doc/dhcp-4.2.5
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
/usr/share/doc/dhcp-4.2.5/dhcpd6.conf.example
/usr/share/doc/dhcp-4.2.5/ldap
/usr/share/doc/dhcp-4.2.5/ldap/README.ldap
/usr/share/doc/dhcp-4.2.5/ldap/dhcp.schema
/usr/share/doc/dhcp-4.2.5/ldap/dhcpd-conf-to-ldap
/usr/share/man/man1/omshell.1.gz
/usr/share/man/man5/dhcpd.conf.5.gz
/usr/share/man/man5/dhcpd.leases.5.gz
/usr/share/man/man8/dhcpd.8.gz
/usr/share/man/man8/dhcrelay.8.gz
/usr/share/systemtap/tapset/dhcpd.stp
/var/lib/dhcpd
/var/lib/dhcpd/dhcpd.leases


4.编辑DHCP的配置文件/etc/dhcp/dhcpd.conf,由于是初次安装所以改配置文件比没有内容,也无法直接启动DHCP服务,所以我们将模板文件/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example拷贝并覆盖配置文件。

拷贝文件
[root@centos7 ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
编辑/etc/dhcp/dhcpd.conf
[root@centos7 ~]# vim /etc/dhcp/dhcpd.conf 
option domain-name "megedu.org";
option domain-name-servers 114.114.114.114, 8.8.8.8;
default-lease-time 600;
max-lease-time 7200;
#必须有一个子网与DHCP服务器的IP在同一个网段,否则无法启动DHCP服务。
subnet 192.168.147.0 netmask 255.255.255.0{
        range 192.168.147.10 192.168.147.100;   #分配的IP范围
        option routers 192.168.147.1;  #路由的IP地址
}


在没有启动DHCP服务之前,我们可以看到客户端没有获取到IP地址

搭建dhcp服务器_第4张图片
搭建dhcp服务器_第5张图片

5.启动DHCP服务

[root@centos7 ~]# systemctl restart dhcpd.service 
[root@centos7 ~]# systemctl status dhcpd.service 
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2019-04-09 10:38:06 CST; 1h 31min ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 10610 (dhcpd)
   Status: "Dispatching packets..."
   CGroup: /system.slice/dhcpd.service
           └─10610 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid

4月 09 12:04:34 centos7.cwj.com dhcpd[10610]: DHCPREQUEST for 192.168.147.12 from 00:50:56:c0:00:01 (cwj-PC) via ens33
4月 09 12:04:34 centos7.cwj.com dhcpd[10610]: DHCPACK on 192.168.147.12 to 00:50:56:c0:00:01 (cwj-PC) via ens33
4月 09 12:05:01 centos7.cwj.com dhcpd[10610]: DHCPREQUEST for 192.168.147.11 from 00:0c:29:ae:65:19 (centos6) via ens33
4月 09 12:05:01 centos7.cwj.com dhcpd[10610]: DHCPACK on 192.168.147.11 to 00:0c:29:ae:65:19 (centos6) via ens33
4月 09 12:07:37 centos7.cwj.com dhcpd[10610]: DHCPINFORM from 192.168.147.12 via ens33: not authoritative for subnet 192.168.147.0
4月 09 12:07:40 centos7.cwj.com dhcpd[10610]: DHCPINFORM from 192.168.147.12 via ens33: not authoritative for subnet 192.168.147.0
4月 09 12:08:58 centos7.cwj.com dhcpd[10610]: DHCPREQUEST for 192.168.147.11 from 00:0c:29:ae:65:19 (centos6) via ens33
4月 09 12:08:58 centos7.cwj.com dhcpd[10610]: DHCPACK on 192.168.147.11 to 00:0c:29:ae:65:19 (centos6) via ens33
4月 09 12:09:04 centos7.cwj.com dhcpd[10610]: DHCPREQUEST for 192.168.147.10 from 00:0c:29:85:c7:dd (centos6) via ens33
4月 09 12:09:04 centos7.cwj.com dhcpd[10610]: DHCPACK on 192.168.147.10 to 00:0c:29:85:c7:dd (centos6) via ens33
从这里就可以看出DHCP已经分配给客户端IP地址了,可以通过查看/var/lib/dhcpd/dhcpd.leases了解详细的地址分配记录。

[root@centos7 ~]# cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.2.5
# 这个是windows主机获取到的IP
lease 192.168.147.12 {
  starts 2 2019/04/09 03:34:35;
  ends 2 2019/04/09 03:44:35;
  cltt 2 2019/04/09 03:34:35;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:50:56:c0:00:01;
  uid "\001\000PV\300\000\001";
  client-hostname "cwj-PC";
}
lease 192.168.147.11 {
  starts 2 2019/04/09 03:37:12;
  ends 2 2019/04/09 03:47:12;
  cltt 2 2019/04/09 03:37:12;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:ae:65:19;
  client-hostname "centos6";
}
lease 192.168.147.10 {
  starts 2 2019/04/09 03:38:49;
  ends 2 2019/04/09 03:48:49;
  cltt 2 2019/04/09 03:38:49;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:85:c7:dd;
  client-hostname "centos6";
}

可以查看客户端获取的IP地址及其他相关信息。
搭建dhcp服务器_第6张图片
搭建dhcp服务器_第7张图片
获取到的路由及dns信息
搭建dhcp服务器_第8张图片
搭建dhcp服务器_第9张图片

你可能感兴趣的:(搭建dhcp服务器)