跳转至内容

dnsmasq

来自 ArchWiki

dnsmasq 提供了一个 DNS 服务器,一个支持 DHCPv6PXEDHCP 服务器,以及一个 TFTP 服务器。它被设计为轻量级且占资源少,适合资源受限的路由器和防火墙。dnsmasq 还可以配置为缓存 DNS 查询,以提高对先前访问过的站点的 DNS 查找速度。

安装

安装 dnsmasq 包。然后 启动/启用 dnsmasq.service

网络也需要重新启动,以便 DHCP 客户端可以创建新的 /etc/resolv.conf

配置

要配置 dnsmasq,请编辑 /etc/dnsmasq.conf。该文件包含解释选项的注释。有关所有可用选项,请参阅 dnsmasq(8)

注意 dnsmasq 的默认配置启用了其 DNS 服务器。如果您不需要它,您需要通过设置 port=0 来明确禁用它。

如果 dnsmasq 不用作本地 DNS 解析器,您可能还想 编辑 dnsmasq.service,使其不加载 nss-lookup.target

/etc/systemd/system/dnsmasq.service.d/no-nss-lookup-target.conf
[Unit]
Wants=
提示 要检查配置文件语法,请执行
$ dnsmasq --test

DNS 服务器

要将 dnsmasq 设置为单台计算机上的 DNS 缓存守护程序,请指定 listen-address 指令,添加本地主机 IP 地址。

listen-address=::1,127.0.0.1

要让此计算机侦听其 LAN IP 地址以供网络上的其他计算机使用。在这种情况下,建议您使用静态 LAN IP。例如:

listen-address=::1,127.0.0.1,192.168.1.1

您也可以分配一个网络接口。

interface=enp5s0

使用 cache-size=size 设置缓存的域名数量(默认为 150)。

cache-size=10000

要验证 DNSSEC,请加载 dnsmasq 包提供的 DNSSEC 信任锚,并设置 dnssec 选项。

conf-file=/usr/share/dnsmasq/trust-anchors.conf
dnssec

有关您可能想使用的更多选项,请参阅 dnsmasq(8)

DNS 地址文件和转发

配置 dnsmasq 后,您需要将本地主机地址添加为 /etc/resolv.conf 中唯一的名称服务器。这会使所有查询都发送到 dnsmasq。

由于 dnsmasq 是一个存根解析器而不是递归解析器,您必须设置转发到外部 DNS 服务器。这可以通过使用 openresolv 或在 dnsmasq 的配置中手动指定 DNS 服务器地址来自动完成。

openresolv

如果您的网络管理器支持 resolvconf,而不是直接修改 /etc/resolv.conf,您可以使用 openresolv生成 dnsmasq 的配置文件

编辑 /etc/resolvconf.conf 并将回送地址添加为名称服务器,然后配置 openresolv 以输出 dnsmasq 配置。

/etc/resolvconf.conf
# Use the local name server
name_servers="::1 127.0.0.1"
resolv_conf_options="trust-ad"

# Write out dnsmasq extended configuration and resolv files
dnsmasq_conf=/etc/dnsmasq-conf.conf
dnsmasq_resolv=/etc/dnsmasq-resolv.conf

运行 resolvconf -u 以创建配置文件。如果文件不存在,dnsmasq.service 将无法启动。

编辑 dnsmasq 的配置文件以使用 openresolv 生成的配置[1]

# Read configuration generated by openresolv
conf-file=/etc/dnsmasq-conf.conf
resolv-file=/etc/dnsmasq-resolv.conf
手动转发

首先,您必须将本地主机地址设置为 /etc/resolv.conf 中唯一的名称服务器。

/etc/resolv.conf
nameserver ::1
nameserver 127.0.0.1
options trust-ad

请确保按 域名解析#覆盖 /etc/resolv.conf 中的说明保护 /etc/resolv.conf 免受修改。

本文或章节可以合并到 NetworkManager#在连接中设置自定义 DNS 服务器 (nmcli / connection file)

注意: NetworkManager 配置超出了本文档的范围。(在 Talk:Dnsmasq 中讨论)

或者,NetworkManager 可以配置为使用以下命令自动生成特定连接的 /etc/resolv.conf 文件。

$ nmcli connection modify 'connection-name' ipv4.dns 127.0.0.1
$ nmcli connection modify 'connection-name' ipv4.dns-options trust-ad
$ nmcli connection modify 'connection-name' ipv4.ignore-auto-dns yes
$ nmcli connection modify 'connection-name' ipv6.dns ::1
$ nmcli connection modify 'connection-name' ipv6.dns-options trust-ad
$ nmcli connection modify 'connection-name' ipv6.ignore-auto-dns yes

然后 重新启动 NetworkManager.service

上游 DNS 服务器地址必须在 dnsmasq 的配置文件中指定为 server=server_address。还要添加 no-resolv,这样 dnsmasq 就不会不必要地读取 /etc/resolv.conf,后者只包含它自己的本地主机地址。

/etc/dnsmasq.conf
[...]
no-resolv

