首先檢查DHCP Daemon是否安裝
要使用 dhcp 服務所需要的程式,檢查是否有安裝.沒有安裝就繼續下一個步驟.安裝完成請跳至第三個步驟.
[root@benjr ~]# rpm -avq | grep dhcp dhcp
安裝DHCP Daemon
在 RedHat 第二片光碟片中的目錄 /mnt/cdrom/ReaHat/RPMS/dhcp-…………..rpm 可以找到所需的程式
[root@benjr ~]# mount /mnt/cdrom [root@benjr ~]# rpm -ivh dhcp-……………rpm
啟動DHCP Daemon 前的準備
首先要先建立 /etc/dhcpd.conf檔 (/usr/share/doc/dhcp-xxx/dhcpd.conf.sample 有提供範例檔),內容如下
[root@benjr ~]# cp /usr/share/doc/dhcp/dhcp.conf.sample /etc/dhcpd.conf [root@benjr ~]# vi /etc/dhcpd.conf ddns-update-style interim; subnet 192.10.0.0 netmask 255.255.255.0 { range 192.10.0.10 192.10.0.20; default-lease-time 600; option subnet-mask 255.255.255.0; option broadcast-address 192.10.0.255; }
- ddns-update-style interim;
ddns-update-style interim DHCP V3.0以後都要加這一行? - subnet 192.10.0.0 netmask 255.255.255.0
我的伺服器IP為 192.10.0.2,所以給一個相同區段的IP(也只能給相同區段的) - range 192.10.0.10 192.10.0.20;
DHCP IP 範圍由 192.10.0.10 至 192.10.0.20 - default-lease-time 600;
IP 的租期為 600秒. - option subnet-mask 255.255.255.0;
子網路遮罩為 255.255.255.0 - option broadcast-address 192.10.0.255;
廣播網段為 192.10.0.255
還有更多的設定,可使用man dhcpd.conf 來查詢.
與 dhcpd 相關的檔案還有
- /var/lib/dhcp/dhcpd.leases
記錄著 client 端IP租用情況[root@benjr ~]# vi /var/lib/dhcp/dhcpd.leases # All times in this file are in UTC (GMT), not your local timezone. This is # not a bug, so please don't ask about it. There is no portable way to # store leases in the local timezone, so please don't request this as a # feature. If this is inconvenient or confusing to you, we sincerely # apologize. Seriously, though – don't ask. # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-V3.0pl1 lease 192.10.0.20 { starts 0 2003/07/06 13:31:41; ends 1 2003/07/07 01:31:41; tstp 1 2003/07/07 01:31:41; binding state active; next binding state free; hardware ethernet 00:02:b3:af:b2:82; }
- /var/log/messages
存放著 client 端向 dhcp daemon 要求 IP 的封包請求互動過程[root@benjr ~]# cat /var/log/message | grep DHCP Jul 6 21:52:06 unsvr dhcpd: DHCPDISCOVER from 00:02:b3:af:b2:82 via eth0 Jul 6 21:52:06 unsvr dhcpd: DHCPOFFER on 192.10.0.20 to 00:02:b3:af:b2:82 via eth0 Jul 6 21:52:08 unsvr dhcpd: DHCPREQUEST for 192.10.0.20 (192.10.0.3) from 00:02:b3:af:b2:82 via eth0 Jul 6 21:52:08 unsvr dhcpd: DHCPACK on 192.10.0.20 to 00:02:b3:af:b2:82 via eth0
DHCP Client 向 DHCP Server (使用 UDP) 要求 IP 時主要的四個動作( DHCPDISCOVER , DHCPOFFER , DHCPREQUEST , DHCPACK ),如果能看到這四個動作,這就代表 Client 已經成功獲得 IP .而 /var/lib/dhcp/dhcp.lease 就會將此 IP 租用紀錄下來.
當 DHCP 有錯誤發生時可以透過 tcpdump 來觀察,下面 Client 是正確透過 DHCP 獲取 IP 的過程.
[root@benjr ~]# tcpdump -i eth0 -n port 67 and port 68 15:42:31.013004 IP 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:02:b3:af:b2:82, length 314 15:42:32.013978 IP 192.10.0.2.67 > 192.10.0.20.68: BOOTP/DHCP, Reply, length 300 15:42:32.014176 IP 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:02:b3:af:b2:82, length 320 15:42:32.039599 IP 192.10.0.2.67 > 192.10.0.20.68: BOOTP/DHCP, Reply, length 300
如果不確定封包是否正常,這時候可以透過 tcpdump -vv 來看詳細內容應該會是 DISCOVER , OFFER , REQUEST , ACK .
[root@benjr ~]# tcpdump -i eth0 -n port 67 and port 68 | grep -i DHCP-Message DHCP-Message Option 53, length 1: Discover DHCP-Message Option 53, length 1: Offer DHCP-Message Option 53, length 1: Request DHCP-Message Option 53, length 1: ACK
- /etc/sysconfig/dhcpd
[root@benjr ~]# cat /etc/sysconfig/dhcpd # Command line options here DHCPDARGS=eth1
系統預設啟動 DHCP Daemon 是使用 eth0,如果你想使用其他的網路裝置時,你就需要在這裡特別來指定.
CentOS / RHEL 7 設定不一樣請參考 https://benjr.tw/98042
啟動 DHCP Daemon
可以透過 dhcpd 指令或是 server 指令立即啟動服務,透過 ntsysv 或是 chkconfig 讓開機的時候就啟動服務.
# dhcpd option interface
option
- -cf config-file
指定 config-file 來代替預設 /etc/dhcpd.conf 系統組態檔 - lf lease-file
指定 lease-file 來代替預設的 /var/lib/dhcp/dhcp.lease 日誌檔 - -q
使用簡潔的日誌檔
interface 預設值為 eth0
[root@benjr ~]# service dhcpd start
立即啟動 dhcpd server,或者使用
[root@benjr ~]# ntsysv
勾選 dhcpd 讓 dhcpserver 可以在 current level 開機時啟動
[root@benjr ~]# chkconfig dhcpd on [root@benjr ~]# chkconfig –list dhcpd dhcpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
可讓 dhcp server 可以在 run level 2,3,4,5 開機時啟動
client 端需要使用的程式
# dhcpcd option interface
option
- -d
輸出除錯訊息,dhcpcd 使用 syslog local0.所以必須在 /etc/syslog.conf 加入 local0.* /var/log/dhcpcd.log ,然後重新啟動 syslog 的 service, 下次就可以看到錯誤訊息紀錄至 dhcpcd.log. 關於 syslog可以參考 系統日誌檔 - -k
釋回IP位址給DHCP - -n
重新向DHCP要求IP - -R
????
interface 預設值為 eth0
dhcpd.conf 常用的選項
option subnet-mask 255.255.255.0; option broadcast-address 192.168.163.255;
One thought on “Linux – DHCP (Dynamic Host Configuration Protocol)”