让 dnsmasq 自动在其 DNS 上注册 DHCP 网络中的主机名

让 dnsmasq 自动在其 DNS 上注册 DHCP 网络中的主机名

我已将 dnsmasq 配置为向以太网 LAN 上的主机分配 10.0.0.* 地址。此网络上的主机使用 DHCP 从运行在 10.0.0.1 上的 dnsmasq 服务器请求静态 IP 地址。它们还可以通过 wifi 访问路由器以访问互联网。例如,10.0.0.2 上的主机在 中有以下条目/etc/dhcpcd.conf

### Wifi-router
interface wlan0
static ip_address=192.168.1.202
static routers=192.168.1.1

### Ethernet Switch
interface eth0
static ip_address=10.0.0.2
static domain_name_servers=10.0.0.1 ### Where dnsmasq runs

现在,我希望 dnsmasq 自动充当这些主机的 DNS 服务器,以便它们可以通过名称相互通信。例如,如果位于 10.0.0.2 的主机的主机名为“node2”,则可以使用以下命令从此网络中的任何主机 ssh 到 10.0.0.2 ssh user@node2

我的问题是,如何配置 dnsmasq 来缓存每个主机的名称自动地? 我知道您可以/etc/hosts在运行 dnsmasq 的机器上添加条目并应用选项expand-hosts/etc/dnsmasq.conf但我不想维护这样一个额外的列表——我更愿意只在每个单独的主机上设置主机名,并让其成为它们命名方式的 SSOT。我该如何设置?

这是我的/etc/dnsmasq.conf文件。

interface=eth0
listen-address=127.0.0.1
dhcp-range=10.0.0.0,10.0.0.10,12h
bind-interfaces
domain-needed
bogus-priv
expand-hosts

### Upstream DNS servers
server=8.8.8.8
server=8.8.4.4

答案1

问题是您在客户端上指定了一个静态地址。来自man 5 dhcpcd.conf

             Configures a static value.  If you set ip_address then dhcpcd
             will not attempt to obtain a lease and will just use the value
             for the address with an infinite lease time.

因此 dnsmasq 永远不会收到DHCPREQUEST并且永远不会意识到客户端的存在。

这里有两个选项:

  • 从 DHCP 服务器分配固定地址。dnsmasq 允许您匹配 MAC 地址、客户端 ID 或其他自定义标签 - 通常使用 MAC 地址。您可以添加一行,例如:

    dhcp-host=00:53:00:11:22:33:10.0.0.2
    

    并在客户端删除您的静态地址。

  • 或者,使用 dhcpcdrequestinform选项,其作用是:

         request [address]
                 Request the address in the DHCP DISCOVER message.  There is no
                 guarantee this is the address the DHCP server will actually give.
                 If no address is given then the first address currently assigned
                 to the interface is used.
    
         inform [address[/cidr[/broadcast_address]]]
                 Behaves like request as above, but sends a DHCP INFORM instead of
                 DISCOVER/REQUEST.  This does not get a lease as such, just
                 notifies the DHCP server of the address in use.  You should also
                 include the optional cidr network number in case the address is
                 not already configured on the interface.  dhcpcd remains running
                 and pretends it has an infinite lease.  dhcpcd will not de-
                 configure the interface when it exits.  If dhcpcd fails to
                 contact a DHCP server then it returns a failure instead of
                 falling back on IPv4LL.
    

一般来说,我建议从服务器分配静态地址,因为这样可以最大限度地减少冲突的可能性。如果您只有一个首选地址,并且不介意更改它(如果它已经在使用),那么request从客户端使用可能没问题。inform通常应该被认为是危险的,因为它有冲突的风险。

相关内容