ユーザ用ツール

サイト用ツール


サイドバー

Index

はじめてのおつかい






DokuWiki整形記法


PlayGround



serverapps:dns:dnsmasq

Dnsmasq

Dnsmasqは軽量で比較的容易に設定できるDNSサーバのフォワーダとDHCPサーバをもつソフトウェアである。(wikipediaより)
DNSのキャッシュサーバとTFTPサーバの機能もあります。
と言うことで、今までisc-dhcpdとBindの組み合わせを使用していたが、大げさなので社内はこっちに移行してみる。

Install

# pkg install dnsmasq

名前解決

Dnsmasqは名前解決に/etc/hostsまたはDHCPを利用します。
解決できない場合には/etc/resolve.confを参照して上位DNSに問い合わせます。

/etc/resolv.conf

nameserver 127.0.0.1
nameserver 上位DNS IP

/etc/hosts

192.168.1.254 gateway gateway.local.example.com

DNS設定

/usr/local/etc/dnsmasq.conf

# Never forward plain names (without a dot or domain part)through
domain-needed ← ドメインの付加されていない名前解決は上位DNSに転送しない

# Never forward addresses in the non-routed address spaces.
bogus-priv ← プライベートIPの逆引きは上位DNSに転送しない

# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts ← domain= で指定されたドメインで補完する

# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
domain=local.example.com ← 補完するドメイン

# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
local=/local.example.com/ ← localなドメインを指定します。このドメインは/etc/hosts か DHCP でのみ解決します。

特定のドメインだけ別のネームサーバに問い合わせる

int.example.comドメインは 192.168.2.254 に問い合わせる。
192.168.2.xxxの逆引きは 192.168.2.254 に問い合わせる。

/usr/local/etc/dnsmasq.conf

# Change this line if you want dns to get its upstream servers from
# somewhere other that /etc/resolv.conf
resolv-file=/etc/dnsmasq.resolv.conf

# By  default,  dnsmasq  will  send queries to any of the upstream
# servers it knows about and tries to favour servers to are  known
# to  be  up.  Uncommenting this forces dnsmasq to try each query
# with  each  server  strictly  in  the  order  they   appear   in
# /etc/resolv.conf
strict-order

# Add other name servers here, with domain specs if they are for
# non-public domains.
server=/int.example.com/192.168.2.254


# Add routing PTR queries to nameservers
server=/2.168.192.in-addr.arpa/192.168.2.254
# /etc/dnsmasq.resolv.conf
# 上位ISPのDNSサーバ
nameserver 8.8.8.8
# /etc/resolv.conf
domain laputa.example.com
nameserver 127.0.0.1

hosts以外のファイルから追加

/usr/local/etc/dnsmasq.conf に設定することでhosts以外のファイルに固定したIPを設定できる

addn-hosts=/path/to/adding-hosts-file

DNS キャッシュの設定

/usr/local/etc/dnsmasq.conf

listen-address=127.0.0.1
listen-address=192.168.1.1    # Example IP ← これがないと、外からの問い合わせに答えません

DHCP設定

/usr/local/etc/dnsmasq.confにこんな感じ

# dhcp-range=先頭IPアドレス,末尾IPアドレス,リース期間(単位:mは分、hは時間、省略で秒。デフォルトは1時間)
dhcp-range=192.168.1.101,192.168,1.200,30m ← アドレスの範囲

# dhcp-host=MACアドレス[,ホスト名][,IPアドレス][,リース期間] ← 特定のIPを振る
dhcp-host=01:23:45:67:89:ab,192.168.1.123

# dhcp-option=番号,値 ← 各種オプション
dhcp-option=3,192.168.1.254       ← デフォルトゲートウェイ
dhcp-option=42,192.168.1.1,192.168.1.2  ← NTPサーバ
    OR
dhcp-option=option:router,192.168.1.254
dhcp-option=option:ntp-server,192.168.1.1,192.168.1.2

細かいオプションはこちら→を参照DHCP and BOOTP parameters1)2)

または、

# dnsmasq --help dhcp
Known DHCP options:
  ...<省略>...
  3 router
  6 dns-server
  7 log-server
  9 lpr-server
  ...<省略>...
 42 ntp-server
 44 netbios-ns
 45 netbios-dd
 46 netbios-nodetype
 47 netbios-scope
  ...<省略>...

複数のI/F

interface=eth1
interface=eth2
interface=eth3

dhcp-range=tag:eth1,10.0.0.10,10.0.0.250,30m
dhcp-range=tag:eth2,10.0.1.10,10.0.1.250,30m
dhcp-range=tag:eth3,10.0.2.10,10.0.2.250,30m

dhcp-option=tag:eth1,option:router,10.0.0.1
dhcp-option=tag:eth2,option:router,10.0.1.1
dhcp-option=tag:eth3,option:router,10.0.2.1

リース中のIPリスト

# cat /var/db/dnsmasq.leases 
1367930996 aa:bb:cc:dd:ee:60 192.168.0.aaa hostname1 aa:bb:cc:dd:ee:60
1367930932 aa:bb:cc:dd:ee:b1 192.168.0.bbb hostname2 aa:bb:cc:dd:ee:b1
1367929670 aa:bb:cc:dd:ee:3d 192.168.0.ccc hostname3 aa:bb:cc:dd:ee:3d

TFTPサーバ

enable-tftp
tftp-root=/tftpboot

起動

/etc/rc.confに

dnsmasq_enable="YES"

を追加

参考

1)
tagを使ってオプションを分けたりできるけど、そこら辺は自分で調べてね
2)
名前の設定も可能なんだけど、どれがどの名前でサポートされているのやら。Exampleを見るしか手がないので番号の方が無難かも
serverapps/dns/dnsmasq.txt · 最終更新: 2022/08/14 16:19 by hayashi