如何调试需要重启服务才能工作的 dnsmasq?

如何调试需要重启服务才能工作的 dnsmasq?

在我的服务器上,我设置了 dnsmasq;它被配置为另一台服务器,它可以在那里工作(尽管与其他硬件和操作系统一起)。

但是在这个特定的主机上,dnsmasq 在启动后不起作用(客户端无法解析名称),但如果我手动重新启动它(service dnsmasq restart),它就可以工作。

我无法从日志中找出任何东西,日志没有显示任何问题。启动后立即提取系统日志:

15:Apr 13 12:31:39 <server_hostname> systemd[1]: Stopping dnsmasq - A lightweight DHCP and caching DNS server...
276:Apr 13 12:32:22 <server_hostname> systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
285:Apr 13 12:32:22 <server_hostname> dnsmasq[592]: dnsmasq: syntax check OK.
511:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: started, version 2.75 cachesize 150
512:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect inotify
513:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: DNS service limited to local subnets
514:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: warning: ignoring resolv-file flag because no-resolv is set
515:Apr 13 12:32:22 <server_hostname> dnsmasq-dhcp[622]: DHCP, IP range 192.168.166.2 -- 192.168.166.254, lease time 1h
516:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: using nameserver 209.222.18.218#53
517:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: using nameserver 209.222.18.222#53
518:Apr 13 12:32:22 <server_hostname> dnsmasq[622]: read /etc/hosts - 5 addresses
558:Apr 13 12:32:22 <server_hostname> systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
568:Apr 13 12:32:24 <server_hostname> dnsmasq-dhcp[622]: DHCP packet received on eth1 which has no address
575:Apr 13 12:32:27 <server_hostname> dnsmasq-dhcp[622]: DHCP packet received on eth1 which has no address
589:Apr 13 12:32:32 <server_hostname> dnsmasq-dhcp[622]: DHCPDISCOVER(eth1) 192.168.166.129 <client_mac>
590:Apr 13 12:32:32 <server_hostname> dnsmasq-dhcp[622]: DHCPOFFER(eth1) 192.168.166.129 <client_mac>
591:Apr 13 12:32:32 <server_hostname> dnsmasq-dhcp[622]: DHCPREQUEST(eth1) 192.168.166.129 <client_mac>
592:Apr 13 12:32:32 <server_hostname> dnsmasq-dhcp[622]: DHCPACK(eth1) 192.168.166.129 <client_mac> <client_hostname>

执行后添加的条目service dnsmasq restart

625:Apr 13 12:33:39 <server_hostname> systemd[1]: Stopping dnsmasq - A lightweight DHCP and caching DNS server...
626:Apr 13 12:33:39 <server_hostname> dnsmasq[622]: exiting on receipt of SIGTERM
627:Apr 13 12:33:39 <server_hostname> systemd[1]: Stopped dnsmasq - A lightweight DHCP and caching DNS server.
628:Apr 13 12:33:39 <server_hostname> systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
629:Apr 13 12:33:39 <server_hostname> dnsmasq[875]: dnsmasq: syntax check OK.
630:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: started, version 2.75 cachesize 150
631:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect inotify
632:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: DNS service limited to local subnets
633:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: warning: ignoring resolv-file flag because no-resolv is set
634:Apr 13 12:33:39 <server_hostname> dnsmasq-dhcp[885]: DHCP, IP range 192.168.166.2 -- 192.168.166.254, lease time 1h
635:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: using nameserver 209.222.18.218#53
636:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: using nameserver 209.222.18.222#53
637:Apr 13 12:33:39 <server_hostname> dnsmasq[885]: read /etc/hosts - 5 addresses
638:Apr 13 12:33:40 <server_hostname> systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.

服务器是 Ubuntu 16.04;dnsmasq 版本是2.75-1ubuntu0.16.04.4

配置(下/etc/dnsmasq.d/)是:

bind-interfaces
dhcp-range=eth1,192.168.100.2,192.168.100.254
server=<dns_server_1>
server=<dns_server_2>
no-resolv

我该如何调试此问题?可能的原因是什么?

答案1

这实际上是 systemd 和 dnsmasq 之间的竞争条件,在 Raspbian 上也可能发生。

解决方案是确保在 dnsmasq 启动之前网络接口已启动:

sudo systemctl edit dnsmasq

并添加:

[Unit]
After=network-online.target
Wants=network-online.target

参考:https://discourse.pi-hole.net/t/have-to-manually-restart-dns-server-when-reboot-pihole-another-one/2255

相关内容