dnsmasq 不记录 dns 查询

Saa*_*med 4 networking dns dnsmasq 23.10

以下是我从命令行调试此问题的方法:

$ dnsmasq --no-daemon --log-queries
Run Code Online (Sandbox Code Playgroud)

log-queries选项放入配置文件中并将其作为守护进程运行,日志文件中仍然没有显示任何内容。我期待当我这样做时dig example.com,它应该出现。

这是上面命令的输出:

dnsmasq: started, version 2.89 cachesize 150
dnsmasq: compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset nftset auth cryptohash DNSSEC loop-detect inotify dumpfile
dnsmasq: reading /etc/resolv.conf
dnsmasq: using nameserver 8.8.8.8#53
dnsmasq: using nameserver 1.1.1.1#53
dnsmasq: using nameserver 2001:4860:4860::8888#53
dnsmasq: using nameserver 2001:4860:4860::8844#53
dnsmasq: read /etc/hosts - 7 names
Run Code Online (Sandbox Code Playgroud)

Ubuntu版本:23.10
内核6.5.0
dnsmasq版本:2.89

Dan*_*l T 6

如果真的使用dnsmasq

dig @127.0.0.1 example.com这样做吧。对于整个系统,请跳至下面的正确解决方案

您的行dnsmasq: using nameserver 8.8.8.8显示您/etc/resolv.conf已远离 systemd-resolved 进行了编辑。现在的问题不在于 dnsmasq 没有记录查询。问题是查询没有发送到 dnsmasq。每个应用程序都会调用getaddrinfoglibc 函数,该函数会查看/etc/resolv.conf. 在本例中,正在读取该文件,并直接dig向 发出 DNS 请求,而不询问.8.8.8.8dnsmasq

systemd-resolved 编辑该/etc/resolv.conf文件是有原因的。它这样做是为了使用127.0.0.53. 这样做dnsmasq只会导致无限循环,因为dnsmasq使用该文件本身。systemd-resolved不会遇到这个问题,因为它使用/etc/systemd/resolved.conf. 我们可以用适当的解决方案来复制它:

编辑:请参阅 Pihhan 的简单答案https://askubuntu.com/a/1501802/1004020。它可以做到这一点,只是步骤更少。

  1. sudo mv /etc/resolv.conf /etc/resolv-for-dnsmasq.conf
  2. echo 'nameserver 127.0.0.1' | sudo tee /etc/resolv.conf
  3. dnsmasq --no-daemon --log-queries -r /etc/resolv-for-dnsmasq.conf改为启动服务器
  4. dig example.com所有其他应用程序都应该开始受益于 dnsmasq 的缓存

如果使用 systemd-resolved

dnsmasqUbuntu 23.10默认不使用。它使用systemd-resolved,因此使用相应的命令:

  1. sudo resolvectl log-level debug
  2. 做你原来的dig example.com或使用resolvectl query --cache=0 example.com
  3. 请参阅顶部的输出sudo journalctl -r -u systemd-resolved
  4. 为了隐私,请关闭调试sudo resolvectl log-level info