Ubuntu 20.04 に Dnsmasq をインストールする


はじめに

Ubuntu 20.04 に Dnsmasq をインストールするためのメモ。

環境

$ hostnamectl status
   Static hostname: ---
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ---
           Boot ID: ---
    Virtualization: microsoft
  Operating System: Ubuntu 20.04.4 LTS
            Kernel: Linux 5.4.0-110-generic
      Architecture: x86-64

001. Dnsmasqをインストール

Dnsmasq をインストールする。

$ sudo apt -y install dnsmasq

002. コンフィグファイルを編集

「/etc/dnsmasq.conf」のバックアップを作成する。

$ sudo cp -av /etc/dnsmasq.conf{,.`date +%Y%m%d%H%M%S`}

「/etc/dnsmasq.conf」に設定を実施します。

$ sudo tee /etc/dnsmasq.conf << EOF >/dev/null
# ポート番号
port=53
# メインなしは、上位DNSサーバに問い合わせしない
domain-needed
# プライベートIPアドレスの逆引きを上位ネームサーバに問い合わせしない
bogus-priv
# ローカルのドメインを指定
local=/long-in.local/
# resolv.conf のネームサーバを上から順に問い合わせ
strict-order
# /etc/hosts 無視する
no-hosts
# DHCP機能を無効化
no-dhcp-interface=eth0
# 独自のhostsファイルを指定
addn-hosts=/etc/dnsmasq-hosts
# 上位ネームサーバの情報
resolv-file=/etc/dnsmasq_resolv.conf
EOF

独自のhostsファイルを作成する。

$ sudo tee /etc/dnsmasq-hosts << EOF >/dev/null
192.168.1.20 test test.local
EOF

上位ネームサーバを追記する。

$ sudo tee /etc/dnsmasq_resolv.conf << EOF >/dev/null
nameserver 8.8.8.8
nameserver 1.1.1.1
EOF

構文チェックを実施する。

$ sudo dnsmasq --test
dnsmasq: syntax check OK.

設定を反映するためにサービスを再起動する。

$ sudo systemctl restart dnsmasq systemd-resolved

003. 動作確認

確認には、digコマンドを利用するのでインストールする。

$ sudo apt -y install dnsutils

digコマンドで test.local の名前解決を実施する。

$ dig test.local

; <<>> DiG 9.16.1-Ubuntu <<>> test.local
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58235
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;test.local.               IN      A

;; ANSWER SECTION:
test.local.        0       IN      A       192.168.1.20

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat May 14 18:06:00 UTC 2022
;; MSG SIZE  rcvd: 60

004. クライアントの設定

Dnsmasq で構築した簡易DNSサーバを別のサーバから利用する。

「/etc/systemd/resolved.conf」のバックアップを取得する。

$ sudo cp -av /etc/systemd/resolved.conf{,.`date +%Y%m%d%H%M%S`}

「/etc/systemd/resolved.conf」にネームサーバを明示的に設定する。

$ sudo vim /etc/systemd/resolved.conf
[Resolve]
DNS=<DnsmasqをインストールしたサーバのIPアドレス>
DNSStubListener=no

シンボリックリンクを書き換える。

$ sudo ln -fs /run/systemd/resolve/resolv.conf /etc/resolv.conf

設定の反映のためにサービスを再起動する。

$ sudo systemctl restart systemd-resolved

正しく、ネームサーバが設定されているのか確認する。

$ sudo cat /etc/resolv.conf 
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver <DnsmasqをインストールしたサーバのIPアドレス>
nameserver 8.8.8.8
nameserver 1.1.1.1
# Too many DNS servers configured, the following entries may be ignored.
nameserver 192.168.1.1
nameserver 2001:ce8:0:1734::1
nameserver 2001:ce8:0:1534::1