Dnsmasq

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Dnsmasq and the translation is 86% complete.
Outdated translations are marked like this.
Other languages:
Resources

dnsmasq是一个简单的 DHCP/DNS 服务器,它可以在一个小于 1000 个客户端的局域网中使用。其主要特点是容易配置的而且占用空间很少。此外,它还支持 IPv6。

Installation

USE 标记

选择适当的 USE 标记:

USE flags for net-dns/dnsmasq Small forwarding DNS server

auth-dns Add support for acting as an authorative DNS server.
conntrack Add support for Linux conntrack connection marking.
dbus Enable dbus support for anything that needs it (gpsd, gnomemeeting, etc)
dhcp Enable support for acting as a DHCP server.
dhcp-tools Install extra command line tools for manually managing DHCP leases.
dnssec Enable support DNSSEC validation and caching.
dumpfile Include code to dump packets to a libpcap-format file for debugging
id Whether report *.bind CHAOS info to clients, otherwise forward such requests upstream instead
idn Enable support for Internationalized Domain Names
inotify Enable inotify filesystem monitoring support
ipv6 Add support for IP version 6
libidn2 Enable support for Internationalized Domain Names, via net-dns/libidn2 rather than net-dns/libidn
loop Include functionality to probe for and remove DNS forwarding loops
lua Enable Lua scripting support
nettlehash Use hashing functions from dev-libs/nettle
nls Add Native Language Support (using gettext - GNU locale utilities)
script Enable support for calling scripts when leases change.
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
static !!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically
tftp Enables built in TFTP server for netbooting.

Emerge

下一步,安装 net-dns/dnsmasq 软件包:

root #emerge --ask net-dns/dnsmasq

配置

有很多资源都可以被用来改变dnsmasq的行为,它们包括:

  • 通过 /etc/conf.d/dnsmasq来提供的命令行选项
  • 主配置文件 (/etc/dnsmasq.conf)

服务

OpenRC

Add dnsmasq to the default runlevel if it needs to be started automatically:

root #rc-update add dnsmasq default

现在启动服务:

root #rc-service dnsmasq start

服务配置

/etc/conf.d/dnsmasq中,需要在启动时传递给dnsmasq后台驻留程序的命令行选项可以被配置。

文件 /etc/conf.d/dnsmasqdnsmasq服务配置范例
DNSMASQ_OPTS="--user=dnsmasq --group=dnsmasq -H /srv/virt/gentoo/hosts --max-cache-ttl=10"

主配置文件

dnsmasq的主配置文件为/etc/dnsmasq.conf。这个文件使用 key[=value] 语法而且是随安装包一起被提供出来的,它的文档非常全面,因此建议被阅读。在文件里或者通过命令行选项的设置,外部的资源是可以被引用的(例如一个DHCP主机文件)。

下面就是一个范例配置文件:

文件 /etc/dnsmasq.conf
# 只监听这个接口
interface=eth1
  
# 基本MAC地址来指定主机名
dhcp-host=00:1e:68:c2:ff:ee,endor,192.168.0.54,24h
  
# 任意其它的DHCP请求将从如下地址范围中获取IP地址
dhcp-range=eth1,192.168.0.100,192.168.0.120,12h
  
# 使能TFTP服务器并且为通过TFTP可以获取的文件设置根目录
enable-tftp
tftp-root=/var/lib/tftpboot
dhcp-boot=/pxelinux.0

配置完配置文件后,服务需要被重启——重载虽然是被支持的但它是为其它资源准备的。

root #/etc/init.d/dnsmasq restart

Hosts文件

dnsmasq使用 /etc/hosts 文件作为它提供DNS服务所需的资源之一,除非 -h (--no-hosts) 命令行参数被传递给服务进程。

如果/etc/hosts是最新的,dnsmasq服务需要接收一个SIGHUP信号来重新载入这些设置,这也可以通过初始化脚本“reload”命令来支持。

root #/etc/init.d/dnsmasq reload

这个行为也可以通过在配置文件中设置 no-hosts参数来禁止。

额外的hosts文件

为了DNS查询参考一个(额外的)hosts文件作为源是可能的。要实现这一点,增加-H /path/to/hostsfile (--addn-hosts=/path/to/hostsfile) 命令行选项。也可以传递一个目录,在这种情况下,被包括在这个目录下的所有文件将被当作额外的hosts文件。

类似于标准的hosts文件,一个SIGHUP信号会重新载入这个文件。

这个行为也可以通过在配置文件里的 addn-hosts参数进行设置。

上游的名称服务器们

缺省地,dnsmasq使用 /etc/resolv.conf 里定义的名称服务器们作为它自己的上游名称服务器。

通过 -r (--resolv-file) 命令行选项可以使用一个不同的文件。

这个行为也可以通过在配置文件中的 resolv-file 参数来进行设置。

