1,678 瀏覽數

DHCP (Dynamic Host Configuration Protocol)

首先檢查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.168.163.0 netmask 255.255.255.0
{
range 192.168.163.10 192.168.163.20;
default-lease-time 600;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.163.255;
}
  • ddns-update-style interim;
    ddns-update-style interim DHCP V3.0以後都要加這一行?
  • subnet 192.168.163.0 netmask 255.255.255.0
    我的伺服器IP為192.168.163.2,所以給一個相同區段的IP(也只能給相同區段的)
  • range 192.168.163.10 192.168.163.20;
    範圍由192.168.163.10 至 192.168.163.20.
  • default-lease-time 600;
    IP的租期為600秒.
  • option subnet-mask 255.255.255.0;
  • option broadcast-address 192.168.163.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 要求 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
    DHCPDARGS=eth1
    

    系統預設啟動 DHCP Daemon 是使用eth0,如果你想使用其他的網路裝置時,你就需要在這裡特別來指定.
    CentOS / RHEL 7 設定不一樣請參考 http://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 Reply to “DHCP (Dynamic Host Configuration Protocol)”

  1. 自動參照通知: services | Benjr.tw

發表迴響