CentOS7.3搭建dhcp服务器

一、实验环境:

VMware Workstation 12, CentOS 7.3 x64系统, CentOS 7.3 x64系统安装光盘

二、实验过程:

1.配置虚拟机软件的“虚拟网络编辑器”,将vmnet1网段修改如下图CentOS7.3搭建dhcp服务器_第1张图片

2.修改CentOS7.3系统的仅主机模式的网卡,如下图

CentOS7.3搭建dhcp服务器_第2张图片

3.配置本地yum源

[root@centos7 ~]#mkdir /etc/yum.repos.d/backup
[root@centos7 ~]#mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
[root@centos7 ~]#mount /dev/sr0 /mnt/cdrom
[root@centos7 ~]# vim /etc/yum.repos.d/basecd.repo
[root@centos7 ~]# cat /etc/yum.repos.d/basecd.repo 
[basecd]
name=basecd
baseurl=file:///mnt/cdrom
gpgcheck=0

4.将仅主机模式的网卡设定成静态IP

[root@centos7 ~]# nmcli con mod ens33 ipv4.method manual ipv4.address 192.168.21.7/24
[root@centos7 ~]# nmcli con up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

5.安装dhcp服务,并设置dhcp.conf

[root@centos7 ~]# yum -y install dhcp
[root@centos7 ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@centos7 ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@centos7 ~]# vim /etc/dhcp/dhcp.conf 
需要修改:
7:option domain-name "example.org" 改为 option domain-name "hengxia.top"  # dhcp的主机名
8:option domain-name-servers ns1.example.org, ns2.example.org;  改为option domain-name-servers 114.114.114.114; # DNS服务器
新增:
27行:
 subnet 192.168.21.0 netmask 255.255.255.0 {
     range 192.168.21.60 192.168.21.80; # 分配地址范围
     option routers 192.168.21.1; # 增加默认网关
     option domain-name-servers 114.114.114.114; # 默认DNS服务器
  }
[root@centos7 ~]# systemctl start dhcpd # 启动dhcpd服务

6.在测试centos6.9 64位系统上修改网卡为仅主机模式,修改仅主机模式的卡为dhcp获取。测试可以获取到。

[root@centos7 ~] cd /var/lib/dhcpd/      # 在CentOS7.3 DHCP 服务器上可以看到CentOS6.9 获取地址的信息
[root@centos7 ~]# cat 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

server-duid "\000\001\000\001!\006%6\000\014)\005#\025";

lease 192.168.21.60 {
  starts 6 2017/07/22 15:19:37;
  ends 6 2017/07/22 15:29:37;
  cltt 6 2017/07/22 15:19:37;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:01:71:22;
  client-hostname "centos6.xh.com";
}
[root@centos6 ~] cd /var/lib/dhclient        # 在获取IP主机上查看获取IP信息
[root@centos6 dhclient]# ls -lt | head -n 2  # 获取最新的记录
total 104
-rw-r--r--. 1 root root 2496 Jul 22 10:36 dhclient-c9090166-5360-40ea-968f-65fcb08a6a8e-eth0.lease

[root@centos6 dhclient]# cat dhclient-c9090166-5360-40ea-968f-65fcb08a6a8e-eth0.lease

lease {
  interface "eth0";
  fixed-address 192.168.21.60;
  option subnet-mask 255.255.255.0;
  option routers 192.168.21.1;
  option dhcp-lease-time 600;
  option dhcp-message-type 5;
  option domain-name-servers 114.114.114.114;
  option dhcp-server-identifier 192.168.21.7;
  option domain-name "centos7.com";
  renew 6 2017/07/22 02:36:08;
  rebind 6 2017/07/22 02:40:20;
  expire 6 2017/07/22 02:41:35;
}
lease {
  interface "eth0";
  fixed-address 192.168.21.60;
  option subnet-mask 255.255.255.0;
  option routers 192.168.21.1;
  option dhcp-lease-time 600;
  option dhcp-message-type 5;
  option domain-name-servers 114.114.114.114;
  option dhcp-server-identifier 192.168.21.7;
  option domain-name "centos7.com";
  renew 6 2017/07/22 02:40:49;                  UTC时间 date -u转换
  rebind 6 2017/07/22 02:44:59;
  expire 6 2017/07/22 02:46:14;
}

你可能感兴趣的:(网络安全)