当我进行查询时,Unbound DNS服务器不会记录任何日志。

我最近开始使用unbound dns。
我已经正确配置了所有需要的东西。但是当我进行查询时,服务器不会记录日志。
我的unbound.conf文件中没有错误。
以下是我的.conf文件。
# The server clause sets the main parameters.
server:
# whitespace is not necessary, but looks cleaner.

# verbosity number, 0 is least verbose. 1 is default.
verbosity: 1

# print statistics to the log (for every thread) every N seconds.
# Set to "" or 0 to disable. Default is disabled.
statistics-interval: 5


interface: 192.168.116.134

# port to answer queries from
port: 53


cache-min-ttl: 400
cache-max-ttl: 86400


# Enable IPv4, "yes" or "no".
do-ip4: yes

# Enable IPv6, "yes" or "no".
# do-ip6: yes

# Enable UDP, "yes" or "no".
    do-udp: yes

# Enable TCP, "yes" or "no".
    do-tcp: yes



access-control: 0.0.0.0/0 allow

# chroot: "/etc/unbound"


# username: "unbound"


# directory: "/etc/unbound"

# the log file, "" means log to stderr.
# Use of this option sets use-syslog to "no".
logfile: "/var/log/unbound/unbound.log"

forward-zone:
name: "."
forward-addr: 8.8.4.4
forward-addr: 8.8.8.8  

在给定的目录中确实存在lig文件,并且我已经使用chown将unbound用户设置为其所有者,但是当我进行查询时,日志文件仍然为空。

平台:Ubuntu 18桌面版

4个回答

默认情况下,未绑定的Debian/Ubuntu或类似软件包将日志记录到/var/log/syslog。经过一些尝试和错误,我找到了如何更改它的方法。 步骤1 确保所有CHROOT指令都被注释掉,并添加日志指令:
搜索指令:
sudo grep -R "chroot" /etc/unbound/*` :

如果正确注释,你应该看到类似于这样的内容:
# chroot:""

确保您的配置文件中有所需的日志位置 - 添加以下行:
 logfile: "/var/log/unbound/unbound.log"

第二步
确保日志文件和父文件夹都属于unbound用户所有:
sudo chown unbound:unbound /var/log/unbound /var/log/unbound/unbound.log

此时对我(或你)来说,这仍然没有起作用。
在此之后,我注意到虽然将详细程度调到了调试级别,但仍然会记录到系统日志中。我还注意到内核日志显示“apparmor”以“DENIED”的方式记录了对未绑定日志位置的访问:
sudo cat /var/log/syslog | grep DENIED

在系统日志中的示例:

 Dec 30 16:41:48 ip-192-168-1-1 kernel: [ 1368.641789] audit: type=1400 audit(1577724108.624:29): apparmor="DENIED" operation="open" profile="/usr/sbin/unbound" name="/var/log/unbound/unbound.log" pid=2247 comm="unbound" requested_mask="ac" denied_mask="ac" fsuid=112 ouid=112

第三步
为了纠正这个问题,我在`apparmor.d`区域中添加了一个本地覆盖。
sudo nano /etc/apparmor.d/local/usr.sbin.unbound

将这一行代码添加到其中:
/var/log/unbound/unbound.log rw,

(是的,末尾有逗号)保存。 第四步 重新加载unbound的apparmor条目:
sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.unbound

第五步

重新启动Unbound:

sudo systemctl restart unbound

检查日志:
$ sudo tail -f /var/log/unbound/unbound.log
[1577725445] unbound[2721:0] info: start of service (unbound 1.6.7).

工作。如果你注意到,当syslog记录时,它使用了一个标准的日期格式。但是,Unbound自定义日志/非syslog默认使用Unix/Epoch time(从1970年开始的秒数)。如果你希望有类似syslog的时间戳,请将以下内容添加到你的unbound配置文件中,并重新加载服务:
log-time-ascii: yes

如果您的日志位置不同,请确保更改上述引用的所有路径/文件名。
我的配置: gist text

References:
https://nlnetlabs.nl/documentation/unbound/unbound.conf/
https://wiki.debian.org/AppArmor/Debug


2只是想说谢谢这个。通过互联网搜索我来到这里,这些确切的步骤对我非常有效,在运行 Debian 的树莓派上运行 unbound 时完美适用。 - Soundscape
1+1。非常感谢您提供完整详细的步骤,解决了我的问题。 - ali14

你需要设置一个适当的详细程度。3应该足够了。
verbosity: <number>
          The verbosity number, level 0 means no verbosity,  only  errors.
          Level  1  gives  operational information. Level 2 gives detailed
          operational information. Level 3 gives query level  information,
          output  per  query.   Level 4 gives algorithm level information.
          Level 5 logs client identification for cache misses.  Default is
          level  1.  The verbosity can also be increased from the command-
          line, see unbound(8).

我已经做了这个,但日志文件仍然是空的。 - Mathew

你正在运行在一个chroot环境下(/etc/unbound),这意味着你的日志实际上应该保存在/etc/unbound/var/log/unbound/unbound.log。这是你在寻找的位置吗?

嘿,我已经做了这个,但是它没有起作用。 - Mathew
chroot指令被注释掉了,所以他没有运行chroot。 - gerardw
chroot默认已启用。如果您想禁用chroot,请取消注释并设置chroot: "" - guzzijason

这个答案和 @b. shea 的一样。只是用 ansible 自动化了步骤。
- name: unbound logging
  ansible.builtin.file:
    path: /var/log/unbound
    state: directory
    owner: unbound
    group: unbound
  become: true

- name: unbound logging - file
  ansible.builtin.file:
    path: /var/log/unbound/unbound.log
    state: touch
    owner: unbound
    group: unbound
  become: true

- name: unbound logging - apparmor
  ansible.builtin.copy:
    dest: /etc/apparmor.d/local/usr.sbin.unbound
    content: |
      /var/log/unbound/unbound.log rw,
  register: unbound_apparmor
  become: true

- name: unbound logging - apparmor parse
  ansible.builtin.shell:
    cmd: apparmor_parser -r /etc/apparmor.d/usr.sbin.unbound
  become: true
  when: unbound_apparmor.changed