使用 dnsmasq 按名称寻址机器并解析外部主机名

BrD*_*aHa 5 dns airport dnsmasq

我的 dnsmasq 设置遇到了一些问题。我想要它做的就是将某些主机名解析为我网络上任何机器的某些 IP 地址。更具体地说,我试图使我网络上的所有设备能够仅使用指定的主机名访问我网络上的某些机器。我想我一定遗漏了一些重要的东西,因为即使我让它工作了一次,在我的 DNS 服务器重新启动后,它停止转发对 LAN 外服务器的请求。

这是我的设置:(所有 ip 地址只有 IPv4 地址的最后一位)

  • Apple Airport Extreme 设置为 DHCP 和 NAT 模式(不能只在这个路由器上有 NAT,所以为了获得 NAT,我将 DHCP 范围设置为 253-254 并为这些 IP 保留了一些虚假的 mac 地址)。在我的网络上,它的 IP 地址为 1

  • 运行 raspbian 和 dnsmasq 的 Raspberry PI。它的 IP 地址为 4,主机名为“pi”

  • 一台名为“塔”的机器在 3

Dnsmasq 被设置为 DNS 和 DHCP 服务器。所有连接到网络的机器都会获得一个我在 dnsmasq 中指定的正确范围内的 IP 地址,并将树莓派 (IP 4) 自动指定为 DNS 服务器。Dnsmasq 还设置为将它找不到的任何请求转发到其他公共 dns 服务器。这是我的全部 dns 服务器,其中包含已编辑的完整 ip 地址:

/etc/dnsmasq.conf:

# Dnsmasq.conf for raspberry pi    
# Full examples found here:  
# http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq.conf.example  

# Set up your local domain here    
domain=hyrule.home

# Example: The option local=/localnet/ ensures that any domain name query which ends in .localnet will be answered if possible from /etc/hosts or DHCP, but never sent to an upstream server
# don't forward requests (andrewoberstar.com/blog/2012/12/30/raspberry-pi-as-server-dns-and-dhcp)
local=/hyrule.home/

#resolv-file=/etc/resolv.dnsmasq  
resolv-file=/etc/resolv.conf  
#min-port=4096   

## DNS SERVERS
#openNic california
server=173.230.156.28

#openNic Washington
server=23.226.230.72

#google public DNS
server=8.8.8.8
server=8.8.4.4

# Max cache size dnsmasq can give us
cache-size=10000

# Use the hosts file on this machine
expand-hosts

# ethernet - ip address mappings from /etc/ethers file
read-ethers

# Below are settings for dhcp. 
dhcp-range=XXX.XXX.X.10,XXX.XXX.X.200,12h
dhcp-option=3,XXX.XXX.X.1
dhcp-authoritative
log-queries
Run Code Online (Sandbox Code Playgroud)

/etc/resolv.conf:

domain hyrule.home 
#search hyrule.home
#nameserver 8.8.8.8
Run Code Online (Sandbox Code Playgroud)

/etc/hosts

127.0.0.1   localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0     ip6-localnet
ff00::0     ip6-mcastprefix
ff02::1     ip6-allnodes
ff02::2     ip6-allrouters

127.0.1.1   raspberrypi
XXX.XXX.X.3 tower
XXX.XXX.X.4 pi
Run Code Online (Sandbox Code Playgroud)

/etc/ethers文件包含了各自机器的以太网接口的 mac 地址

我可以看到 dnsmasq 正在记录请求,一切看起来都正常,所有请求看起来像

Nov  2 17:29:21 raspberrypi dnsmasq[2067]: query[AAAA] time.apple.com.hsd1.ca.comcast.net from 10.0.1.1
Nov  2 17:29:21 raspberrypi dnsmasq[2067]: forwarded time.apple.com.hsd1.ca.comcast.net to 173.230.156.28
Nov  2 17:29:21 raspberrypi dnsmasq[2067]: forwarded time.apple.com.hsd1.ca.comcast.net to 23.226.230.72
Nov  2 17:29:21 raspberrypi dnsmasq[2067]: forwarded time.apple.com.hsd1.ca.comcast.net to 8.8.8.8
Nov  2 17:29:21 raspberrypi dnsmasq[2067]: forwarded time.apple.com.hsd1.ca.comcast.net to 8.8.4.4
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: query[A] north-america.pool.ntp.org from 10.0.1.59
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded north-america.pool.ntp.org to 8.8.4.4
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded north-america.pool.ntp.org to 8.8.8.8
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded north-america.pool.ntp.org to 23.226.230.72
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded north-america.pool.ntp.org to 173.230.156.28
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: query[TXT] push.apple.com from 10.0.1.3
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded push.apple.com to 8.8.8.8
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded push.apple.com to 23.226.230.72
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded push.apple.com to 173.230.156.28
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded push.apple.com to 8.8.4.4
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: query[AAAA] time.apple.com.hyrule.home from 10.0.1.14
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: config time.apple.com.hyrule.home is NXDOMAIN-IPv6
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: query[A] time.apple.com.hyrule.home from 10.0.1.14
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: config time.apple.com.hyrule.home is NXDOMAIN-IPv4
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: query[AAAA] time.apple.com from 10.0.1.14
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded time.apple.com to 8.8.4.4
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded time.apple.com to 8.8.8.8
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded time.apple.com to 23.226.230.72
Nov  2 17:29:24 raspberrypi dnsmasq[2067]: forwarded time.apple.com to 173.230.156.28
Run Code Online (Sandbox Code Playgroud)

