DHCP (Dynamic Host Configuration Protocol) 自動分配網路參數 (IP、閘道、DNS 等)。本文記錄 ISC DHCP Server 從原始碼編譯安裝、配置檔編寫、服務啟動、租約管理、客戶端測試的完整流程。
# 官方下載: https://www.isc.org/downloads/dhcp/
# 最新版本: dhcp-4.2.1-P1.tar.gz (範例)
wget ftp://ftp.isc.org/isc/dhcp/dhcp-4.2.1-P1.tar.gz
tar zxvf dhcp-4.2.1-P1.tar.gz
cd dhcp-4.2.1-P1
./configure
make
make install
# 必須指定 DDNS 更新模式,否則服務無法啟動
ddns-update-style none;
# 定義服務子網範圍
subnet 192.168.0.0 netmask 255.255.255.0 {
# 閘道
option routers 192.168.0.1;
# 子網掩碼
option subnet-mask 255.255.255.0;
# 網域名稱
option domain-name "example.com";
# DNS 伺服器
option domain-name-servers 192.168.0.100;
# 動態分配 IP 範圍 (排除靜態 IP)
range 192.168.0.2 192.168.0.99;
# 預設租約時間 (6小時)
default-lease-time 21600;
# 最大租約時間 (12小時)
max-lease-time 43200;
# 靜態 IP 綁定 (MAC 對應固定 IP)
host mail {
hardware ethernet 00:03:FF:B6:9E:AB;
fixed-address 192.168.0.200;
}
}
mkdir -p /usr/local/var/db
touch /usr/local/var/db/dhcpd.leases
缺少此檔案會導致啟動失敗:Can't open lease database ... No such file or directory
dhcpd
成功啟動輸出:
Internet Systems Consortium DHCP Server 4.2.1
Listening on LPF/eth0/00:03:ff:b6:9e:ab/192.168.0.0/24
Sending on LPF/eth0/00:03:ff:b6:9e:ab/192.168.0.0/24
Sending on Socket/fallback/fallback-net
C:\> ipconfig /renew
Windows IP Configuration
Ethernet adapter:
IP Address. . . . . . . . . . . . : 192.168.0.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
DHCP Server . . . . . . . . . . . : 192.168.0.200
DNS Servers . . . . . . . . . . . : 192.168.0.200
# 修改網卡配置為 DHCP
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
# 重啟網路服務
service network restart
# 或手動請求租約
dhclient eth0
# 即時查看客戶端請求日誌
tail -f /var/log/messages
# 查看租約資料庫內容
cat /usr/local/var/db/dhcpd.leases
# 範例輸出
lease 192.168.0.2 {
starts 1 2011/05/25 10:22:19;
ends 1 2011/05/25 16:22:19;
hardware ethernet 00:15:f2:bd:9e:ab;
client-hostname "MyServer";
}
# 確認服務進程
ps aux | grep dhcpd
# 確認監聽埠口 (UDP 67)
netstat -unl | grep 67
# udp 0 0 0.0.0.0:67 0.0.0.0:*
# 查看服務埠定義
grep bootp /etc/services
# bootps 67/tcp # BOOTP server
# bootps 67/udp
# bootpc 68/tcp # BOOTP client
# bootpc 68/udp
dhcpd eth0Error opening '/proc/net/if_inet6' 可忽略,不影響 IPv4 服務fixed-address 不可落在 range 範圍內