LoginSignup
16
8

More than 1 year has passed since last update.

dnsmasqでサクッとDNSサーバ構築

Last updated at Posted at 2021-12-17

CentOSに構築していきます。

$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

まずはdnsmasqのインストール

$ yum -y install dnsmasq
...

次にdnsmasqの設定ファイル。主要な項目だけ書き出してみました。

/etc/dnsmasq.conf
# DNSのポートを指定。何もしなければ53が使われる。
# port=5353

# デフォルトの/etc/hostsを参照したくないのであれば記述
no-hosts

# DHCP機能を使わない場合
no-dhcp-interface=eth0

# hostsとして参照させたいファイルを指定
addn-hosts=/etc/hosts-dnsmasq

# ドメイン名を自動的にhostsに付与するか
expand-hosts

# DNSSECバリデーションを有効化
dnssec

# ログの出力先
log-facility=/var/log/dnsmasq/dnsmasq.log

# DNSクエリのログを取る
log-queries

# ローカルドメイン名
domain=thekelleys.org.uk

# このようにサブネット指定で複数のローカルドメインを指定することも可能
domain=wireless.thekelleys.org.uk,192.168.2.0/24

# 上位サーバへ問い合わせる際は、ドメイン名が必須
domain-needed

# 上位サーバに対して、プライベートIPアドレスの逆引き要求をしない
bogus-priv

# 上位DNSの設定ファイル。
resolv-file=/etc/dnsmasq_resolv.conf

hostsファイルを作成。IPは名前解決したいものを指定。今回は適当に。

/etc/hosts-dnsmasq
192.168.1.200 test-dns

次に、上位DNSの指定を書いたファイルを作成する。

/etc/dnsmasq_resolv.conf
nameserver 8.8.8.8

dnsmasqの開始

$ systemctl start dnsmasq
$ systemctl enable dnsmasq
Created symlink from /etc/systemd/system/multi-user.target.wants/dnsmasq.service to /usr/lib/systemd/system/dnsmasq
.service.

firewalld でdnsmasqのデフォルトポート(53)をオープンする。

firewall-cmd --add-service=dns --zone=public --permanent
firewall-cmd --reload

名前解決を確認

digを使うためbind-utilsをインストール

$ sudo yum -y install bind-utils

名前解決を行うDNSサーバを自分自身(localhost)に指定しつつ、先程設定したドメインに対して名前解決リクエストを行う。

$ dig @localhost test-dns
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.8 <<>> @localhost dns
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12024
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;dns.                           IN      A
;; ANSWER SECTION:
dns.                    0       IN      A       192.168.1.200
;; Query time: 1 msec
;; SERVER: ::1#53(::1)
;; WHEN: Fri Dec 17 15:13:36 UTC 2021
;; MSG SIZE  rcvd: 48

すると、Aレコードにhosts(etc/hosts-dnsmasq)ファイルで指定した192.168.1.200が表示されているのが確認できました。

これで、dnsmasqを設定したサーバのIPを他のネットワークからDNSサーバとして参照すれば、hostsに指定したIPに対して名前解決してくれるはずです。

16
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
8