一、DHCP簡單講解
DHCP就是動態主機配置協議(Dynamic Host Configuration Protocol)是一種基於UDP協議且僅限用於局域網的網絡協議,它的目的就是為了減輕TCP/IP網絡的規划、管理和維護的負擔,解決IP地址空間缺乏問題。這種網絡服務有利於對網絡中的客戶機IP地址進行有效管理。
DHCP功能分為兩個部份:一個是服務器端,而另一個是客戶端(客戶端不用安裝) DHCP透過“租約”的概念,有效且動態的分配客戶端的TCP/IP設定。
DHCP服務程序能夠使局域網內的主機自動且動態的獲取IP地址、子網掩碼、網關地址以及DNS服務器地址等信息,且能夠有效的提升地址使用率,提高配置效率,減少管理和維護成本
路由器DHCP服務器的三種IP分配方式:
DHCP服務器具有三種IP的分配方式,手動分配,自動分配和動態分配。其中動態分配功能最為強大,配置也最為煩瑣。目前的DHCP服務器一般支持全部的幾種分配方式或者是其中的兩種。
手動分配:
網絡管理員在DHCP服務器通過手工方法配置DHCP客戶機的IP地址。當DHCP客戶機要求網絡服務時,DHCP服務器把手工配置的IP地址傳遞給DHCP客戶機。
自動分配:
不需要進行任何的IP地址手工分配。當DHCP客戶機第一次向DHCP服務器租用到IP地址后,這個地址就永久地分配給了該DHCP客戶機,而不會再分配給其他客戶機。
動態分配:
當DHCP客戶機向DHCP服務器租用IP地址時,DHCP服務器只是暫時分配給客戶機一個IP地址。只要租約到期,這個地址就會還給DHCP服務器,以供其他客戶機使用。如果DHCP客戶機仍需要一個IP地址來完成工作,則可以再要求另外一個IP地址。
動態分配方法是惟一能夠自動重復使用IP地址的方法,它對於暫時連接到網上的DHCP客戶機來說尤其方便,對於永久性與網絡連接的新主機來說也是分配IP地址的好方法。DHCP客戶機在不再需要時才放棄IP地址,如DHCP客戶機要正常關閉時,它可以把IP地址釋放給DHCP服務器,然后DHCP服務器就可以把該IP地址分配給申請IP地址的DHCP客戶機。
使用動態分配方法可以解決IP地址不夠用的困擾,例如C類網絡只能支持254台主機,而網絡上的主機有三百多台,但如果網上同一時間最多有200個用戶,此時如果使用手工分配或自動分配將不能解決這一問題。而動態分配方式的IP地址並不固定分配給某一客戶機,只要有空閑的IP地址,DHCP服務器就可以將它分配給要求地址的客戶機;當客戶機不再需要IP地址時,就由DHCP服務器重新收回。
淺談DHCP IP相同的問題
DHCP IP相同,DHCP服務器優先分發尚未出租的最前IP地址,以后DHCP客戶機每次重新登錄網絡時,就不需要再發送DHCP discover發現信息,而是直接發送包含前一次所分配的IP地址的DHCP request請求信息。當DHCP服務器收到這一信息后,它會嘗試讓DHCP客戶機繼續使用原來的IP地址,並回答一個DHCP ack確認信息。如果此IP地址已無法再分配給原來的DHCP客戶機使用時,比如此IP地址已分配給其它DHCP客戶機使用,則DHCP服務器給 DHCP客戶機回答一個DHCP nack否認信息。當原來的DHCP客戶機收到此DHCP nack否認信息后,它就必須重新發送DHCP discover發現信息來請求新的IP地址。另外,DHCP服務器向DHCP客戶機出租的IP地址一般都有一個租借期限,期滿后DHCP服務器便會收回出租的IP地址。如果DHCP客戶機要延長其IP租約,則必須更新其IP租約。DHCP客戶機啟動時和IP租約期限過一半時,DHCP客戶機都會自動向 DHCP服務器發送更新其IP租約的信息。
DHCP服務程序的相關術語:
作用域:
一個完整的IP地址段,DHCP服務根據作用域來管理網絡的分布、分配IP地址及其他配置參數。
超級作用域:
用於支持同一個物理網絡上多個邏輯IP地址子網端,包含作用域的列表,並對子作用域統一管理。
排除范圍:
將某些IP地址在作用域中排除,確保這些IP地址不會被提供給DHCP客戶機。
地址池:
在定義DHCP服務的作用域並應用排除范圍后,剩余用來動態分配給DHCP客戶機的IP地址范圍。
租約:
即DHCP客戶機能夠使用動態分配到的IP地址的時間。
預約:
保證局域子網中特定設備總是獲取到相同的IP地址。
二、搭建過程
查看一下dhcp有沒有安裝
# rpm -qa dhcp
[root@localhost ~]# [root@localhost ~]# rpm -qa dhcp [root@localhost ~]#
安裝DHCP服務
# yum install dhcp -y
Installed: dhcp.x86_64 12:4.2.5-68.el7.centos.1 Dependency Updated: dhclient.x86_64 12:4.2.5-68.el7.centos.1 dhcp-common.x86_64 12:4.2.5-68.el7.centos.1 dhcp-libs.x86_64 12:4.2.5-68.el7.centos.1 Complete! [root@localhost ~]#
查看dhcp的版本號
# rpm -qa dhcp
[root@localhost ~]# rpm -qa dhcp dhcp-4.2.5-68.el7.centos.1.x86_64
查看一下dhcp.conf配置文件
# cat /etc/dhcp/dhcpd.conf
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page # [root@localhost ~]#
DHCP執行程序
[root@localhost ~]# ls /usr/sbin/dhcrelay /usr/sbin/dhcrelay [root@localhost ~]# [root@localhost ~]# ls /usr/sbin/dhcpd /usr/sbin/dhcpd [root@localhost ~]#
查看DHCP配置文件模板
# cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
# # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page # # dhcpd.conf # # Sample configuration file for ISC dhcpd # # option definitions common to all supported networks... option domain-name "example.org"; option domain-name-servers ns1.example.org, ns2.example.org; 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 10.254.239.0 netmask 255.255.255.224 { range 10.254.239.10 10.254.239.20; #IP地址池 option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org; } # This declaration allows BOOTP clients to get dynamic addresses, # which we don't really recommend. subnet 10.254.239.32 netmask 255.255.255.224 { range dynamic-bootp 10.254.239.40 10.254.239.60; option broadcast-address 10.254.239.31; option routers rtr-239-32-1.example.org; } # A slightly different configuration for an internal subnet. subnet 10.5.5.0 netmask 255.255.255.224 { range 10.5.5.26 10.5.5.30; option domain-name-servers ns1.internal.example.org; #DNS服務器 option domain-name "internal.example.org"; #可選 設置默認搜索域 option routers 10.5.5.1; #網關 option broadcast-address 10.5.5.31; #可選 備選廣播地址 default-lease-time 600; max-lease-time 7200; } # Hosts which require special configuration options can be listed in # host statements. If no address is specified, the address will be # allocated dynamically (if possible), but the host-specific information # will still come from the host declaration. host passacaglia { hardware ethernet 0:0:c0:5d:bd:95; filename "vmunix.passacaglia"; server-name "toccata.fugue.com"; } # Fixed IP addresses can also be specified for hosts. These addresses # should not also be listed as being available for dynamic assignment. # Hosts for which fixed IP addresses have been specified can boot using # BOOTP or DHCP. Hosts for which no fixed address is specified can only # be booted with DHCP, unless there is an address range on the subnet # to which a BOOTP client is connected which has the dynamic-bootp flag # set. host fantasia { #主機 hardware ethernet 08:00:07:26:c0:a5; #指定文件服務器MAC地址 fixed-address fantasia.fugue.com; #指定IP地址 } # You can declare a class of clients and then do address allocation # based on that. The example below shows a case where all clients # in a certain class get addresses on the 10.17.224/24 subnet, and all # other clients get addresses on the 10.0.29/24 subnet. class "foo" { match if substring (option vendor-class-identifier, 0, 4) = "SUNW"; } shared-network 224-29 { subnet 10.17.224.0 netmask 255.255.255.0 { option routers rtr-224.example.org; } subnet 10.0.29.0 netmask 255.255.255.0 { option routers rtr-29.example.org; } pool { allow members of "foo"; range 10.17.224.10 10.17.224.250; } pool { deny members of "foo"; range 10.0.29.10 10.0.29.230; } }
在配置文件中通常包括三部分,分別是聲明(declarations)、參數(parameters)、選項(option)。
a、聲明是用來描述dhcpd服務器中對網絡布局的划分,是網絡設置的邏輯范圍。
B、參數用來表明如何執行任務,是否要執行任務或將哪些網絡配置選項發送給客戶。
c、選項是用來配置dhcp的可選參數,全部用option關鍵字作為開始。
一個標准的DHCP配置文件應該包括全局配置參數、子網網段聲明、地址配置選項以及地址配置參數:
全局配置參數用於定義整個配置文件的全局參數,而子網網段聲明用於配置整個子網段的地址屬性
三、配置實例
假設有一個公司的局域網,在該網絡中用linux搭建dhcp服務器,網絡中的ip地址網段為192.168.1.0,子網掩碼為255.255.255.0,動態分配的ip地址區間為192.168.0.3~192.168.0.254,dns服務器地址為114.114.114.114,網關為192.168.1.1,公司總經理計算機的ip地址有特殊要求,設置ip地址為192.168.1.88。改如何進行配置?
我們只要把配置文件修改一下即可,如下:
# cat /etc/dhcp/dhcpd.conf
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page # ddns-update-style none; #定義DNS服務動態更新的類型,類型包括:none不支持動態更新、interim互動更新模式、ad-hoc特殊更新模式
ignore client-updates; #忽略、(allow/允許)客戶機更新DNS記錄 subnet 192.168.1.0 netmask 255.255.255.0 { #意思是我所分配的ip地址所在的網段為192.168.1.0 子網掩碼為255.255.255.0 range 192.168.1.3 192.168.1.254; #租用IP地址的范圍
default-lease-time 86400; #默認租約時間 max-lease-time 518400; #最大租約時間 option routers 192.168.1.1; #路由器網關地址,這里是當前 dhcp 機器的網關地址 option domain-name-servers 114.114.114.114,8.8.8.8; #設置DNS域名服務器 host fantasia { #設置主機聲明 hardware ethernet 00:0c:29:eb:31:23; #指定dhcp客戶的mac地址 fixed-address 192.168.1.88; #給指定的mac地址分配ip }
} [root@localhost ~]#
重啟DHCP服務器
# systemctl restart dhcpd
注意:重啟DHCP服務器時,確保設置的物理地址網卡沒有IP地址,處於關機狀態為最佳,否則重啟時報錯
設置開機自啟動
# chkconfig dhcpd on
或者
# systemctl enable dhcpd
模擬總經理計算機
查看總經理計算機物理地址(MAC)
# cat /sys/class/net/eno16777736/address
[root@localhost ~]# cat /sys/class/net/eno16777736/address 00:0c:29:eb:31:23 [root@localhost ~]#
確認一下IP
四、DHCP相關的使用信息
/var/lib/dhcpd/dhcpd.leases 這個文件專門記錄了DHCP的分配情況
[root@localhost dhcpd]# pwd /var/lib/dhcpd [root@localhost dhcpd]# [root@localhost dhcpd]# ls dhcpd6.leases dhcpd.leases dhcpd.leases~ [root@localhost dhcpd]# [root@localhost dhcpd]# 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 lease 192.168.1.3 { starts 5 2019/02/15 15:05:37; ends 6 2019/02/16 15:05:37; tstp 6 2019/02/16 15:05:37; cltt 5 2019/02/15 15:05:37; binding state free; hardware ethernet 00:0c:29:eb:31:23; } lease 192.168.1.4 { starts 0 2019/02/17 15:08:35; ends 1 2019/02/18 15:08:35; cltt 0 2019/02/17 15:08:35; binding state active; next binding state free; rewind binding state free; hardware ethernet c8:d3:ff:de:22:83; uid "\001\310\323\377\336\"\203"; client-hostname "LAPTOP-6T0LVEVT"; } server-duid "\000\001\000\001#\371|\300\000\014)\207$\273"; [root@localhost dhcpd]#
參考博客:
centos7.0中搭建dhcp服務器
https://blog.csdn.net/qq_18831583/article/details/79001796
centos6.5上DHCP安裝與配置
https://blog.csdn.net/kongxx/article/details/43523225
centos7配置DHCP服務
http://www.bubuko.com/infodetail-1804738.html
詳述DHCP服務器的三種IP分配方式
https://www.cnblogs.com/sunflower627/p/3627654.html
linux下dhcp服務器分配出去的IP地址及剩余IP地址
https://blog.csdn.net/zhang_danf/article/details/24321855
end