使用 NetworkManager 的 DNSMasq 插件

dnsmasq 插件是 NetworkManager 的隐藏宝石。 使用插件时,NetworkManager 将配置一个可以自定义的 dnsmasq 本地副本,而不是使用 DHCP 分配的任何 DNS 名称服务器。

你可能会问,你为什么要这样做? 就我个人而言,我有两个用例:

首先,在我的笔记本电脑上,为了测试目的,我运行了完整的 OpenShift 安装。 为了完成这项工作,我真的需要能够添加 DNS 记录。 我可以在没有 NetworkManager 的情况下运行本地 dnsmasq,但是这个配置比管理我自己的配置更容易。

其次,当我在家时,我仍然想在 VPN 上使用我的家庭网络的 DNS。 许多 VPN 配置为仅通过 VPN 隧道路由特定流量,并保留我的默认路由。 这意味着我可以访问本地网络的打印机,并且仍然可以连接到 VPN 上的资源。

这非常好,因为这意味着我在工作时仍然可以访问我的网络打印机或从我的媒体服务器听音乐。 但是,VPN 连接使用来自 VPN 网络的 DNS 服务器覆盖了我的 resolv.conf。 因此,我的家庭网络的 DNS 不再可访问。

dnsmasq 插件通过运行由 NetworkManager 控制的本地 dnsmasq 服务器来解决这个问题。 我的 resolv.conf 总是指向 localhost。 对于本地定义的记录(例如,对于我的 OpenShift 集群),dnsmasq 会正确解析这些记录。 使用更高级的 dnsmasq 配置,我可以选择性地将某些域的请求转发到特定服务器(例如,始终正确解析我的家庭网络主机)。 对于所有其他请求,dnsmasq 将转发到与我当前网络或 VPN 关联的 DNS 服务器。

以下是如何配置它 Fedora 29:

在某些情况下,我的笔记本电脑上的域称为“laplab”,而我的主域是“.homelab”。 在家里,我的 DNS 服务器是 172.31.0.1。 对于 laplab 中的 DNS 条目,大部分在 /etc/hosts 中定义。 然后 dnsmasq 可以将它们吞食。 我还为通配符 DNS 和一些别名定义了一些额外的 DNS 条目。

以下是需要添加的五个文件。 dnsmasq.d 中的文件可以合并,但可以拆分以希望更好地显示 example.

  • /etc/NetworkManager/conf.d/00-use-dnsmasq.conf
  • /etc/NetworkManager/dnsmasq.d/00-homelab.conf
  • /etc/NetworkManager/dnsmasq.d/01-laplab.conf
  • /etc/NetworkManager/dnsmasq.d/02-add-hosts.conf
  • /etc/hosts
# /etc/NetworkManager/conf.d/00-use-dnsmasq.conf
#
# This enabled the dnsmasq plugin.
[main]
dns=dnsmasq
# /etc/NetworkManager/dnsmasq.d/00-homelab.conf
#
# This file directs dnsmasq to forward any request to resolve
# names under the .homelab domain to 172.31.0.1, my 
# home DNS server.
server=/homelab/172.31.0.1
# /etc/NetworkManager/dnsmasq.d/01-laplab.conf
# This file sets up the local lablab domain and 
# defines some aliases and a wildcard.
local=/laplab/

# The below defines a Wildcard DNS Entry.
address=/.ose.laplab/192.168.101.125

# Below I define some host names.  I also pull in   
address=/openshift.laplab/192.168.101.120
address=/openshift-int.laplab/192.168.101.120
# /etc/NetworkManager/dnsmasq.d/02-add-hosts.conf
# By default, the plugin does not read from /etc/hosts.  
# This forces the plugin to slurp in the file.
#
# If you didn't want to write to the /etc/hosts file.  This could
# be pointed to another file.
#
addn-hosts=/etc/hosts
# /etc/hosts
#  
# The hostnames I define in that will be brought in and resolvable
# because of the config in the 02-add-hosts.conf file. 
#
127.0.0.1   localhost localhost.localdomain 
::1         localhost localhost.localdomain 

# Notice that my hosts be in the .laplab domain, like as configured 
# in 01-laplab.conf file
192.168.101.120  ose-lap-jumphost.laplab
192.168.101.128  ose-lap-node1.laplab

# Name not in .laplab will also get picked up.  So be careful 
# defining items here.
172.31.0.88     overwrite.public.domain.com

在所有这些文件都到位后,使用 systemctl restart NetworkManager 重新启动 NetworkManager。 如果一切正常,您应该会看到您的 resolv.conf 指向 127.0.0.1 并生成了一个新的 dnsmasq 进程。

$ ps -ef | grep dnsmasq
dnsmasq   1835  1188  0 08:01 ?        00:00:00 /usr/sbin/dnsmasq --no-resolv 
--keep-in-foreground --no-hosts --bind-interfaces --pid-file=/var/run/NetworkManager/dnsmasq.pid 
--listen-address=127.0.0.1 --cache-size=400 --clear-on-reload --conf-file=/dev/null 
--proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq 
--conf-dir=/etc/NetworkManager/dnsmasq.d
$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 127.0.0.1
$ host ose-lap-jumphost.laplab
ose-lap-jumphost.laplab has address 192.168.101.120

此配置将在重新启动后继续存在,并且在我的测试中,几乎可以与我尝试过的所有网络和 VPN 一起使用。