GeekFactory

int128.hatenablog.com

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サーバが解決し、それ以外はISPDNSサーバが解決する構成もできます。すべての名前解決を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 Linuxyumにも入っていますのでぜひお試しください。

3分間DNS基礎講座

3分間DNS基礎講座