CentOS5 DHCPサーバー
(2010.7.23-2011.10.25, 2014.3.3,2015.2.18)
DHCPサーバーをたてる。
(環境) CentOS 5.5 64bit, 32bit, CentOS 6.5 64bit, CentOS 7.2 64bit
dhcpのインストール
rootで(以下プロンプトが#ならroot,$なら普通のユーザー),
# yum install dhcp
ネットワークカード(NIC)の指定
ネットワークカード(NIC)が2枚以上あるときは,どちらを使うのか,使うほうのニックネーム(eth0, eth1, em1, em2などifconfigで表示される名前)をdhcpの起動引数に指定する。
# vi /etc/sysconfig/dhcpd
# Command line options here
DHCPDARGS="eth0"
dhcp設定ファイルの変更と起動
サンプル設定ファイルdhcpd.conf.sampleをコピーして,設定変更。CentOS6から,設定ファイルdhcpd.confの設置場所が/etc/から/etc/dhcp/に変わった。
(CentOS5の場合)
# cp /usr/share/doc/dhcp-*/dhcpd.conf.sample /etc/dhcpd.conf
# vi /etc/dhcpd.conf
最初の2行はそのまま。
ddns-update-style interim;
ignore client-updates;
(CentOS6,7の場合)
# cp /usr/share/doc/dhcp-バージョン/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
# vi /etc/dhcp/dhcpd.conf
サンプルの行は全て不要なのでコメントアウトする。
subnet文でsubnetの設定を書く。
CentOS5の場合,ネットワークカードが2枚以上ささっている時は,まず初めに,このサーバー自身について書く。これを書かないと起動に「失敗」する。ネットワークカードが1枚の時,あるいはCentOS6では不要。
subnet 10.1.54.0 netmask 255.255.255.0 {
option routers 10.1.54.1;
option subnet-mask 255.255.255.0;
}
次に,IPアドレスを配りたいsubnetを書いてゆく..
subnet 10.1.49.0 netmask 255.255.255.0 {
option routers 10.1.49.1;
option subnet-mask 255.255.255.0;
range dynamic-bootp 10.1.49.151 10.1.49.200;
option domain-name-servers xxx.xxx.xxx.xxx,yyy.yyy.yyy.yyy;
default-lease-time 21600;
max-lease-time 43200;
}
rangeは配るIPアドレスの範囲(複数行あっても可。10.1.56.50 10.1.58.200のようにIPを続けて書いても可)。default-lease-timeはクライアントが期限を求めない場合の割り当て期間(秒),max-lease-timeはクライアントが期限を求めた場合の最大割り当て期間(秒)。
なお,各subnetに共通の設定は,{ }から出して,最初のsubnet文よりも上に書くことができる。以下のような感じ。
#以下の2行はcentos 5用だが6,7にあっても大丈夫
ddns-update-style interim;
ignore client-updates;
option domain-name-servers xxx.xxx.xxx.xxx,yyy.yyy.yyy.yyy;
default-lease-time 21600;
max-lease-time 43200;
subnet 10.1.54.0 netmask 255.255.255.0 {
option routers 10.1.54.1;
option subnet-mask 255.255.255.0;
}
subnet 10.1.49.0 netmask 255.255.255.0 {
option routers 10.1.49.1;
option subnet-mask 255.255.255.0;
range dynamic-bootp 10.1.49.151 10.1.49.200;
}
動作テスト
# dhcpd -f -d
うまくいったら,Ctrl+Cで停止.
dhcpd再起動と自動起動の設定
(CentOS5,6)
# /etc/init.d/dhcpd restart
# /sbin/chkconfig dhcpd on
(CentOS7)
# systemctl restart dhcpd
# systemctl enable dhcpd
以上
dhcpアドレスリース状況
dhcpアドレスリース状況は,
/var/lib/dhcpd.lease
に記録される。しかし
lease 10.2.59.193 {
starts 3 2011/09/28 08:59:05;
ends 3 2011/09/28 20:59:05;
tstp 3 2011/09/28 20:59:05;
binding state free;
hardware ethernet ??:??:??:??:??:??;
uid "\???\???\????\???\???\???";
}
のように,リース開始/終了時間が記録されているので,何か解析スクリプトを使わないとある瞬間の貸し出し数はわからない(と思う)。
参考
CentOSで自宅サーバー構築 (http://centossrv.com/) 2008.5現在
DHCPサーバを立てるには (http://www.atmarkit.co.jp/flinux/rensai/linuxtips/539usedhcpd.html) 2010.7現在