干货 | 教你如何在linux系统上部署DHCP服务器,并用ENSP模拟测试
一、软硬件准备:
软件
VMware软件,这里我以VMware® Workstation 15 Pro为例;
centos镜像,这里我以centos8.3为例;
硬件

二、在window系统部署linux系统,这里以centos为例
1、下载镜像,

这里我选择DVD版的,文件会比较大一些。
虚拟机安装CentOS系统,之前都是用7,这次想试试8,发现只有2种ISO下载,一是7个多G的DVD,一个是700多M的boot版本,如果选boot,只能从网络安装,在安装时需要先打开Network&Host,然后设置Installation Source。

2、虚拟机环境准备
1、选择“创建新的虚拟机”

2、选择“自定义(高级)”
典型安装:VMware为帮你设置一下默认配置,对新手来说很方便,一直点击“下一步”即可
自定义安装:我们针对性的把一些资源需要加强,把不需要的资源移除,避免资源的浪费,同时也获得更好的体验。

3、兼容性设置,这个就看自己需要,否则点击下一步即可。

4、选择“稍后安装操作系统”

5、选择“linux”,版本这里选择centos 7 64位

6、给虚拟机命名及设置虚拟机保存位置

7、对处理器、内存进行分配,根据需要来设置,不够后面还可以修改。


8、选择网络类型,虚拟机提供了4种连接方式:桥接网络、NAT、仅主机模式、不使用网络连接。这里,我以桥接网络为例进行设置。

9、下面2个按默认设置即可


10、选择“创建新虚拟磁盘”

11、磁盘容量暂时分配推荐的20G,后期可随时增加。注意:不要勾选立即分配所有磁盘,否则虚拟机会将20G直接分配给CentOS,会导致宿主机所剩硬盘容量减少。
勾选将虚拟磁盘拆分成多个文件,这样可以使虚拟机方便用储存设备拷贝复制。



上图,可以点击“自定义硬件”,移除不必要的资源,然后点击完成。
如果忘了点击“自定义硬件”,没关系,也可以在设置中进行修改:

例如:以移除声卡为例,单击,然后点击下方“移除”即可

3、安装Centos系统
a、在刚刚创建的虚拟机中设置,加载已下载好的centos镜像。

选择DVD版


b、点击 “开启此虚拟机”,进行安装操作系统


c、设置时间:



d、软件选择,这里选择“sever with GUI”

e、设置root密码、管理员账号密码




设置完,点击开始安装


重启系统



三、搭建DHCP服务器
注意:虚拟机软件VmwareWorkstation默认开启了虚拟机DHCP服务,必须关闭后再进行DHCP实验。


设置仅主机模式:

使用下列命令,进行设置IP地址:
nmtui






上述配好后,点击选择Hide



四、开始实验
首先,安装dhcp前,你的网络无论是选择桥接还是仅主机模式,一定要先验证一下,能不能上网?




安装DHCP服务:
dnf -y install dhcp-server

配置DHCP服务:

如果你不会配置dhcp,可以看看提示,配置dhcpd文件,可以参考该目录下的实例。
以下配置可以直接复制,粘贴:
default-lease-time 600;max-lease-time 7200;ddns-update-style none;authoritative;subnet 192.168.20.0 netmask 255.255.255.0 { range 192.168.20.101 192.168.20.200; option routers 192.168.20.1; option subnet-mask 255.255.255.0; option domain-name-servers 8.8.8.8;
}


粘贴后,按一下esc,输入“:wq!” 保存退出。
default-lease-time为特定设备保留10分钟(600秒)的IP地址
max-lease-time 最多保留2小时(7200秒)的IP地址。
subnet部分定义了192.168.20.0/24网络的DHCP配置。
range部分定义了可分配的IP地址,范围为
192.168.20.101-192.168.20.200。
routers部分定义默认网关的地址。
subnet-mask部分定义将分配给每个主机的子网掩码。
domain-name-servers部分定义将分配给每个主机的DNS服务器地址。
配置完成之后可以使用下面命令启动dhcp服务:
systemctl enable dhcpd && systemctl start dhcpd

配置防火墙
如果开启防火墙,需要在防火墙中放开dhcp服务。
firewall-cmd --permanent --add-service=dhcp
firewall-cmd --reload

使用下列命令查看dhcp服务状态:
systemctl status dhcpd

状态是失败的,需要确保网卡ip和DHCP是同网段的:
(刚刚为联网下载dhcp程序源,网卡临时改为桥接模式,现在再改回仅主机模式)

顺便也确认一下网卡ip地址:

然后再次查看一下dhcp服务状态:

正常了!
接下来,我们来用ensp桥接这台DHCP服务器:

虚拟机与ensp桥接设置如下:

然后把两台PC1设置成通过DHCP获取IP地址:


然后在PC上面查看一下IP地址是否获取到了?


上图,可知ensp里的PC通过DHCP服务器获取到IPv4地址了。