dnsmasq設定メモ
背景
内部DNSサーを使う検証をする状況が発生。 出社して環境を構築するという考えもよぎりましたが、単純に疑似的に作った社内サーバ相当のWebサーバの名前を解決だけできる簡易なDNSサーバが必要というだけであったため、 家で使えるパソコン(Ubuntu18.04LTSインストール済)にDNSサーバを導入することにしました。
DNSの選定
単純な構成なのでなんでもよかった。 最近はDNSサーバを独自に構築していなかったので、BIND、Unboundあたりかな?と何も考えていなかったが、 後輩からdnsmasqが楽だという話を聞いたので今回はdnsmasqを試すこととした。
インストール
user@ns:~$ sudo apt-get install dnsmasq Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: resolvconf The following NEW packages will be installed: dnsmasq 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 16.2 kB of archives. After this operation, 73.7 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 dnsmasq all 2.79-1 [16.2 kB] Fetched 16.2 kB in 1s (26.6 kB/s) Selecting previously unselected package dnsmasq. (Reading database ... 29300 files and directories currently installed.) Preparing to unpack .../dnsmasq_2.79-1_all.deb ... Unpacking dnsmasq (2.79-1) ... Setting up dnsmasq (2.79-1) ... Created symlink /etc/systemd/system/multi-user.target.wants/dnsmasq.service → /lib/systemd/system/dnsmasq.service. invoke-rc.d: could not determine current runlevel Processing triggers for ureadahead (0.100.0-21) ... Processing triggers for systemd (237-3ubuntu10.42) ...
local DNS stub listenerの無効化
調べているとUbuntu16.10以降はlocal DNS stub listenerというものが動いており、こちらが先に53番ポートを利用してしまうとのこと。 無効にしました。
/etc/systemd/resolved.conf # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Entries in this file show the compile time defaults. # You can change settings by editing this file. # Defaults can be restored by simply deleting this file. # # See resolved.conf(5) for details [Resolve] #DNS= #FallbackDNS= #Domains= #LLMNR=no #MulticastDNS=no #DNSSEC=no #Cache=yes #DNSStubListener=yes DNSStubListener=no デフォルトではDNSStubListener=yesなのでnoに変更する。
設定保存したらsystemd-resolveをリスタートする。
sudo systemctl restart systemd-resolved
dnsmasqの設定
53番ポートの競合について解決したのでdnsmasqの設定を実施。
パラメータ | 説明 |
---|---|
port=53 | dnsmasqのlistenポート |
bogus-priv | プライベートIPアドレスの逆引きを上位DNSサーバに転送しない |
local=/umb003.net/ | ドメイン名の定義(umb003.netという内部のみ利用できるドメインを指定) |
resolv-file=/etc/resolv.conf | 上位のDNSのIPアドレスを指定。dnsmasq自身で解決できない場合に参照する。 |
strict-order | resolv-fileに指定しているIPを上から順番に問い合わせ |
log-querie | ログを出力する |
log-facility=/var/log/dnsmasq/dnsmasq.log | ログの出力場所 |
上位DNSの設定
/etc/resolve.confに上位DNSサーバを指定。 今回はCloudflareのパブリックDNSサーバを指定。
nameserver 1.1.1.3
DNSレコードの設定
/etc/hostsファイルに名前解決させるIPアドレスとホスト名のエントリを追加。 設定した内部ドメインは /etc/hosts を参照し、その他は上位のDNSサーバー(resolve.confに書かれているもの)に問い合わせて結果を返します。
192.168.112.10 ns.umb003.net 192.168.112.3 ws.umb003.net
これで完成。
digコマンドで動作確認
内部サーバ向け
$ dig ws.umb003.net ; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> ws.umb003.net ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52363 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;ws.umb003.net. IN A ;; ANSWER SECTION: ws.umb003.net. 0 IN A 192.168.112.3 ;; Query time: 2 msec ;; SERVER: 192.168.112.10#53(192.168.112.10) ;; WHEN: Sun Aug 23 23:08:34 JST 2020 ;; MSG SIZE rcvd: 58
外部サーバ向け
dig yahoo.co.jp ; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> yahoo.co.jp ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55779 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ;; QUESTION SECTION: ;yahoo.co.jp. IN A ;; ANSWER SECTION: yahoo.co.jp. 300 IN A 182.22.59.229 yahoo.co.jp. 300 IN A 183.79.135.206 ;; Query time: 11 msec ;; SERVER: 192.168.112.10#53(192.168.112.10) ;; WHEN: Sun Aug 23 23:08:41 JST 2020 ;; MSG SIZE rcvd: 83
サーバログ確認
dnsmasq.confに設定したログ(/var/log/dnsmasq/dnsmasq.log)を見るとこんな出力です。 NODATA-IPv6とあるのはそのドメイン名には指定されたタイプのリソースレコードは存在しないという意味です。 AAAAレコードをクライアントから問い合わせしてもそれは存在しませんとサーバが返事しています。
Aug 23 14:12:17 dnsmasq[1410]: query[PTR] 10.112.168.192.in-addr.arpa from 192.168.112.5 Aug 23 14:12:17 dnsmasq[1410]: /etc/hosts 192.168.112.10 is ns.umb003.net Aug 23 14:12:17 dnsmasq[1410]: query[A] ws.umb003.net from 192.168.112.5 Aug 23 14:12:17 dnsmasq[1410]: /etc/hosts ws.umb003.net is 192.168.112.3 Aug 23 14:20:32 dnsmasq[1410]: /etc/hosts ws.umb003.net is 192.168.112.3 Aug 23 14:20:32 dnsmasq[1410]: query[AAAA] ws.umb003.net from 192.168.112.5 Aug 23 14:20:32 dnsmasq[1410]: config ws.umb003.net is NODATA-IPv6
外部ドメインの場合はこんな出力。 forwarded www.yahoo.co.jp to 1.1.1.3とあるので DNSサーバが自分で解決できないので上位のDNSサーバ(1.1.1.3)に投げています。
Aug 23 14:19:15 dnsmasq[1410]: query[PTR] 10.112.168.192.in-addr.arpa from 192.168.112.5 Aug 23 14:19:15 dnsmasq[1410]: /etc/hosts 192.168.112.10 is ns.umb003.net Aug 23 14:19:15 dnsmasq[1410]: query[A] www.yahoo.co.jp from 192.168.112.5 Aug 23 14:19:15 dnsmasq[1410]: forwarded www.yahoo.co.jp to 1.1.1.3 Aug 23 14:19:15 dnsmasq[1410]: reply www.yahoo.co.jp is <CNAME> Aug 23 14:19:15 dnsmasq[1410]: reply edge12.g.yimg.jp is 183.79.217.124 Aug 23 14:19:15 dnsmasq[1410]: query[AAAA] www.yahoo.co.jp from 192.168.112.5 Aug 23 14:19:15 dnsmasq[1410]: cached www.yahoo.co.jp is <CNAME> Aug 23 14:19:15 dnsmasq[1410]: forwarded www.yahoo.co.jp to 1.1.1.3 Aug 23 14:19:15 dnsmasq[1410]: reply www.yahoo.co.jp is <CNAME> Aug 23 14:19:15 dnsmasq[1410]: reply edge12.g.yimg.jp is NODATA-IPv6
おまけ(CloudflareのパブリックDNSについて)
今回の構成で使った上位DNSはCloudflareのパブリックDNSです。CloudflareのパブリックDNSは以下を参照下さい。
CloudflareのパブリックDNSは1.1.1.1が有名なのですが、 他にもマルウェアブロック機能付きの1.1.1.2とマルウェアとアダルトコンテンツをブロックする1.1.1.3(今回使ったもの)があります。
試しに有名なアダルトサイトのFanza(dmm.co.jp)の名前解決をしようとすると0.0.0.0で帰ってきます。
Aug 23 14:24:13 dnsmasq[1410]: query[PTR] 10.112.168.192.in-addr.arpa from 192.168.112.5 Aug 23 14:24:13 dnsmasq[1410]: /etc/hosts 192.168.112.10 is ns.umb003.net Aug 23 14:24:13 dnsmasq[1410]: query[A] www.dmm.co.jp from 192.168.112.5 Aug 23 14:24:13 dnsmasq[1410]: forwarded www.dmm.co.jp to 1.1.1.3 Aug 23 14:24:13 dnsmasq[1410]: reply www.dmm.co.jp is 0.0.0.0 Aug 23 14:24:13 dnsmasq[1410]: query[AAAA] www.dmm.co.jp from 192.168.112.5 Aug 23 14:24:13 dnsmasq[1410]: cached www.dmm.co.jp is ::
ブラウザで開こうとするとこんな感じです。
まとめ
dnsmasqはとてもシンプルにDNSサーバとして使えるのでちょっとした試験がしたいという方にはオススメです。 自分でもそんなに苦戦せずに構築できました。Centosでもほぼ同じ手順でできるのではないでしょうか。
構築するにあたりDNSがよくわかる教科書を読みましたが、非常にわかりやすく書かれていると思います。 自分も繰り返し読んで習熟するつもりです。