# Google's nameservers, for example
server=8.8.8.8
server=8.8.4.4
  • 此示例使用 Google Public DNS。请将其替换为您信任的 DNS 解析器。请参阅 域名解析#第三方 DNS 服务
  • dnsmasq 不支持连接到第三方 DNS 服务时所需的 加密协议。一种解决方法是运行一个支持加密 DNS 的独立 DNS 解析器,并将 dnsmasq 的查询转发给它。有关示例,请参阅 Stubby#本地 DNS 缓存配置

现在 DNS 查询将使用 dnsmasq 解析,只有在无法从缓存中回答查询时才会检查外部服务器。

添加自定义域名

您可以通过简单地添加来分配一个域名

address=/router/192.168.1.1

或者,如果您继续使用并将自定义域名添加到您(本地)网络中的主机

local=/home.arpa/
domain=home.arpa

在此示例中,可以 ping 一个主机/设备(例如,在您的 /etc/hosts 文件中定义的),其名称为 hostname.home.arpa

取消注释 expand-hosts 以将自定义域名添加到主机条目。

expand-hosts

没有此设置,您将不得不将域名添加到 /etc/hosts 条目中。

测试

要进行查找速度测试,请选择一个自 dnsmasq 启动以来尚未访问过的网站(drillldns 包的一部分)。

$ drill archlinux.org | grep "Query time"

再次运行该命令将使用缓存的 DNS IP,如果 dnsmasq 设置正确,则查找时间会更快。

$ drill archlinux.org | grep "Query time"
;; Query time: 18 msec
$ drill archlinux.org | grep "Query time"
;; Query time: 2 msec

要测试 DNSSEC 验证是否正常工作,请参阅 DNSSEC#测试本地验证解析器

DHCP 服务器

本文章或章节需要扩充。

原因:添加 IPv6 说明(在 Talk:Dnsmasq 中讨论)

默认情况下,dnsmasq 的 DHCP 功能是关闭的,如果您想使用它,必须将其打开。以下是重要的设置:

# Only listen to routers' LAN NIC.  Doing so opens up tcp/udp port 53 to localhost and udp port 67 to world:
interface=enp0s0

# dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with dynamic interfaces (assigning dynamic IPs).
# dnsmasq will discard world requests to them, but the paranoid might like to close them and let the kernel handle them.
# You may also need this option if you have other instances of dnsmasq running (eg. because of libvirtd)
bind-interfaces

# Optionally set a domain name
domain=example.org

# Set default gateway
dhcp-option=3,0.0.0.0

# Set DNS servers to announce
dhcp-option=6,0.0.0.0

# If your dnsmasq server is also doing the routing for your network, you can use option 121 to push a static route out.
# x.x.x.x is the destination LAN, yy is the CIDR notation (usually /24), and z.z.z.z is the host which will do the routing.
dhcp-option=121,x.x.x.x/yy,z.z.z.z,0.0.0.0/0,0.0.0.0

# Dynamic range of IPs to make available to LAN PC and the lease time.
# Ideally set the lease time to 5m only at first to test everything works okay before you set long-lasting records.
# The range of addresses here must lie within the address range assigned to the virtual interface.
dhcp-range=192.168.111.50,192.168.111.100,12h

# Provide IPv6 DHCP leases, the range is constructed using the network interface as prefix
dhcp-range=::f,::ff,constructor:enp0s0

# If you’d like to have dnsmasq assign static IPs to some clients, bind the LAN computers NIC MAC addresses:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
dhcp-host=aa:bb:cc:ff:dd:ee,192.168.111.51

有关更多选项,请参阅 dnsmasq(8)

代理 DHCP

如果网络中已经有一个 DHCP 服务器运行,并且您想与之互操作,dnsmasq 可以设置为充当“代理 DHCP”,因此仅向客户端提供 #PXE 服务器的特定信息。此模式仅适用于 IPv4。使用以下语法,提供现有 DHCP 服务器地址:

dhcp-range=192.168.0.1,proxy

测试

从一台连接到装有 dnsmasq 的计算机上,将其配置为使用 DHCP 进行自动 IP 地址分配,然后尝试正常登录网络。

如果在服务器上检查 /var/lib/misc/dnsmasq.leases 文件,您应该能够看到租约。

TFTP 服务器

dnsmasq 内置了 TFTP 服务器。

要使用它,请创建一个 TFTP 的根目录(例如 /srv/tftp)来存放可传输的文件。

enable-tftp
tftp-root=/srv/tftp

为了增加安全性,建议使用 dnsmasq 的 TFTP 安全模式。在安全模式下,只有 dnsmasq 用户拥有的文件才能通过 TFTP 提供。您需要 chown TFTP 根目录及其所有文件到 dnsmasq 用户才能使用此功能。

tftp-secure

有关更多选项,请参阅 dnsmasq(8)

PXE 服务器

