1. 程式人生 > >Linux系統上DHCP伺服器的配置

Linux系統上DHCP伺服器的配置

一、DHCP的工作流程

 (1) DHCP伺服器發現階段。DHCP客戶機向廣播地址255.255.255.255傳送DHCPDiscover訊息。

 (2) DHCP伺服器響應階段。DHCP伺服器從尚未出租的IP地址池中選擇一個分配給DHCP客戶機,向DHCP客戶機發送一個包含出租的IP地址和其他設定的DHCPoffer提供資訊。

 (3) IP地址選擇階段。DHCP客戶機選擇某個DHCP伺服器提供的IP地址,以廣播方式回答一個DHCPrequest請求資訊.

 (4) IP地址確認階段。伺服器迴應包含它所提供的IP地址和其他設定的DHCPack確認資訊。然後客戶機將其與TCP/IP協議與網絡卡繫結。

 (5) 客戶機重新登入。再次傳送前次分配的IP地址的DHCPRequest請求請求資訊,DHCP伺服器檢查是否可以繼續讓客戶機使用,然後發回DHCPack確認資訊或者DHCPnck否認資訊。如果IP已不能用,客戶機需從(1)開始重新申請。

 (6) 更新租約。IP地址租借期限到了,如果DHCP客戶機要延長其租約,必須更新。DHCP客戶機啟動時和IP租約期限過一半時,都會向伺服器傳送更新期IP租約的資訊。

二、DHCP伺服器配置檔案

 路徑在/etc/dhcp/dhcpd.conf。下面是一個配置檔案的例子:

# 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;
  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;
  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;
  fixed-address fantasia.fugue.com;
}

# 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;
  }
}
DHCP配置檔案通常包括3部分:declarations, parameters, options。

declarations:描述網路的佈局;描述客戶;提供客戶的地址。

parameters:表明如何執行任務,是否要執行任務,或將哪些網路配置選項傳送給客戶。

option:配置DHCP可選引數。

常用宣告:

shared-network 告知DHCP伺服器是否為一些子網路分享相同網路

subnet描述一個IP是否屬於子網

range提供動態分配IP地址的起始和結束範圍

host為特定的主機提供網路引數

group為一組引數提供宣告

常用引數:

ddns-update-style 配置DHCP-DNS互動更新模式

default-lease-time 指定預設租賃時間的長度,單位為秒

max-lease-time 指定最大租賃時間長度,單位為秒

hardware指定網絡卡介面型別和MAC地址

server-name 告知DHCP客戶伺服器名稱

fixed-address 為客戶端指定一個固定的IP地址

常用選項:

domain-name 為客戶端指定域名

domain-name-servers為客戶端指明DNS伺服器IP地址

host-name為客戶端指明主機名稱

routers為客戶端指明預設閘道器

broadcast-address 為客戶端設定廣播地址

subnet-mask 為客戶端設定子網掩碼

ntp-server為客戶端設定格林威治時間的偏移時間,單位為秒

下面是一個DHCPv6的配置檔案的例子:

# Server configuration file example for DHCPv6
# From the file used for TAHI tests.

# IPv6 address valid lifetime
#  (at the end the address is no longer usable by the client)
#  (set to 30 days, the usual IPv6 default)
default-lease-time 2592000;

# IPv6 address preferred lifetime
#  (at the end the address is deprecated, i.e., the client should use
#   other addresses for new connections)
#  (set to 7 days, the	usual IPv6 default)
preferred-lifetime 604800;

# T1, the delay before Renew
#  (default is 1/2 preferred lifetime)
#  (set to 1 hour)
option dhcp-renewal-time 3600;

# T2, the delay before Rebind (if Renews failed)
#  (default is 3/4 preferred lifetime)
#  (set to 2 hours)
option dhcp-rebinding-time 7200;

# Enable RFC 5007 support (same than for DHCPv4)
allow leasequery;

# Global definitions for name server address(es) and domain search list
option dhcp6.name-servers 3ffe:501:ffff:100:200:ff:fe00:3f3e;
option dhcp6.domain-search "test.example.com","example.com";

# Set preference to 255 (maximum) in order to avoid waiting for
# additional servers when there is only one
##option dhcp6.preference 255;

# Server side command to enable rapid-commit (2 packet exchange)
##option dhcp6.rapid-commit;

# The delay before information-request refresh
#  (minimum is 10 minutes, maximum one day, default is to not refresh)
#  (set to 6 hours)
option dhcp6.info-refresh-time 21600;

# The path of the lease file
dhcpv6-lease-file-name "/usr/local/var/db/dhcpd6.leases";

# Static definition (must be global)
host myclient {
	# The entry is looked up by this
	host-identifier option
		dhcp6.client-id 00:01:00:01:00:04:93:e0:00:00:00:00:a2:a2;

	# A fixed address
	fixed-address6 3ffe:501:ffff:100::1234;

	# A fixed prefix
	fixed-prefix6 3ffe:501:ffff:101::/64;

	# Override of the global definitions,
	# works only when a resource (address or prefix) is assigned
	option dhcp6.name-servers 3ffe:501:ffff:100:200:ff:fe00:4f4e;

	# For debug (to see when the entry statements are executed)
	#  (log "sol" when a matching Solicitation is received)
	##if packet(0,1) = 1 { log(debug,"sol"); }
}

# The subnet where the server is attached
#  (i.e., the server has an address in this subnet)
subnet6 3ffe:501:ffff:100::/64 {
	# Two addresses available to clients
	#  (the third client should get NoAddrsAvail)
	range6 3ffe:501:ffff:100::10 3ffe:501:ffff:100::11;

	# Use the whole /64 prefix for temporary addresses
	#  (i.e., direct application of RFC 4941)
	range6 3ffe:501:ffff:100:: temporary;

	# Some /64 prefixes available for Prefix Delegation (RFC 3633)
	prefix6 3ffe:501:ffff:100:: 3ffe:501:ffff:111:: /64;
}

# A second subnet behind a relay agent
subnet6 3ffe:501:ffff:101::/64 {
	range6 3ffe:501:ffff:101::10 3ffe:501:ffff:101::11;

	# Override of the global definitions,
	# works only when a resource (address or prefix) is assigned
	option dhcp6.name-servers 3ffe:501:ffff:101:200:ff:fe00:3f3e;

}

# A third subnet behind a relay agent chain
subnet6 3ffe:501:ffff:102::/64 {
	range6 3ffe:501:ffff:102::10 3ffe:501:ffff:102::11;
}