ansible-playbooks/roles/unbound/templates/unbound.conf.j2
2026-03-17 23:08:44 +01:00

135 lines
5.3 KiB
Django/Jinja

# {{ ansible_managed }}
# see: https://unix.stackexchange.com/a/617194
# see: https://unix.stackexchange.com/questions/617091/can-you-specify-a-different-configuration-for-different-interfaces-in-unbound
# see: https://www.dnsleaktest.com
server:
verbosity: 0
# listening port
port: {{ unbound_port }}
# Allow binding to IP addresses that don't exist yet (e.g., WireGuard at boot)
ip-freebind: yes
# Define interface binds by IP address
interface: 127.0.0.1 # lo (IPv4)
interface: ::1 # lo (IPv6)
{% for iface in unbound_interfaces %}
interface: {{ iface.address }} # {{ iface.comment | default('') }}
{% endfor %}
# Define access controls (note that ufw might be also configured)
access-control: 0.0.0.0/0 refuse
access-control: 127.0.0.0/8 allow # lo interface
{% for acl in unbound_access_control %}
access-control: {{ acl.subnet }} {{ acl.action }} # {{ acl.comment | default('') }}
{% endfor %}
access-control: ::0/0 refuse
access-control: ::1 allow
# Specify custom local answers for each interface by using views:
{% for acl in unbound_access_control %}
{% if acl.view is defined %}
access-control-view: {{ acl.subnet }} {{ acl.view }}
{% endif %}
{% endfor %}
do-ip4: yes
do-udp: yes
do-tcp: yes
do-ip6: yes
# You want to leave this to no unless you have *native* IPv6. With 6to4 and
# Terredo tunnels your web browser should favor IPv4 for the same reasons
prefer-ip6: no
# Use this only when you downloaded the list of primary root servers!
# If you use the default dns-root-data package, unbound will find it automatically
root-hints: "{{ unbound_root_hints_path }}"
# enable to not answer id.server and hostname.bind queries.
hide-identity: yes
# enable to not answer version.server and version.bind queries.
hide-version: yes
# Trust glue only if it is within the server's authority
harden-glue: yes
# Require DNSSEC data for trust-anchored zones, if such data is absent, the zone becomes BOGUS
harden-dnssec-stripped: yes
# Don't use Capitalization randomization as it known to cause DNSSEC issues sometimes
# see https://discourse.pi-hole.net/t/unbound-stubby-or-dnscrypt-proxy/9378 for further details
use-caps-for-id: no
# the time to live (TTL) value lower bound, in seconds. Default 0.
# Setting to 0 respects upstream TTLs, avoiding outages on fast-flux services.
cache-min-ttl: 0
# the time to live (TTL) value cap for RRsets and messages in the
# cache. Items are not cached for longer. In seconds.
cache-max-ttl: 86400
# Serve expired records while fetching fresh ones in the background.
# Improves reliability when upstream resolvers are temporarily unavailable.
serve-expired: yes
serve-expired-ttl: 86400
serve-expired-client-timeout: 0
# Reduce EDNS reassembly buffer size.
# IP fragmentation is unreliable on the Internet today, and can cause
# transmission failures when large DNS messages are sent via UDP. Even
# when fragmentation does work, it may not be secure; it is theoretically
# possible to spoof parts of a fragmented DNS message, without easy
# detection at the receiving end. Recently, there was an excellent study
# >>> Defragmenting DNS - Determining the optimal maximum UDP response size for DNS <<<
# by Axel Koolhaas, and Tjeerd Slokker (https://indico.dns-oarc.net/event/36/contributions/776/)
# in collaboration with NLnet Labs explored DNS using real world data from the
# the RIPE Atlas probes and the researchers suggested different values for
# IPv4 and IPv6 and in different scenarios. They advise that servers should
# be configured to limit DNS messages sent over UDP to a size that will not
# trigger fragmentation on typical network links. DNS servers can switch
# from UDP to TCP when a DNS response is too big to fit in this limited
# buffer size. This value has also been suggested in DNS Flag Day 2020.
edns-buffer-size: 1232
# Perform prefetching of close to expired message cache entries
# This only applies to domains that have been frequently queried
# NOTE: Disabled because prefetch doesn't work with subnet module (views)
# see: https://unbound.docs.nlnetlabs.nl/en/latest/manpages/unbound.conf.html#unbound-conf-prefetch
prefetch: no
# Threads and cache slabs optimized for 4-core CPU
num-threads: 4
msg-cache-slabs: 4
rrset-cache-slabs: 4
infra-cache-slabs: 4
key-cache-slabs: 4
# Cache sizes optimized for 64GB RAM
msg-cache-size: 512m
rrset-cache-size: 1g
# Ensure kernel buffer is large enough to not lose messages in traffic spikes
so-rcvbuf: 4m
so-sndbuf: 4m
# Ensure privacy of local IP ranges
private-address: 192.168.0.0/16
private-address: 169.254.0.0/16
private-address: 172.16.0.0/12
private-address: 10.0.0.0/8
private-address: fd00::/8
private-address: fe80::/10
# Allow the domain (and its subdomains) to contain private addresses.
# local-data statements are allowed to contain private addresses too.
private-domain: "{{ unbound_custom_lan_domain }}"
# Enable DNSSEC
auto-trust-anchor-file: "{{ unbound_anchor_root_key }}"
include: "{{ unbound_custom_lan_config_path }}"
include: "{{ unbound_custom_vpn_config_path }}"