DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理、分配IP地址,使网络环境中的主机动态的获得IP地址、Gateway地址、DNS服务器地址等信息,并能够提升地址的使用率。
安装DHCP服务
[root@server ~]# yum search dhcp ##搜索dhcp相关安装包
Loaded plugins: langpacks
rhel_dvd | 4.1 kB 00:00
(1/2): rhel_dvd/group_gz | 136 kB 00:00
(2/2): rhel_dvd/primary_db | 3.6 MB 00:00
============================== N/S matched: dhcp ===============================
dhcp-common.x86_64 : Common files used by ISC dhcp client and server
dhcp-libs.x86_64 : Shared libraries used by ISC dhcp client and server
dhcp-libs.i686 : Shared libraries used by ISC dhcp client and server
dhclient.x86_64 : Provides the ISC DHCP client daemon and dhclient-script
dhcp.x86_64 : Dynamic host configuration protocol software
dnsmasq.x86_64 : A lightweight DHCP/caching DNS server
Name and summary matches only, use "search all" for everything.
[root@server ~]# yum install dhcp.x86_64 -y ##安装dhcp
一般情况下,DHCP配置文件位置在/etc/dhcp/dhcpd.conf
[root@server ~]# cd /etc/dhcp ##移动到/etc下查看dhcp配置文件
[root@server dhcp]# ls
dhclient.d dhcpd6.conf dhcpd.conf
[root@server dhcp]# vim dhcpd.conf ##查看dhcpd服务的conf文件
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example ##conf文件提示的配置文件模板文件
# see dhcpd.conf(5) man page ##conf文件提示的帮助信息
#
DHCP配置文件的参数可以man 5 dhcpd.conf查看,里面参数还蛮多的,我们直接拷贝模板文件过来配置
[root@server dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf ##复制配置文件模板
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@server dhcp]# vim dhcpd.conf ##编辑配置文件
1 # dhcpd.conf
2 #
3 # Sample configuration file for ISC dhcpd
4 #
5
6 # option definitions common to all supported networks...
7 option domain-name "example.org"; ##配置域名
8 option domain-name-servers ns1.example.org, ns2.example.org; ##配置DNS
9
10 default-lease-time 600; ##默认租期
11 max-lease-time 7200; ##最大租期
26 # This is a very basic subnet declaration.
27
28 subnet 172.25.254.0 netmask 255.255.255.0 { ##设置DHCP地址段,掩码
29 range 172.25.254.60 172.25.254.70; ##设置地址池
30 option routers 172.25.254.250; ##设置网关
31 }
32
##其他多余内容可以删除
编辑好配置文件后,启动dhcp服务,设置开机启动
[root@server dhcp]# systemctl start dhcpd ##启动dhcp服务
[root@server dhcp]# systemctl status dhcpd ##查看启动是否成功
dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled)
Active: active (running) since Fri 2018-04-26 20:39:29 EDT; 12s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 30297 (dhcpd)
Status: "Dispatching packets..."
CGroup: /system.slice/dhcpd.service
└─30297 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -g...
Apr 26 20:39:29 server dhcpd[30297]: Internet Systems Consortium DHCP Serve....5
Apr 26 20:39:29 server dhcpd[30297]: Copyright 2004-2013 Internet Systems C...m.
Apr 26 20:39:29 server dhcpd[30297]: All rights reserved.
Apr 26 20:39:29 server dhcpd[30297]: For info, please visit https://www.isc...p/
Apr 26 20:39:29 server dhcpd[30297]: Not searching LDAP since ldap-server, ...le
Apr 26 20:39:29 server dhcpd[30297]: Wrote 0 leases to leases file.
Apr 26 20:39:29 server dhcpd[30297]: Listening on LPF/eth0/52:54:00:17:b1:2...24
Apr 26 20:39:29 server dhcpd[30297]: Sending on LPF/eth0/52:54:00:17:b1:2...24
Apr 26 20:39:29 server dhcpd[30297]: Sending on Socket/fallback/fallback-net
Apr 26 20:39:29 server systemd[1]: Started DHCPv4 Server Daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@server ~]# systemctl enable dhcpd ##没有开机启动,设置开机启动
ln -s '/usr/lib/systemd/system/dhcpd.service' '/etc/systemd/system/multi-user.target.wants/dhcpd.service'
这样DHCP服务就搭建完成,可以查看/var/lib/dhcpd/dhcpd.leases里面记录着dhcp池分配ip的情况