DHCP配置

本文介紹DHCP服務器的工作方式及配置方法


1. DHCP服務器的作用

把一個主機接入TCP/IP網絡,要爲主機配置的網絡參數:
  IP/mask:
  Gateway:網關(可完成網絡間通訊)
  DNS Server:
  Wins Server , NTP Server
配置這些參數的方式:
  手動配置
  動態分配
   早期:bootp
   現在:dhcp 引入了“租約”的bootp。也可以實現爲特定主機保留固定地址。


2. DHCP的報文格式

  DHCP DISCOVER: 客戶端廣播,請求獲取IP地址
  DHCP OFFER : 服務器到客戶端:給客戶端分配地址
  DHCP REQUEST: 客戶端到服務器:確認使用IP地址
  DHCP ACK : 服務器到客戶端:確認信息
  DHCP NAK: 服務器到客戶端,通知用戶無法分配合適的IP地址:一般爲地址池空
  DHCP DECLINE : 客戶端到服務器,指示地址已被使用:地址衝突
  DHCP RELEASE: 客戶端到服務器,放棄網絡地址和取消剩餘的租約時間:客戶端釋放地址
  DHCP INFORM: 客戶端到服務器, 客戶端如果需要從DHCP服務器端獲取更爲詳細的配置信息,則發送Inform報文向服務器進行請求,極少用到


3. DHCP的工作流程

工作流程:
  1. 客戶端:發請求報文 dhcp discover
  2. 服務端:dhcp offer (提供ip/mask , gw ……)
發送的dhcp offer中對ip地址有效期有租約期限:lease time(最長2天)
  3. 客戶端:dhcp request 向服務端發確認請求
  4. 服務囂:dhcp ack 服務端確認
DHCP的租約:
  假如租約爲2hours:
  租約還剩50%時,向DHCP服務器發送續約請求(dhcp request),若DHCP無響應,租約還剩25%時(50%的一半),客戶端再向DHCP服務囂發送續約請求,若DHCP還是無響應,租約剩12.5%時,客戶端再向本網絡發送dhcp discover請求。


4. DHCP服務的實現

dhcp:由ISC提供,只提供dhcp服務(本文僅討論本實現)
dnsmasq:可提供dhcp服務和dns服務(雲計算時會用到)

dhcp:
  dhcpd:提供dhcp服務
  dhcrelay:提供中繼服務(中繼與dhcp服務兩者不能同時提供)
服務的啓動:
  CentOS 6 : chkconfig dhcpd on ; service dhcpd start
  CentOS 7: systemctl start dhcpd
DHCP服務監聽的端口:
  服務端:67UDP端口
  客戶端:68UDP端口


5. 配置文件

dhcp服務端配置文件默認爲/etc/dhcpd/dhcpd.conf文件,但是文件裏只有以下這段話:

#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#

可從/usr/share/doc/dhcp(release-version)/下複製一份配置文件,改名爲/etc/dhcpd/dhcpd.conf再編輯。
其中option爲全局配置文件,也可在subnet中指定,如在subnet中指定,以subnet中的爲準。
下面是一份精簡的配置文件:

option domain-name "lxk.com";                       #指明DHCP服務器名稱
option domain-name-servers 8.8.8.8;                 #指明DNS服務器地址

default-lease-time 60000;                           #默認租約
max-lease-time 720000;                              #最長約租

log-facility local7;

subnet 192.168.200.0 netmask 255.255.255.0 {            #網段及掩碼
    range 192.168.200.10 192.168.200.200;           #分配的地址段範圍
    option routers 192.168.200.254;                 #默認路由
    next-server 192.168.200.254;                        #PXE啓動時下一個服務器的地址
    filename "pxelinux.0";                          #要加載的PXE啓動的文件
}

其它選項:
filename: 指明引導文件名稱;
  The filename statement can be used to specify the name of the initial boot file which is to be loaded by a client. The filename should be a filename recognizable to whatever file transfer protocol the client can be expected to use to load the file.
next-server:提供引導文件所在的服務器主機的IP地址
  The next-server statement is used to specify the host address of the server from which the initial boot file (specified in the filename statement) is to be loaded. Server-name should be a numeric IP address or a domain name. If no next-server statement applies to a given client, the address 0.0.0.0 is used.

6. DHCP客戶端測試工具

DHCP測試工具:

dhclient:DHCP客戶端測試
  -d:Force dhclient to run as a foreground process 前端運行
DHCP配置

7. 服務端查看地址分配記錄

[root@centos7 pxeboot]# cat /var/lib/dhcpd/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.200.10 {
  starts 6 2018/04/14 15:15:52;
  ends 0 2018/04/15 07:55:52;
  tstp 0 2018/04/15 07:55:52;
  cltt 6 2018/04/14 15:15:52;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:b5:04:a3;
}
lease 192.168.200.14 {
  starts 0 2018/04/15 03:01:48;
  ends 0 2018/04/15 19:41:48;
  tstp 0 2018/04/15 19:41:48;
  cltt 0 2018/04/15 03:01:48;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:85:af:9c;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章