编辑: 我决定将 pi 重置为默认的 raspbian,因为我对它做了很多实验,所以我想删除任何其他变量。我继续加载minibian并安装了 dnsmasq。我编辑了我的 resolv.conf 看起来像这样(感谢@Chuck Kollars 指出该文件真正在做什么):

domain hyrule.home
search hyrule.home
nameserver XXX.XXX.X.4
Run Code Online (Sandbox Code Playgroud)

并且只设置我的 dnsmasq.conf 来查询这些名称服务器(默认情况下读取我的主机文件):

server=/localnet/192.168.0.1 <--- still have the feeling this isn't right
## DNS SERVERS
#openNic california
server=173.230.156.28

#openNic Washington
server=23.226.230.72
Run Code Online (Sandbox Code Playgroud)

在我的主机文件中,我还按照@Chuck Kollars 的建议设置了别名,格式为

<ipv4>   <subdomain>  <fqdn>
Run Code Online (Sandbox Code Playgroud)

此时,我仍然遇到问题,所以我注意到每当我 ping 诸如“tower”之类的东西时,dnsmasq 都会尝试解析“tower.hsdn.comcast.com”或类似的东西,将其转发到外部名称服务器因为它没有在内部解决。comcast 部分看起来很熟悉,所以在我的机场实用程序 (5.6.1) 中,在 internet 选项卡下,在 TCP/IP 下,在域名字段中填充了相同的地址(幻影,就像它被继承一样),所以我改变了它也成为 hyrule.home,一切都按预期进行!

我仍然很困惑为什么该域名必须在路由器中,以及为什么它没有从 resolv.conf 文件中得到尊重......

小智 5

虽然我不能完全弄清楚您的系统为什么会出现异常行为的所有细节,但我确实有以下建议,您可以从中获利:

1) Dnsmasq 仅将 /etc/resolv.conf 用于源自同一台机器的查询 - 来自所有其他机器的查询直接进入 Dnsmasq。所以 /etc/resolv.conf 通常包含一个指向名称服务器 127.0.0.1 的指针,以便将来自该机器上的应用程序的请求发送到 Dnsmasq。事实上,甚至在 Dnsmasq 启动时,/etc/resolv.conf 甚至可能被覆盖。将操作说明放在/etc/resolv.conf 中要非常小心;特别是我不认为“域 hyrule.home”正在为您的某些主机做您认为它正在做的事情。

2)另一种方式来处理当地的简短名字(我做到这一点),是明确提出这两个短名称和每个本地机器的LONGNAME到/ etc / hosts文件(而不是依赖于任何软件机制),是这样的:

XXX.XXX.X.1   router router.hynet.home
XXX.XXX.X.3   tower tower.hynet.home
XXX.XXX.X.4   pi pi.hynet.home
Run Code Online (Sandbox Code Playgroud)

3)我不太确定将“raspberrypi”等同于“localhost”的定义是什么。我害怕这样的等价性有时会在错误的时间对错误的系统产生错误的响应,从而导致一些查询进入圆形文件而不是它们应该出现的位置。我的系统没有这样的定义。

4) 为了在您的各种日志中清晰起见,您可能还需要“localnet”的定义(类似于您对“ip6-localnet”的定义)。你会期望它进入 /etc/networks ......事实上这可能会起作用。但是 Dnsmasq 本身只查看 /etc/hosts,因此您可能不得不将定义放入 /etc/hosts 中,即使它是针对网络的,如下所示:

XXX.XXX.X.0 localnet hynet.home
Run Code Online (Sandbox Code Playgroud)