PXE 需要 DHCP 和 TFTP 服务器;两者都可以由 dnsmasq 提供。要设置 PXE 服务器,请按照以下步骤操作:

  1. 在 dnsmasq 配置文件中设置 #TFTP 服务器#DHCP 服务器(在完整 DHCP 或代理模式下)。
  2. 在 TFTP 根目录中复制并配置一个 PXE 兼容的引导加载程序(例如 PXELINUX)。
  3. 在 dnsmasq 配置文件中启用 PXE。

要简单地发送一个文件

dhcp-boot=lpxelinux.0

根据客户端架构发送文件

pxe-service=x86PC,"PXELINUX (BIOS)",bios/lpxelinux
pxe-service=X86-64_EFI,"PXELINUX (EFI)",efi64/syslinux.efi
  • 文件路径相对于 TFTP 根路径。
  • 如果文件有 .0 后缀,您必须在 pxe-service 选项中排除该后缀。

如果 pxe-service 在识别架构方面不起作用(尤其是对于基于 UEFI 的客户端),则可以结合使用 dhcp-matchdhcp-boot。请参阅 RFC 4578 2.1 以获取更多与 dhcp 引导协议一起使用的 client-arch 号码。

dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-match=set:efi-x86_64,option:client-arch,9
dhcp-match=set:efi-x86,option:client-arch,6
dhcp-match=set:bios,option:client-arch,0
dhcp-boot=tag:efi-x86_64,efi64/syslinux.efi
dhcp-boot=tag:efi-x86,efi32/syslinux.efi
dhcp-boot=tag:bios,bios/lpxelinux.0

有关更多选项,请参阅 dnsmasq(8)

其余的取决于 引导加载程序

技巧与提示

阻止 OpenDNS 重定向 Google 查询

要阻止 OpenDNS 将所有 Google 查询重定向到他们自己的搜索服务器,请在 /etc/dnsmasq.conf 中添加:

server=/www.google.com/isp-dns-ip

覆盖地址

在某些情况下,例如在操作强制门户时,将特定域名解析为硬编码地址集可能很有用。这可以通过 address 配置完成。

address=/example.com/1.2.3.4

此外,可以使用特殊的通配符为所有未从 /etc/hosts 或 DHCP 获得响应的域名返回特定地址。

address=/#/1.2.3.4

多个实例

如果我们希望每个接口有两个或更多 dnsmasq 服务器同时工作。

静态

要静态地执行此操作,每个服务器每个接口,请使用 interfacebind-interfaces 选项。这会强制启动第二个 dnsmasq。

动态式

在这种情况下,我们可以排除每个接口并绑定其他任何接口。

except-interface=lo
bind-dynamic
注意 这是 libvirt 中的默认设置。

域名屏蔽列表

要屏蔽域名,即用 NXDOMAIN 回答它们的查询,请使用 address 选项而不指定 IP 地址。

address=/blocked.example/
address=/anotherblocked.example/
注意/etc/hosts 文件不同,dnsmasq 将会屏蔽这些域名以及所有子域名,例如 subdomain.blocked.example

也支持通配符。在模式开头添加一个 *

# blocks both blocked.example and anotherblocked.example and all their subdomains
address=/*blocked.example/

# blocks subdomains like mail.google.com but not google.com
address=/*.google.com/

可以使用 # 作为服务器地址来解除对某些特定子域名的阻止。

# blocks google.com and all subdomains except mail.google.com.
address=/google.com/
server=/mail.google.com/#
  • address=/example.com/server=/example.com/ 选项是等效的。两者都将用 NXDOMAIN 回答它们的查询。
  • address=/example.com/#server=/example.com/# 选项不等效。
    • address=/example.com/# 将用 NULL 地址(IPv6 为 0.0.0.0 或 ::)回答该域名的查询。
    • server=/example.com/# 将把该域名的查询发送到标准配置的服务器。
  • 模式 /example.com//.example.com/ 是等效的。两者都将匹配 example.com 及其所有子域名。

为了方便使用,请将屏蔽列表放在一个单独的文件中,例如 /etc/dnsmasq.d/blocklist.conf,并从 /etc/dnsmasq.conf 中使用 conf-file=/etc/dnsmasq.d/blocklist.confconf-dir=/etc/dnsmasq.d/,*.conf 加载它。

提示
  • 有关潜在屏蔽列表来源的列表,请参阅 OpenWrt 的 adblock 包的 README
  • 可以使用 addn-hosts=hosts.txt 选项来使用 hosts 文件屏蔽列表,或者使用此 awk 命令将其转换为 dnsmasq 屏蔽列表:awk '/^[^#]/ { print "address=/"$2"/"$1"" }' hosts.txt

查看缓存统计信息

可以使用混沌请求使用 drill 工具从 ldns 包查询缓存统计信息。

$ drill misses.bind TXT CH
$ drill hits.bind TXT CH

输出将分别包含缓存未命中和命中的数量。

;; ANSWER SECTION:
misses.bind.    0       CH      TXT     "411"

其他选项包括 cachesize.bindinsertions.bindevictions.bindauth.bindservers.bind

参见

© . This site is unofficial and not affiliated with Arch Linux.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.