Dnsmasq

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Dnsmasq and the translation is 100% complete.
Resources

dnsmasq は 1000 クライアント以下のローカルネットワークで使用できるシンプルな DHCP/DNS サーバです。主な特徴は設定の簡単さと軽量さです。IPv6 にも対応しています。

インストール

USE フラグ

適切な USE フラグを選択してください:

USE flags for net-dns/dnsmasq Small forwarding DNS server

auth-dns Add support for acting as an authorative DNS server.
conntrack Add support for Linux conntrack connection marking.
dbus Enable dbus support for anything that needs it (gpsd, gnomemeeting, etc)
dhcp Enable support for acting as a DHCP server.
dhcp-tools Install extra command line tools for manually managing DHCP leases.
dnssec Enable support DNSSEC validation and caching.
dumpfile Include code to dump packets to a libpcap-format file for debugging
id Whether report *.bind CHAOS info to clients, otherwise forward such requests upstream instead
idn Enable support for Internationalized Domain Names
inotify Enable inotify filesystem monitoring support
ipv6 Add support for IP version 6
libidn2 Enable support for Internationalized Domain Names, via net-dns/libidn2 rather than net-dns/libidn
loop Include functionality to probe for and remove DNS forwarding loops
lua Enable Lua scripting support
nettlehash Use hashing functions from dev-libs/nettle
nls Add Native Language Support (using gettext - GNU locale utilities)
script Enable support for calling scripts when leases change.
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
static !!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically
tftp Enables built in TFTP server for netbooting.

Emerge

次に、net-dns/dnsmasq パッケージをインストールしてください:

root #emerge --ask net-dns/dnsmasq

設定

dnsmasq の振る舞いを変えるには、さまざまなリソースを変更することで行えます。これには以下が含まれます。

  • /etc/conf.d/dnsmasq を通じて提供されるコマンドラインオプション
  • メインの設定ファイル (/etc/dnsmasq.conf)

サービス

OpenRC

自動的に開始する必要がある場合は、dnsmasq を default ランレベルに追加してください:

root #rc-update add dnsmasq default

すぐにサービスを開始するには:

root #rc-service dnsmasq start

サービスの設定

/etc/conf.d/dnsmasq では、dnsmasq デーモンの起動時に渡されるコマンドラインオプションを設定できます。

ファイル /etc/conf.d/dnsmasqdnsmasq サービス設定の例
DNSMASQ_OPTS="--user=dnsmasq --group=dnsmasq -H /srv/virt/gentoo/hosts --max-cache-ttl=10"

メインの設定ファイル

dnsmasq の主な設定は /etc/dnsmasq.conf 設定ファイルを通じて行われます。このファイルは キー[=値] の構文を使用しています。パッケージにより提供されるファイルはよく文書化されており、通しで読むことをおすすめします。ファイルの中で、あるいはコマンドラインオプションを通じて、(DHCP hosts ファイル等の) 追加のリソースに言及されているかもしれません。

以下はサンプルの設定ファイルです:

ファイル /etc/dnsmasq.conf
# このインターフェースだけで listen する
interface=eth1
  
# mac アドレスに基づいて名前を割り当てる
dhcp-host=00:1e:68:c2:ff:ee,endor,192.168.0.54,24h
  
# 他の DHCP リクエストはこの範囲から ip を取得する
dhcp-range=eth1,192.168.0.100,192.168.0.120,12h
  
# TFTP サーバを有効化し、TFTP からアクセスできるファイルのルートディレクトリを設定する。
enable-tftp
tftp-root=/var/lib/tftpboot
dhcp-boot=/pxelinux.0

設定ファイルの編集が完了したら、サービスを再起動する必要があります。リロードには対応していますが、これは他のリソースのためのものです。

root #/etc/init.d/dnsmasq restart

hosts ファイル

dnsmasq アプリケーションは -h (--no-hosts) コマンドライン引数を渡されない限り、DNS サービスを提供するために /etc/hosts ファイルを情報源のひとつとして使用します。

/etc/hosts ファイルが更新されたら、dnsmasq サービスはその設定を再読み込みするために SIGHUP シグナルを受信する必要があります。これは init スクリプトの reload コマンドを通しても対応しています:

root #/etc/init.d/dnsmasq reload

この挙動は設定ファイルで no-hosts パラメータを利用して無効化することもできます。

追加の hosts ファイル

DNS クエリのための情報源として使用するために、(追加の) hosts ファイルを渡すこともできます。これを行うには、-H /path/to/hostsfile (--addn-hosts=/path/to/hostsfile) コマンドラインオプションを追加してください。ディレクトリを渡すこともでき、その場合はディレクトリ内のすべてのファイルが追加の hosts ファイルとして扱われるでしょう。

通常の hosts ファイルと同様に、SIGHUP シグナルでファイルを再読み込みします。

この挙動は設定ファイルで addn-hosts パラメータを利用して設定することもできます。

上流のネームサーバ

dnsmasq はデフォルトでは、/etc/resolv.conf で指定されたネームサーバを上流のネームサーバとして利用します。

-r (--resolv-file) コマンドラインオプションを介して、異なるファイルを利用することもできます。

この挙動は設定ファイルで resolv-file パラメータを利用して設定することもできます。

機能

