2012-02-26
dnsmasqで始めるプライベートDNSサーバ
ローカルネットワークのホスト名とIPアドレスの対応はDNSサーバで管理すると便利です。IPアドレスを覚えたりhostsをメンテナンスするのは大変です。でもbindって難しそうだし、メンテナンスに手間がかかるし、という方にはdnsmasqがおすすめです。
dnsmasqは軽量なDNSサーバとDHCPサーバです。/etc/hostsを管理する延長でDNSサーバを管理できます。この記事ではDNSサーバに焦点を当てて説明します。
ローカルネットワークにDNSを立てる
まず、以下の場合を考えてみます。
- ホスト名とIPアドレスの対応を一元管理したい。
- ローカルネットワークのホストには xxx.laputa.example.com を付与する。
- FQDNでなくてもアクセス可能にしたい。
例えば、192.168.2.3 が muska.laputa.example.com という名前だとすると、
ping muska
でも
ping muska.laputa.example.com
でも 192.168.2.3 に到達できるようにします。
そんな時は、サーバにdnsmasqをインストールし、以下の設定を入れます。
# /etc/dnsmasq.conf # Never forward plain names (without a dot or domain part) domain-needed # Never forward addresses in the non-routed address spaces. bogus-priv # Add local-only domains here, queries in these domains are answered # from /etc/hosts or DHCP only. local=/laputa.example.com/ # Set this (and domain: see below) if you want to have a domain # automatically added to simple names in a hosts-file. expand-hosts # Set the domain for dnsmasq. this is optional, but if it is set, it # does the following things. # 1) Allows DHCP hosts to have fully qualified domain names, as long # as the domain part matches this setting. # 2) Sets the "domain" DHCP option thereby potentially setting the # domain of all systems configured by DHCP # 3) Provides the domain part for "expand-hosts" domain=laputa.example.com
# /etc/hosts
192.168.2.3 muska.laputa.example.com muska
dnsmasqを実行するサーバのリゾルバはISPに向けたままにします。これにより、
- DNSサーバで
- musukaを解決しようとすると、リゾルバは /etc/hosts を参照する。
- musuka.laputa.example.comを解決しようとすると、リゾルバは /etc/hosts を参照する。
- www.google.comを解決しようとすると、リゾルバは上位DNSサーバに問い合わせる。
- DNSサーバのdnsmasqが問い合わせを受けて
- musukaを解決しようとすると、dnsmaskは /etc/hosts を参照する。
- musuka.laputa.example.comを解決しようとすると、dnsmaskは /etc/hosts を参照する。
- www.google.comを解決しようとすると、dnsmasqは上位DNSサーバに問い合わせる。
となります。
これぐらいなら市販されている一般的なルータでも可能ですね。
特定のドメインだけ別のネームサーバに問い合わせる
dnsmasqでは、特定のドメインだけ別のネームサーバに問い合わせることが可能です。例えば、VPNのドメインだけ別のDNSサーバが解決し、それ以外はISPのDNSサーバが解決する構成もできます。すべての名前解決をVPNトンネルの先にあるDNSサーバに任せると遅延や障害の影響を受けてしまいます。
上記が分かりにくかったので書き直しました(2012-02-26)
以下のような構成を考えます。
- /etc/hostsに書かれているホストはそのまま答える。
- laputa.example.comドメインは 192.168.2.254 に問い合わせる。
- 192.168.2.xxxの逆引きは 192.168.2.254 に問い合わせる。
- それ以外は上位DNSサーバに問い合わせる。
その場合、ローカルにdnsmasqをインストールし、以下のような設定を入れます。
# /etc/dnsmasq.conf # Change this line if you want dns to get its upstream servers from # somewhere other that /etc/resolv.conf resolv-file=/etc/dnsmasq.resolv.conf # By default, dnsmasq will send queries to any of the upstream # servers it knows about and tries to favour servers to are known # to be up. Uncommenting this forces dnsmasq to try each query # with each server strictly in the order they appear in # /etc/resolv.conf strict-order # Add other name servers here, with domain specs if they are for # non-public domains. server=/laputa.example.com/192.168.2.254 # Add routing PTR queries to nameservers server=/2.168.192.in-addr.arpa/192.168.2.254
# /etc/dnsmasq.resolv.conf # 上位ISPのDNSサーバ nameserver 8.8.8.8
# /etc/resolv.conf
domain laputa.example.com
nameserver 127.0.0.1
まとめ
dnsmasqを使うと、ほんのちょっとの設定で内部DNSサーバを構築できます。/etc/hostsをメンテナンスするだけなので誰でも運用できるようになります。さらにドメインによってDNSサーバを使い分けるように複雑な構成も可能です。Amazon Linuxのyumにも入っていますのでぜひお試しください。
- 320 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cts=1331337198628&ved=0CDAQFjAA&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=5ZdaT8OtLO7JmQXpiNmAAg&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ&sig2=NMOqb80Aroy1XM6aYOH1CA
- 192 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cts=1331174003549&ved=0CEYQFjAB&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=WRpYT8m2Ee-KmQX1wazHDw&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ&sig2=Vwf14TbE1-G5hzphoNvXdg
- 130 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CEsQFjAE&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=R9N7T4DpDsG5iQfgipXMCQ&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ&sig2=Fcd4K4q_hihcb9Z3xCO86Q
- 110 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CEQQFjAC&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=jd1qT-6yL6SniALgquH8BA&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ&sig2=w1mBJs4xNb95grGYCYVKHQ
- 100 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CEUQFjAD&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=cXpwT4rvHuiTiQfr-IDkBQ&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ
- 96 http://www.google.co.jp/url?sa=t&rct=j&q=dnsmasq 設定&source=web&cd=22&ved=0CDIQFjABOBQ&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=VBpNT96nOc3nmAWszdEN&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ
- 92 https://www.google.co.jp/
- 82 http://www.google.co.uk/url?sa=t&source=web&cd=1
- 77 http://b.hatena.ne.jp/hotentry/it
- 68 http://www.google.co.jp/url?sa=t&rct=j&q=dnsmasq&source=web&cd=5&ved=0CFgQFjAE&url=http://d.hatena.ne.jp/int128/20120226/1330247800&ei=mkt-T_vLM-njmAWDtPWVDg&usg=AFQjCNHdLaAjNqJ7Uk-eEWIAiALwtUmykQ&sig2=CKewcRo8-IXJVIiZiOtxKA