一.什么是DHCP
动态主机配置协议是一个局域网的网络协议。指的是由服务器控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码。担任DHCP服务器的计算机需要安装TCP/IP协议,并为其设置静态IP地址、子网掩码、默认网关等内容。
二.实验操作流程
1. 搜索自己使用的计算机中是否有dhcp服务的安装程序
dnf serach dhcp
dnf install dhcp-server.x86_64##找到dhcp服务对应的程序进行安装
2. 编辑配置文件
cd /etc/dhcp/ ##切换到配置文件的环境
ll ##查看这个环境下的文件
cat /etc/dhcp/dhcpd.conf ##查看配置文件内容,发现里面没有内容但发现有一个模板/usr/share/doc/dhcp-server/dhcpd.conf.example
cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf ##复制模板到/etc/dhcp/dhcpd.conf
vim /etc/dhcp/dhcpd.conf ##编辑配置文件
配置文件的内容:
dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "xyf.org"; ##设置域名
option domain-name-servers 114.114.114.114;##设置dns,这里设置的是电信服务商
default-lease-time 600;##租约的最短有效期
max-lease-time 7200;##租约最长有效期
# 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 {
#}
# This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 { ##设置ip网络和子网掩码
range 172.25.254.37 172.25.254.137 ;##ip池,可以分配ip的范围
option routers 172.25.254.250 ;##网关设置
}
3.开启dhcp服务
systemctl status dhcp.service ##查看dhcp服务的状态,此时发现是关闭的。
systemctl start dhcpd.service ##开启dhcp服务
这个时候dhcp服务就已经开启了。