Linux-搭建DHCP服務

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的情況
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章