DHCP Server 設定及使用教學-使用 isc-dhcp 套件

此篇文章記錄在Debian/Ubuntu系統上使用isc-dhcp套件,安裝、設定及啟用DHCP Server功能的一個筆記。此篇文章會示範如何在DHCP Config設定IPv4 Pool,以可以讓Client在連上組織網路時,經由DHCP自動取得IPv4位址相關資訊。

本文教學將會在Debian/Ubuntu系統上操作,如果你使用其他的Linux發行版,當然也是可以參考本教學,但在指令方面需要自行更改以調整至適用你系統的環境。

**請注意isc-dhcp套件已不再被受到ISC公司的支援,已被新的isc-kea套件取代。

在設定IPv4 DHCP Pool前的準備:

 先整理好組織所使用的各網段資訊,比如:網段、子網路遮罩、Default Gateway和DNS位址。

 需排除的網段IP位址範圍(有些網路裝置是設定靜態IP位址或Default Gateway等等,都需要進行排除,不要讓DHCP進行發放)。

 需要使用到DHCP Options參數和資料。

 若DHCP Server與Client在不同一個網段的話,請務必在做routing的網路設備上設定好DHCP Relay(指向DHCP Server IP位址)。

 isc-dhcp套件可支援的isc-dhcp Options參數和資料。

 isc-dhcp套件支援的Option參數格式,可參考isc-dhcp Options格式文檔。

1)更新系統的套件資訊及安裝isc-dhcp-server套件。

Step 1:將Debian/Ubuntu系統的套件資訊與版本更新到最新。

sudo apt-get update && sudo apt-get upgrade -y

 

Step 2:安裝isc-dhcp-server套件

sudo apt-get install isc-dhcp-server -y

 

2)設定和編輯好DHCPD的Config檔。

Step 1:先進行備份原始『dhcpd.conf』設定檔。

sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.backup

 

**DHCPD的Config檔有多個設定,有大部分可以不用異動,用預設即可,之後可依照你組織環境去慢慢做最佳化調整,最基礎入門設定:先定義好網段、子網路遮罩、Default Gateway和DNS位址、IP位址租約時間等等的DHCP Options即可。

**『dhcpd.conf』設定檔有Global(全域)和Section(部分;每個不同網段的設定都為獨立的Section)設定,若每個網段都用同一個設定,比如『option domain-name-servers ns1.example.org;』,那就在Global區域定義一次即可,在設定Section時,就無須再次設定,除非有例外,在Section同時出現一樣的設定的話,比如在某個Section網段設定為『option domain-name-servers 8.8.8.8;』,那client在連線到這個網段時,DHCP就會派發此Section『option domain-name-servers 8.8.8.8;』,而不是Global的設定;否則若Section沒有此設定時,才會使用Global設定。

Step 2:編輯『dhcpd.conf』設定檔的Global設定。

sudo vim /etc/dhcp/dhcpd.conf

因為某些設定只需定義一次,故我們可以先從Global進行定義和設定:

Global設定大約在第9列開始:

**若你有定義其他Global設定的需求,請自行在下面繼續完成設定:

# ...
# 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;
# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages ('none', since DHCP v2 didn't
# have support for DDNS.)
ddns-update-style none;
# ...
項目說明
option domain-name "example.org";定義解析主機名的預設領域名稱;若有在『/etc/resolv.conf』設定了一個:比如『search example.com』的話,那Client在搜尋主機名稱時,就會幫Client加上此領域名稱;若組織網路沒此要求的話,此option可直接註解掉,可不設。
option domain-name-servers ns1.example.org, ns2.example.org;此設定可強制修改Client端的『/etc/resolv.conf』設定;講籠統一些就是讓Client知道要去哪找DNS主機,以讓Client可使用組織提供的DNS主機去解析位址,可使用域名或IP位址來指定DNS主機位址,若有多組DNS主機,請使用『,』隔開,以進行設定兩筆或最多三筆的DNS主機。
default-lease-time 600;定義Client可租用IP位址的時間,若Client沒特別要求租約時間的話,那麼就以此設定的租用時間為準,租期以秒計算,預設值為10分鐘。
max-lease-time 7200;類似『default-lease-time』,但此設定定義了Client可租用IP位址的最大時間,若Client要求租約時間大於此設定的話,那會強制使用此設定的租用時間為準,最大租期以秒計算,預設值為2小時。
ddns-update-style none;此設定使用預設即可,不用去做異動。

筆者本次Global設定,範例如下:

# ...
# option definitions common to all supported networks...
option domain-name "kjnotes.com";
option domain-name-servers ns1.kjnotes.com, ns2.kjnotes.com;
# default-lease-time值設定為1天
default-lease-time 86400;
# max-lease-time值設定為3天
max-lease-time 259200;
# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages ('none', since DHCP v2 didn't
# have support for DDNS.)
ddns-update-style none;
# ...

 

