DHCP (Dynamic Host Configuration Protocol)

首先檢查DHCP Daemon是否安裝

要使用 dhcp 服務所需要的程式,檢查是否有安裝.沒有安裝就繼續下一個步驟.安裝完成請跳至第三個步驟.

[root@benjr ~]# rpm -avq | grep 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 netmask
default-lease-time 600;
option subnet-mask;
option broadcast-address;
  • ddns-update-style interim;
    ddns-update-style interim DHCP V3.0以後都要加這一行?
  • subnet netmask
  • range;
    範圍由192.168.163.10 至
  • default-lease-time 600;
  • option subnet-mask;
  • option broadcast-address;

還有更多的設定,可使用man dhcpd.conf 來查詢.


  • /var/lib/dhcp/dhcpd.leases

    [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 {
      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 to 00:02:b3:af:b2:82 via eth0
    Jul  6 21:52:08 unsvr dhcpd: DHCPREQUEST for ( from 00:02:b3:af:b2:82 via eth0
    Jul  6 21:52:08 unsvr dhcpd: DHCPACK on to 00:02:b3:af:b2:82 via eth0

    DHCP Client 向 DHCP Server 要求 IP 時主要的四個動作( DHCPDISCOVER , DHCPOFFER , DHCPREQUEST , DHCPACK ),如果能看到這四個動作,這就代表 Client 已經成功獲得 IP .而 /var/lib/dhcp/dhcp.lease 就會將此 IP 租用紀錄下來.

  • /etc/sysconfig/dhcpd
    [root@benjr ~]# cat /etc/sysconfig/dhcpd
    # Command line options here

    系統預設啟動 DHCP Daemon 是使用eth0,如果你想使用其他的網路裝置時,你就需要在這裡特別來指定.
    CentOS / RHEL 7 設定不一樣請參考 http://benjr.tw/98042

啟動 DHCP Daemon

可以透過 dhcpd 指令或是 server 指令立即啟動服務,透過 ntsysv 或是 chkconfig 讓開機的時候就啟動服務.

# dhcpd option interface


  • -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


  • -d
    輸出除錯訊息,dhcpcd 使用 syslog local0.所以必須在 /etc/syslog.conf 加入 local0.* /var/log/dhcpcd.log ,然後重新啟動 syslog 的 service, 下次就可以看到錯誤訊息紀錄至 dhcpcd.log. 關於 syslog可以參考 系統日誌檔
  • -k
  • -n
  • -R

interface 預設值為 eth0

dhcpd.conf 常用的選項

option subnet-mask;
option broadcast-address;

One thought on “DHCP (Dynamic Host Configuration Protocol)


你的電子郵件位址並不會被公開。 必要欄位標記為 *