dnsmasq は DNS、TFTP、PXE、ルータ広告と DHCP サービスに対応しています。そのため、小〜中規模のネットワークのための多目的なネットワーク管理ツールとなっています。

DNS サービス

DNS サービス (のみ) を提供するためには、まず使用する上流ネームサーバを特定してください。もしこれが /etc/resolv.conf 内で指定されているネームサーバと同じである場合は、追加の手順を踏むは必要ありません。そうでない場合は、-r (--resolv-file) コマンドラインを通じて、dnsmasq が適切な resolv.conf ファイルを見るようにしてください。文法は /etc/resolv.conf ファイルが使用するものと同じですが、dnsmasq は nameserver 定義しか確認しません。

例えば:

root #echo "nameserver 8.8.8.8" >> /etc/dnsmasq.conf.resolv

次に、設定ファイルを通じて dnsmasq がこのファイルを見るようにしてください:

ファイル /etc/dnsmasq.confカスタム resolv ファイルの設定
resolv-file=/etc/dnsmasq.conf.resolv

設定ファイルを変更してサービスを再開した後、サービスが動作していることを確認するには、dig コマンド (net-dns/bind-tools から提供されています) を使用して、DNS サーバ (以下の例では localhost で動作しているもの) にローカルまたはリモートのアドレスを解決させてみてください:

user $dig @localhost +short www.gentoo.org
www-bytemark-v4v6.gentoo.org.
89.16.167.134

DNSSEC

dnsmasq は DNSSEC データを受け渡すと同時に検証することができます。これはコンフィグファイルにこれらの行を追加することで実現できます (dnssec USE フラグを有効化しておくのを忘れずに):

ファイル /etc/dnsmasq.confDNSSEC を有効にする
# Uncomment these to enable DNSSEC validation and caching:
# (Requires dnsmasq to be built with DNSSEC option.)
conf-file=/usr/share/dnsmasq/trust-anchors.conf
dnssec

# Replies which are not DNSSEC signed may be legitimate, because the domain
# is unsigned, or may be forgeries. Setting this option tells dnsmasq to
# check that an unsigned reply is OK, by finding a secure proof that a DS 
# record somewhere between the root and the domain does not exist. 
# The cost of setting this is that even queries in unsigned domains will need
# one or more extra DNS queries to verify.
dnssec-check-unsigned

この変更を加えると、検証が失敗した場合に、dnsmasq は SERVFAIL を返して DNS データを返さなくなるでしょう。検証が成功した場合は、ad ("authenticated data") フラグを設定します。ドメインが DNSSEC をサポートしていない場合は、dnsmasq は設定変更前と同様に動作します。

DHCP サービス

dnsmasq の DHCP サービスを有効化するには、dhcp-range 設定を使用してください。

例えば、ルータ広告 (RA) を利用した無限リースタイムの IPv6 アドレス設定と、無限リースタイムの IPv4 アドレス設定を有効化するには:

ファイル /etc/dnsmasq.confIPv6 および IPv4 のリースを有効化する
dhcp-range=2001:db8:81:e2::,ra-only,infinite
dhcp-range=192.168.100.100,192.168.100.149,infinite

メインの設定ファイル (dhcp-host= 設定) か、分離されたファイルを利用して、既知のホストのために静的定義を使用することもできます。分離されたファイルを使用する場合は、--dhcp-hostsfile コマンドラインオプションを通じて、dnsmasq がそのファイルを見るようにしてください。後者のアプローチの利点は、エントリを再読み込みするためには SIGHUP シグナルを送信する (またはサービスを reload する) だけで済む点です。一方で設定ファイルに定義すると、サービスの完全な再開 (restart) が必要になります。

dhcp-host パラメータの文法はとても広いので、文法についてのさらなる情報はマニュアルページか設定ファイルを参照してください。

使い方

この節では、dnsmasq サービスのさまざまな利用シナリオ (保守および運用タスク) を扱います。

リースをリセットする

ネットワークインターフェースを更新して、MAC アドレスが変わってしまった場合、意図した IP アドレスがすぐには得られないかもしれません。これは、dnsmasq サービスがこの IP アドレスを古い MAC アドレスに提供していて、このアドレスのリースが切れて再割り当てされるまで待とうとするからです。

dnsmasq サービスはリースを /var/lib/misc/dnsmasq.leases に保管します。リースをいち早く削除する必要がある場合は、dnsmasq サービスを停止し、dnsmasq.leases ファイルからリースを削除して、サービスを再度開始してください。

root #/etc/init.d/dnsmasq stop
root #nano -w /var/lib/misc/dnsmasq.leases
root #/etc/init.d/dnsmasq start

メイン以外の設定ファイルを再読み込みする

dnsmasq.conf ファイルの他に、dnsmasq サービスは以下のサービスのために外部定義を使用することができます:

  • DHCP ホスト設定エントリ (--dhcp-hostsfile コマンドラインオプションを通じて)
  • DHCP オプション (--dhcp-optsfile コマンドラインオプションを通じて)

これらのファイルが変更された場合、SIGHUP シグナルは dnsmasq にこれらの設定ファイルの再読み込みを実行させます。

メモ
resolv.conf ファイル群はデフォルトで dnsmasq によってポーリングされます; -n (--no-poll) コマンドラインオプションが設定されているか、no-poll 設定オプションが使用されている場合を除き、これらのファイルへの変更は自動的に反映されます。