Humanity

Edit the world by your favorite way

dnsmasqでDHCP・DNSサーバを立てて移行した手順まとめ

きっかけ

  • DHCPオプションが何気にいろいろ管理できる*1ことを最近知ったのでDHCPサーバでっち上げてみたかった
  • MACアドレスIPアドレス、ホスト名をまとめて1設定ファイルに書けるといいなー
    • それdnsmasqでできるよ
    • dnsmasqのよくある使い方としては/etc/hostsをそのまま読み込んでくれるので簡単にホストに名前をつけられるとかそういうのだと思う(多分)けど、自分は上にあるようにまとめて書きたいって要求とDHCP機能も使いたいって要求があったので/etc/dnsmasq.confにMACアドレスIPアドレス、ホスト名をまとめて書くことにする。こうすることでMACアドレスIPアドレスからDHCPでIP割り振れるしDNSでホスト名つけられるし最強感がある
  • 近々変える予定のルータにDHCPサーバ機能ついてるのか怪しい(調べてない)
    • ルータにはルーティングのみ任せるようにしたい

現在の環境

市販用ルータ(BBR-4MG)上でDHCPサーバが動いてる状態。
こいつをオフにしてDHCPサーバとついでにDNSサーバもでっちあげる。
とりあえず便宜上ホスト名はhogeでIPは192.168.0.10とする。
DHCPをインストールするサーバはDebian squeeze。

手順

dnsmasqのインストール
$ sudo apt-get install dnsmasq

でいけるはず。

とりあえずdnsmasqを止める
$ sudo service dnsmasq stop

とりあえず。まだルータのDHCPサーバが動いてるので。

dnsmasqの設定

以下は設定例です。実際の設定とは違っています。

--- /etc/dnsmasq.conf.bak   2012-02-26 11:44:47.403911388 +0900
+++ /etc/dnsmasq.conf   2012-02-26 12:14:20.908510981 +0900
@@ -140,6 +140,7 @@
 # agent. If you don't know what a DHCP relay agent is, you probably
 # don't need to worry about this.
 #dhcp-range=192.168.0.50,192.168.0.150,255.255.255.0,12h
+dhcp-range=192.168.0.50,192.168.0.70,255.255.255.0,12h
 
 # This is an example of a DHCP range which sets a tag, so that
 # some DHCP options may be set only for this network.
@@ -163,6 +164,28 @@
 # need to be on the same network. The order of the parameters in these
 # do not matter, it's permissble to give name,adddress and MAC in any order
 
+dhcp-host=de:ad:be:ef:co:ff,hoge,192.168.0.10,infinite
+dhcp-host=ee:vi:mi:st:he:be,fuga,192.168.0.11,infinite
+dhcp-host=st:te:xt:ed:it:or,piyo,192.168.0.12,infinite
+
+
 # Always allocate the host with ethernet address 11:22:33:44:55:66
 # The IP address 192.168.0.60
 #dhcp-host=11:22:33:44:55:66,192.168.0.60
@@ -253,12 +276,16 @@
 # are some options which are recommended, they are detailed at the
 # end of this section.
 
+# DNS server
+dhcp-option=option:dns-server,192.168.0.10,192.168.0.1
+
 # Override the default route supplied by dnsmasq, which assumes the
 # router is the same machine as the one running dnsmasq.
 #dhcp-option=3,1.2.3.4
 
 # Do the same thing, but using the option name
 #dhcp-option=option:router,1.2.3.4
+dhcp-option=option:router,192.168.0.1
 
 # Override the default route supplied by dnsmasq and send no default
 # route at all. Note that this only works for the options sent by
@@ -268,6 +295,10 @@
 
 # Set the NTP time server addresses to 192.168.0.4 and 10.10.0.5
 #dhcp-option=option:ntp-server,192.168.0.4,10.10.0.5
+#
+#ntp.nict.jp.            3600    IN      A       133.243.238.163
+#ntp.ring.gr.jp.         1610    IN      A       133.243.237.36
+dhcp-option=option:ntp-server,133.243.238.163,133.243.237.36
 
 # Set the NTP time server address to be the same machine as
 # is running dnsmasq
@@ -447,6 +478,7 @@
 
 # Set the cachesize here.
 #cache-size=150
+cache-size=0
 
 # If you want to disable negative caching, uncomment this.
 #no-negcache
DHCPサーバ自身に静的IPを割り振る

同じくDHCPサーバは自分自身のIPは管理できない(要出典)。
DHCPサーバ(dnsmasq)を立ち上げるにはIPを取得して通信できる状態でないとダメ(のような気がする(未検証))なのでDHCPサーバ自身は静的IPにする必要がある。ツッコミ待ち。
なので/etc/network/interfacesに以下の設定を書く。

auto eth0
iface eth0 inet static
  address 192.168.0.10
  network 192.168.0.0
  netmask 255.255.255.0
  broadcast 192.168.0.255
  gateway 192.168.0.1

自分はLXCを動かしたりしていてbridgeを使ってるので以下の設定を書いた(キリッ
ごめんなさいbridgeよくわかってません。ツッコミ待ち。

auto br0
iface br0 inet static
  bridge_ports eth0
  bridge_fd 0
  bridge_maxwait 0
  address 192.168.0.10
  network 192.168.0.0
  netmask 255.255.255.0
  broadcast 192.168.0.255
  gateway 192.168.0.1

bridge_ports, bridge_fd, bridge_maxwait以外は同じですね。

自身を/etc/resolv.confに載せる

同じくDHCPサーバは自分自身のDNSサーバは管理できない(要ry)ので
/etc/resolv.confの1行目に127.0.0.1を追加。

自身を/etc/hostsに載せる

hypervisorのみなぜか引けなかったので/etc/hostsに追加。

# for dnsmasq:
# * Do not distribute 127.0.0.1 as hypervisor.
# * Distribute hypervisor's IP and hostname
#   because only hypervisor can't be looked up...(why?)
#127.0.0.1      localhost hypervisor
127.0.0.1       localhost
192.168.0.10   hypervisor
ルータ(BBR-4MG)のDHCP機能をオフる

画像はイメージです。

dnsmasqを起動
$ sudo service dnsmasq start
移行完了

ごめんなさい、途中順序微妙に入れ替わってる部分とかあるので正直ステップ通りに行くか自信が(ry
まぁおおむねうまくいくんじゃないでしょうか。
うまくいかなかったらコメント欄で言ってください。
でも罵倒されるとあまりのショックにEmacs使うかもしれないので優しく指摘してくれればうれしいです。

*1:NTPサーバ、プリンタサーバ、MTU、デフォルトTTL、等々

*2:ありえないMACアドレスがありますが気にしないでください