ytooyamaのブログ

サーバ構築とか、仕事で発見したこととか、趣味のこととかを書いています。

dnsmasqで簡易的なDNSサーバーを構築

【14/10/3追記】
「dnsmasqで簡易的なDHCPサーバーを構築」はこちら
ytooyama.hatenadiary.jp

LinuxDNSというとBINDがすぐに思い浮かびます。
DNSサーバーを構築する時の選択肢は、最近は色々増えてきましたが、家の環境で名前解決したいような小規模な用途程度でBINDを構築するのはちょっとばっかし気が重いですよね。

そこでWebで色々調べたところ、dnsmasqというものが用途としてあっているようでした。例えばCentOS 7やFedoraなら次の手順で導入できます。
(以下、太字はコマンド)

■導入方法

Fedora/CentOS 7など

# yum install dnsmasq

Debian/Raspbian/Ubuntuなど

# apt update && apt install dnsmasq


■dnsmasqの設定方法

# vi /etc/dnsmasq.conf

# Listen on this specific port instead of the standard DNS port
# (53). Setting this to zero completely disables DNS function,
# leaving only DHCP and/or TFTP.
port=53 #コメントを外してポートを変更

# Never forward plain names (without a dot or domain part)
domain-needed #コメントを外す

# Never forward addresses in the non-routed address spaces.
bogus-priv #コメントを外す

# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts #コメントを外す

# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
#local=/localnet/
local=/local.tooyama.org/
(shorthostnameで補完するドメインを記述)

# 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=local.tooyama.org
(shorthostnameで補完するドメインを記述)

# vi /etc/hosts
DNSサーバーとするhostsファイルを編集)
...
192.168.1.3 www2.local.tooyama.org www2
192.168.1.3 ntp2.local.tooyama.org ntp2
192.168.1.6 www.local.tooyama.org www
(名前解決したいエントリを記述)

# systemctl restart dnsmasq
(dnsmasqサービスを再起動..CentOSの場合はserviceコマンドを使う)

# systemctl enable dnsmasq
(dnsmasqサービスを自動起動..CentOSの場合はchkconfigコマンドを使う)

■resolv.confの設定方法

以上の設定を行ってdnsmasqを再起動すると、ソフトウェアのバージョンの組み合わせによってdnsmasqが/etc/resolv.confの内容を上書きすることがあるため、「Arch Linux Wiki - resolv.conf」を参考に/etc/resolv.conf.headを作成して記述します。

ex.

# vi /etc/resolv.conf.head

search local.tooyama.org
nameserver 208.67.222.222
nameserver 208.67.220.220
nameserver 192.168.1.1

ファイアウォール
ポート53を解放します。

ファイアウォールの設定はCentOS 7以前の場合はiptablesを直接いじるか、system-config-firewallコマンドで設定します。FedoraCentOS
7以降の場合はfirewall-cmdで設定します。DebianUbuntuなどでは標準設定のままであれば自動的に必要なポートが開放されるので特に設定は不要です。

# firewall-cmd --add-port=53/tcp
# firewall-cmd --add-port=53/udp
# firewall-cmd --permanent --add-port=53/tcp
# firewall-cmd --permanent --add-port=53/udp
ファイアウォールコマンドでポート開放)

これだけです。あとはクライアントのDNSの設定を構築したサーバーに設定すれば、簡易DNSサーバーの完成です。

■名前解決したいエントリーを追加する
名前解決したいエントリーを追加するには、dnsmasqの/etc/hostsに記述していきます。追加した内容を反映させるには、dnsmasqサービスを再起動するだけです。本当に簡単ですね。

■使用例

$ ping -c 1 www2.local.tooyama.org
PING www2.local.tooyama.org (192.168.1.3): 56 data bytes
64 bytes from 192.168.1.3: icmp_seq=0 ttl=64 time=0.297 ms

--- www2.local.tooyama.org ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.297/0.297/0.297/0.000 ms

DHCPもたてたい
まだ実際に試していないけど、dnsmasqでできるらしいです。

【参考にしたサイト】

このブログサイトはJavaScriptを使っていますが、読み込んでいるJavaScriptは全てはてなが提供しているものであり、筆者が設置しているものではありません。