Ubuntu 22.04LTS へ dnsmasq をインストールする

Ubuntu 22.04LTS へ dnsmasq をインストールする

流れ

  • systemd-resolved の停止
  • dnsmasq のインストール
  • dnsmasq の設定
  • dnsmasq の起動
  • 名前解決のテスト

systemd-resolved の停止

Ubuntu では systemd-resolved というスタブリゾルバが動作しており、53/UDP を Listen しています。
その為、dnsmasq をインストールしようとしても同じポートを利用しようとし、結果的にエラーになってしまいます。

# systemctl status systemd-resolved
● systemd-resolved.service - Network Name Resolution
     Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-05-03 17:28:38 JST; 3h 46min ago
(skip)

その為、予め systemd-resolved を停止しておきます。

systemctl stop systemd-resolved
systemctl disable systemd-resolved

停止後は systemctl status systemd-resolved で「停止されたこと」を確認しておきます。

systemctl status systemd-resolved

dnsmasq のインストール

dnsmasq をインストールします。

apt -y install dnsmasq

バージョンの確認

# dnsmasq --version
Dnsmasq version 2.86  Copyright (c) 2000-2021 Simon Kelley
Compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset auth cryptohash DNSSEC loop-detect inotify dumpfile

This software comes with ABSOLUTELY NO WARRANTY.
Dnsmasq is free software, and you are welcome to redistribute it
under the terms of the GNU General Public License, version 2 or 3.

dnsmasq の設定

dnsmasq の設定ファイルは /etc/dnsmasq.conf にあります。 これを以下のように最低限、DNS サーバとして動作するように設定しておきます。

cat << 'EOF' > /etc/dnsmasq.conf
domain-needed
bogus-priv
local=/example.net/
expand-hosts
domain=example.net
EOF

名前解決用に /etc/hosts へ (検証用の) エントリーを追加しておきます。

cat << EOF >> /etc/hosts
192.0.2.1 sv1
192.0.2.2 sv2
192.0.2.3 sv3
EOF

dnsmasq の起動

dnsmasq を起動します。

systemctl start dnsmasq.service
systemctl enable dnsmasq.service

systemctl status dnsmasq.service を実行してサービスが正しく起動していることを確認しておきます。

systemctl status dnsmasq.service

名前解決のテスト

dig で名前解決出来ることを確認します。

# dig +short sv1.example.net. @127.0.0.1
192.0.2.1

dnsmasqカテゴリの最新記事