Step 3:接下來按照組織網路每個網段不同的需求,編輯Section部分的設定。

大約在63列左右下面開始新增你要配發IP的網段:

# ...
# 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 subnet-mask 255.255.255.224;
#  option routers 10.5.5.1;
#  option broadcast-address 10.5.5.31;
#  default-lease-time 600;
#  max-lease-time 7200;
#}
#上面為dhcpd.conf提供的範例,下面開始新增你要為網段配發DHCP資訊的網段資訊:
# ...

從『dhcpd.conf』提供的範例及其他的設定參數,來了解DHCP的設定:

**若有其他Option需求,請自行在Section新增所需的Option設定。

項目說明
range 10.5.5.26 10.5.5.30;此網段可分發的IP範圍。
option subnet-mask 255.255.255.224;此網段的遮罩。
option routers 10.5.5.1;此網段的Gateway。
option broadcast-address 10.5.5.31;此網段的廣播位址;可不設。
ping-check true;此設定可在IP配發給Client時的前置測試,若某IP位址測試沒回應就會配發此IP位址給Client;現在一般裝置都不允許ping,故通常會設在Server或網路設備的網段。

比如筆者要配發網段資訊和設定範例如下:

VoIP網段(192.168.32.0/24)

#VLAN-NAME-VoIP_192.168.32.0/24
subnet 192.168.32.0 netmask 255.255.255.0 {
  range 192.168.32.100 192.168.32.200;
  option subnet-mask 255.255.255.0;
  option routers 192.168.32.254;
  ping-check true;
  default-lease-time 259200;
  max-lease-time 604800;
}

Staff網段(192.168.33.0/24)

#VLAN-NAME-Staff_192.168.33.0/24
subnet 192.168.33.0 netmask 255.255.255.0 {
  range 192.168.33.1 192.168.33.230;
  option subnet-mask 255.255.255.0;
  option routers 192.168.33.254;
}

Guest網段(192.168.34.0/24)

#VLAN-NAME-Guest_192.168.34.0/24
subnet 192.168.34.0 netmask 255.255.255.0 {
  range 192.168.34.10 192.168.34.200;
  option subnet-mask 255.255.255.0;
  option routers 192.168.34.254;
  option domain-name-servers 1.1.1.1, 8.8.8.8;
  option domain-name "guest.kjnotes.com";
  default-lease-time 28800;
}

下面為筆者個人的筆記,其他比較特殊的option需求:

 Aruba AP跟控制器報到的範例:

**『option serverip 192.168.30.150;』更改成當地網路環境控制器的IP或VRRP IP。

#VLAN-NAME-NAD_192.168.30.192/28
option serverip code 43 = ip-address;
class "vendor-class" {
  match option vendor-class-identifier;
}
subnet 192.168.30.64 netmask 255.255.255.224 {
  range 192.168.30.193 192.168.30.205;
  option routers 192.168.30.206;
  option subnet-mask 255.255.255.240;
  option domain-name-servers 168.95.1.1, 1.1.1.1;
  option domain-name "nad.kjnotes.com";
  default-lease-time 604800;
  max-lease-time 604800;
  ping-check true;
  subclass "vendor-class" "ArubaInstantAP" {
    option vendor-class-identifier "ArubaAP";
    option serverip 192.168.30.150;
  }
  subclass "vendor-class" "ArubaAP" {
    option vendor-class-identifier "ArubaAP";
    option serverip 192.168.30.150;
  }
}

 

Step 4:如果此台DHCP服務是組織主要的DHCP主機,則需要加『authoritative;』參數。

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
# 下面註解拿掉即可
authoritative;

 

Step 5:如果需要固定某台Client的IP位址,那可以在『dhcpd.conf』設定檔中看到如下的範例,在下面加你要的固定某台Client的MAC和IP位址即可:

# 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.example.com;
#}
#筆者的範例
host KJie-PC {
hardware ethernet 11:22:33:44:55:A1;
fixed-address 192.168.22.220;
}
host KJie-BYOD {
hardware ethernet 11:22:33:44:55:A2;
fixed-address 192.168.22.221;
}
# ...

 

Step 6:重啟isc-dhcp服務,讓設定生效:

sudo systemctl restart isc-dhcp-server

 

3)系統防火牆要允許UDP Port 67的封包。

Step 1:Client在連上網路會傳送DHCPDISCOVE封包向網路進行廣播,如果DHCP主機有在使用防火牆服務,就需要監聽UDP Port 67,以Debian/Ubuntu來說,最常使用的防火牆為UFW,那可以輸入以下指令來允許UDP Port 67的封包:

sudo ufw allow 67/udp

 

4)可檢視DHCP服務已租出去的IP位址。

Step 1:若要檢視DHCP主機租出去的IP位址,可使用下面指令來檢視已分配給Client的IP位址資訊:

dhcp-lease-list

 

此篇文章上次修改日期:
2024/02/19