LoginSignup
47
57

More than 3 years have passed since last update.

dnsmasqを使った内部用DNSサーバの構築

Last updated at Posted at 2018-11-01

目的

小規模の内部用(プライベート)用のDNSをお手軽に立てたいので、dnsmasqを使用することにしました。
dnsmasqは主に以下の機能があります。

  • 簡易DNS機能
  • DHCP機能

今回は使用しないのでDHCP機能はOFF。

dnsmasqの利点

  • ホストファイル(形式のファイル)でDNSの設定ができる
  • 設定がお手軽

環境

CentOS 7

# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

インストール

# yum -y install dnsmasq

詳細を見る限りはPXE boot用のサーバも立てれそうですね。

# rpm -qi dnsmasq
Name        : dnsmasq
Version     : 2.76
Release     : 5.el7
Architecture: x86_64
Install Date: 2018年10月22日 21時48分16秒
Group       : System Environment/Daemons
Size        : 599747
License     : GPLv2 or GPLv3
Signature   : RSA/SHA256, 2018年04月25日 19時57分38秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : dnsmasq-2.76-5.el7.src.rpm
Build Date  : 2018年04月11日 09時53分22秒https://www.netflix.com/watch/80193444?trackId=14170044&tctx=0%2C0%2Ca10330a1-1690-4222-b019-31df947e3cd0-821344456%2C92037450-d886-4632-88e7-6f886e47e33b_32240913X19XX1541076134697%2C92037450-d886-4632-88e7-6f886e47e33b_ROOT
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.thekelleys.org.uk/dnsmasq/
Summary     : A lightweight DHCP/caching DNS server
Description :
Dnsmasq is lightweight, easy to configure DNS forwarder and DHCP server.
It is designed to provide DNS and, optionally, DHCP, to a small network.
It can serve the names of local machines which are not in the global
DNS. The DHCP server integrates with the DNS server and allows machines
with DHCP-allocated addresses to appear in the DNS with names configured
either in each host or in a central configuration file. Dnsmasq supports
static and dynamic DHCP leases and BOOTP for network booting of diskless
machines.

設定ファイル

dnsmasqの設定ファイルの主な設定箇所は以下です。
今回はhostsファイルを直接使わずに、別のファイルを用意するやり方です。

/etc/dnsmasq.conf
port=53
# -> Port番号設定
no-hosts
# -> デフォルトの /etc/hosts をDNSのレコードとして使用しない
addn-hosts=/etc/hosts-dnsmasq
# -> 読み込むホストファイルを指定
expand-hosts
# -> ドメイン名を自動的に付加する
domain=icebird.local
# -> ローカルのドメイン名を設定する
domain-needed
# -> 外部のDNSへ問い合わせをする場合は、ホスト名では問い合わせない
bogus-priv
# -> プライベートIPアドレスの逆引を上位のDNSへ問い合わせない(無駄なので)

ホストファイル

/etc/hosts-dnsmasq
192.168.1.200 dns

サービス起動

systemctl start dnsmasq
systemctl enable dnsmasq

firewalld はポートに合わせて適切に設定しておいてね。

設定の更新

ホストファイルや設定ファイルを更新した場合は、reloadで反映されます。

systemctl restart dnsmasq

名前解決確認

dig結果
# dig @localhost dns 

; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @localhost dns
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62888
;; 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: 0 msec
;; SERVER: ::1#53(::1)
;; WHEN: 木 11月 01 23:09:54 JST 2018
;; MSG SIZE  rcvd: 48

# dig @localhost dns.icebird.local

; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @localhost dns.icebird.local
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10675
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
dns.icebird.local.  0   IN  A   192.168.1.200

;; Query time: 0 msec
;; SERVER: ::1#53(::1)
;; WHEN: 木 11月 01 23:10:20 JST 2018
;; MSG SIZE  rcvd: 62

インターネット上のドメイン名を名前解決した結果

デフォルトでは /etc/resolv.conf を読み込みこみ、そのDNSサーバへForwardします。

# dig @localhost www.google.com

; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @localhost www.google.com
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1018
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.google.com.            IN  A

;; ANSWER SECTION:
www.google.com.     250 IN  A   172.217.161.68

;; Query time: 10 msec
;; SERVER: ::1#53(::1)
;; WHEN: 木 11月 01 23:10:34 JST 2018
;; MSG SIZE  rcvd: 59

tcpdumpでパケットの確認

実際に dnsmsq がどう名前解決しているのか調べてるためにパケットキャプチャを採取します。
tcpdumpコマンドを流しながらdigコマンドを使用してください。

tcpdump
# tcpdump -n -i any dst port 53 
23:24:01.264241 IP6 ::1.58302 > ::1.domain: 380+ [1au] A? www.google.com. (43)
23:24:01.264336 IP6 2408:10:96ae:ac00:9190:32a5:3fda:ab3.19769 > 2408:10:96ae:ac00:225:dcff:fe3a:fd22.domain: 61678+ [1au] A? www.google.com. (43)
dig
# dig @localhost www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @localhost www.google.com
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 380
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.google.com.            IN  A

;; ANSWER SECTION:
www.google.com.     286 IN  A   172.217.161.68

;; Query time: 13 msec
;; SERVER: ::1#53(::1)
;; WHEN: 木 11月 01 23:24:01 JST 2018
;; MSG SIZE  rcvd: 59

OSの/etc/resolv.confの設定は以下です。
設定されたアドレスに問い合わせていますね。

/etc/resolv.conf
...
nameserver 2408:10:96ae:ac00:225:dcff:fe3a:fd22
47
57
1

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
47
57