特性

Dnsmasq支持DNS, TFTP, PXE,路由器广告和DHCP服务,所以它是一个给中小型网络的万用网管工具。

DNS服务

为了(只)提供DNS服务,首先要分辨所使用的“上游名称服务器”,如果这与在 /etc/resolv.conf 中定义的相同,那么就没什么其它步骤需要进行操作了。否则就需要通过 -r (--resolv-file)命令行参数让dnsmasq指向适当的 resolv.conf 文件。它的语法就是被 /etc/resolv.conf文件使用的方法,虽然dnsmasq只看"nameserver"的定义。

例如:

root #echo "nameserver 8.8.8.8" >> /etc/dnsmasq.conf.resolv

下面通过配置文件让dnsmasq指向这个文件:

文件 /etc/dnsmasq.conf配置一个定制化的主机名解析文件(resolve file)
resolv-file=/etc/dnsmasq.conf.resolv

为了验证服务正在运行(在配置文件被改动并重启之后),使用 dig 命令(被net-dns/bind-tools所提供),询问DNS服务器(在下面的例子中是在本地运行)来解析一个本地的或者远程的地址:

user $dig @localhost +short www.gentoo.org
www-bytemark-v4v6.gentoo.org.
89.16.167.134

DNSSEC

当Dnsmasq透传数据时可以校验DNSSEC数据。这个可以通过在配置文件中添加下面这些行来实现:

文件 /etc/dnsmasq.confEnabling DNSSEC
# DNSSEC setup
dnssec
trust-anchor=.,19036,8,2,49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5
trust-anchor=.,20326,8,2,E06D44B80B8F1D39A95C0B0D7C65D08458E880409BBC683457104237C7F8EC8D
dnssec-check-unsigned
  1. Replies which are not DNSSEC signed may be legitimate, because the domain
  2. is unsigned, or may be forgeries. Setting this option tells dnsmasq to
  3. check that an unsigned reply is OK, by finding a secure proof that a DS
  4. record somewhere between the root and the domain does not exist.
  5. The cost of setting this is that even queries in unsigned domains will need
  6. one or more extra DNS queries to verify.

dnssec-check-unsigned }}

被信任的锚(anchor)可以在on the iana.org site被找到。在这个改变了之后如果校验失败了,dnsmasq将会返回SERVFAIL和“没有DNS数据”。如果校验成功,它会设置“ad”标志。如果这个域不支持DNSSEC,dnsmasq的行为会照常。

DHCP服务

为了使能dnsmasq的DHCP服务,使用 dhcp-range 配置设置。

例如,启用具有无限租期路由广播(RA)的IPv6 地址配置文件,并且 IPv4 地址配置文件也具有无限租期:

文件 /etc/dnsmasq.conf使能IPv6和IPv4租用
dhcp-range=2001:db8:81:e2::,ra-only,infinite
dhcp-range=192.168.100.100,192.168.100.149,infinite

为已知主机定义静态地址是可能的,或者通过主配置文件(dhcp-host=设置) ,或者通过一个单独的文件。如果要使用单独的文件,通过 --dhcp-hostsfile命令行选项让dnsmasq指向它。后面方法的好处是仅发送一个SIGHUP信号(或者重载服务)就可以重新读取那些记录,而在主配置文件中的定义则需要来一次完整的服务重启才可以读取。

关于dhcp-host 参数语法的更多信息,请参考man page或配置文件,因为它的语法可以扩展得很多。

使用

这一节覆盖了dnsmasq服务不同的使用场景(维护和操作任务)。

重置租期

客户端由于升级了网络接口导致的MAC地址变化有可能造成它不能立即得到想要的IP地址。这是因为dnsmasq服务已经把这个IP地址赋予了老MAC地址,然后它就会等到这个地址过了租期再重新指定它。

dnsmasq服务存储它的租期信息在 /var/lib/misc/dnsmasq.leases这个位置。如果这个租约需要被更快地移除,您可以停止dnsmasq服务,从dnsmasq.leases文件中移除这个租约,然后再启动这个服务。

root #/etc/init.d/dnsmasq stop
root #nano -w /var/lib/misc/dnsmasq.leases
root #/etc/init.d/dnsmasq start

重启非主配置中的设置

相对于 dnsmasq.conf 主配置文件,dnsmasq服务可以为如下的服务使用外部的定义:

  • DHCP主机配置记录(通过--dhcp-hostsfile 命令行选项)
  • DHCP选项(通过--dhcp-optsfile 命令行选项)

当这些文件被修改后,一个SIGHUP信号就可以让dnsmasq重新装载这些配置文件。

附注
resolv.conf 文件缺省就会被dnsmasq使用;对这些文件的改变会自动被应用,除非-n (--no-poll) 命令行选项被设置或者no-poll 配置文件参数被使用。