macで軽量DNSサーバ dnsmasqを使う

## 背景

macでローカルにdnsを建てて名前解決をしたい場面が出てその際に行った作業メモ

## dnsmasqとは

dnsmasqとは小規模ネットワーク向けのDNS/DHCP/TFTPサーバです。
同じDNSサーバとしてbindが有名ですがbindに比べて導入コストや設定方法の簡易さと言ったメリットがあげあれます。
DNSクエリをキャッシュすることで以前に訪れたことのあるサイトへの接続速度を向上が見込めます。

[dnsmasq/dnsmasq](https://github.com/dnsmasq/dnsmasq)

## インストール

[dnsmasq](https://formulae.brew.sh/formula/dnsmasq)

brewを使ったインストールで完了です。
設定はdnsmasq.confというファイルを使用するので作成します。

``` bash
$ brew install dnsmasq
$ touch /usr/local/etc/dnsmasq.conf
$ sudo ln -s /usr/local/etc/dnsmasq.conf /etc/dnsmasq.conf
```

dnsをローカルに設定します。(hostsより優先度を高くすれば良いのみで既に設定がある場合は優先度の高い位置へ挿入すればOKです)

``` bash
$ sudo vi /etc/resolv.conf

nameserver 127.0.0.1
nameserver 192.168.0.1
nameserver 8.8.8.8
```

## macでdnsキャッシュのクリア

hosts変えたりした際に動作が期待通りにならないことがある。
その際に下記を実行することでdnsのキャッシュをクリアすることができる。

```
$ sudo killall -HUP mDNSResponder
```

## usage

``` 
nsmasq --help
Usage: dnsmasq [options]

Valid options are:
-a, --listen-address=<ipaddr>                          Specify local address(es) to listen on.
-A, --address=/<domain>/<ipaddr>                       Return ipaddr for all hosts in specified domains.
-b, --bogus-priv                                       Fake reverse lookups for RFC1918 private address ranges.
-B, --bogus-nxdomain=<ipaddr>                          Treat ipaddr as NXDOMAIN (defeats Verisign wildcard).
-c, --cache-size=<integer>                             Specify the size of the cache in entries (defaults to 150).
-C, --conf-file=<path>                                 Specify configuration file (defaults to /usr/local/etc/dnsmasq.conf).
-d, --no-daemon                                        Do NOT fork into the background: run in debug mode.
-D, --domain-needed                                    Do NOT forward queries with no domain part.
-e, --selfmx                                           Return self-pointing MX records for local hosts.
-E, --expand-hosts                                     Expand simple names in /etc/hosts with domain-suffix.
-f, --filterwin2k                                      Don't forward spurious DNS requests from Windows hosts.
-F, --dhcp-range=<ipaddr>,...                          Enable DHCP in the range given with lease duration.
-g, --group=<groupname>                                Change to this group after startup (defaults to dip).
-G, --dhcp-host=<hostspec>                             Set address or hostname for a specified machine.
-h, --no-hosts                                         Do NOT load /etc/hosts file.
-H, --addn-hosts=<path>                                Specify a hosts file to be read in addition to /etc/hosts.
-i, --interface=<interface>                            Specify interface(s) to listen on.
-I, --except-interface=<interface>                     Specify interface(s) NOT to listen on.
-j, --dhcp-userclass=set:<tag>,<class>                 Map DHCP user class to tag.
-J, --dhcp-ignore=tag:<tag>...                         Don't do DHCP for hosts with tag set.
-k, --keep-in-foreground                               Do NOT fork into the background, do NOT run in debug mode.
-K, --dhcp-authoritative                               Assume we are the only DHCP server on the local network.
-l, --dhcp-leasefile=<path>                            Specify where to store DHCP leases (defaults to /usr/local/var/lib/misc/dnsmasq/dnsmasq.leases).
-L, --localmx                                          Return MX records for local hosts.
-m, --mx-host=<host_name>,<target>,<pref>              Specify an MX record.
-M, --dhcp-boot=<bootp opts>                           Specify BOOTP options to DHCP server.
-n, --no-poll                                          Do NOT poll /etc/resolv.conf file, reload only on SIGHUP.
-N, --no-negcache                                      Do NOT cache failed search results.
-o, --strict-order                                     Use nameservers strictly in the order given in /etc/resolv.conf.
-O, --dhcp-option=<optspec>                            Specify options to be sent to DHCP clients.
-p, --port=<integer>                                   Specify port to listen for DNS requests on (defaults to 53).
-P, --edns-packet-max=<integer>                        Maximum supported UDP packet size for EDNS.0 (defaults to 4096).
-q, --log-queries                                      Log DNS queries.
-Q, --query-port=<integer>                             Force the originating port for upstream DNS queries.
-R, --no-resolv                                        Do NOT read resolv.conf.
-r, --resolv-file=<path>                               Specify path to resolv.conf (defaults to /etc/resolv.conf).
-S, --server=/<domain>/<ipaddr>                        Specify address(es) of upstream servers with optional domains.
-s, --domain=<domain>[,<range>]                        Specify the domain to be assigned in DHCP leases.
-t, --mx-target=<host_name>                            Specify default target in an MX record.
-T, --local-ttl=<integer>                              Specify time-to-live in seconds for replies from /etc/hosts.
-u, --user=<username>                                  Change to this user after startup. (defaults to nobody).
-U, --dhcp-vendorclass=set:<tag>,<class>               Map DHCP vendor class to tag.
-v, --version                                          Display dnsmasq version and copyright information.
-V, --alias=<ipaddr>,<ipaddr>,<netmask>                Translate IPv4 addresses from upstream servers.
-W, --srv-host=<name>,<target>,...                     Specify a SRV record.
-w, --help                                             Display this message. Use --help dhcp or --help dhcp6 for known DHCP options.
-x, --pid-file=<path>                                  Specify path of PID file (defaults to /usr/local/var/run/dnsmasq/dnsmasq.pid).
-X, --dhcp-lease-max=<integer>                         Specify maximum number of DHCP leases (defaults to 1000).
-y, --localise-queries                                 Answer DNS queries based on the interface a query was sent to.
-Y, --txt-record=<name>,<txt>[,<txt]                   Specify TXT DNS record.
-z, --bind-interfaces                                  Bind only to interfaces in use.
-Z, --read-ethers                                      Read DHCP static host information from /etc/ethers.
```

## 参考サイト

* [dnsmasq](https://wiki.archlinux.jp/index.php/Dnsmasq)
* [Dnsmasq 軽量 DNS サーバ構築（内部向け＋リゾルバキャッシュ）](https://server.etutsplus.com/dnsmasq-setup-internal-dns-with-resolver-cache/)