How to Enable DNS-over-TLS with Pi-hole, and Why I Haven’t Done It (Yet)

A guide for setting up DoT both upstream and downstream, which you may not actually want to do.
technology
Author

Ian Taylor

Published

November 2, 2026

DNS-over-TLS (DoT) is a protocol for encrypted DNS traffic between a client and a server. People who run their own DNS servers, such as Pi-hole, are often interested in this as a way to further increase the privacy of their network. As a result, there are lots of guides on DoT with Pi-hole available. But something that’s missed in a lot of tutorials on this subject is that there are two places we can use DoT: upstream and downstream.

flowchart LR
    A1@{shape: "procs", label: "Clients"}
    B[Pi-hole]
    C@{shape: "cloud", label: "DNS Server"}
    subgraph Upstream
        C
    end
    subgraph Downstream
        A1
    end
    A1 --Queries--> B
    B --Forwarded Responses--> A1
    B --Forwarded Queries--> C
    C --Responses-->B

Using DoT upstream means that traffic will be encrypted between Pi-hole and the DNS server(s) it forwards queries to, and using DoT downstream means that traffic will be encrypted between local clients and Pi-hole. Both are required if you want DNS queries to be encrypted all the way from the source to the final destination, but most guides out there only describe the upstream direction.

I’ve set up Pi-hole with DoT both upstream and downstream, and I’ve put together this guide for how to do it yourself. Surprisingly, though, I later undid everything and went back to unencrypted DNS. At least for now. My reasons are at the end of the guide, so you can also be aware of some of the current downsides of DoT.

Note

I’m using Pi-hole in Docker with Docker compose on Debian. Some instructions such as installing packages will be specific to Debian, but they should be easy to translate to other distros. I’m using systemd to automatically start services. If your distro doesn’t have systemd, I assume you know what you’re doing already. Anything specific to Docker will have an alternative provided for standard Pi-hole installations.

DoT Upstream - Stubby

If you Google “pihole DNS-over-TLS,” most of the guides you find will cover the upstream. In the name of completeness, I’m also providing my own instructions. We’ll configure Pi-hole to forward encrypted DNS queries to upstream servers using an intermediate server. Stubby is a stub DNS server whose only job is to receive queries and forward them encrypted to other servers upstream, so it’s perfect for this. It’s also very easy to set up.

Install Stubby with sudo apt install stubby, then create the following configuration file:

/etc/stubby/stubby.yml
resolution_type: GETDNS_RESOLUTION_STUB
dns_transport_list:
  - GETDNS_TRANSPORT_TLS
tls_authentication: GETDNS_AUTHENTICATION_REQUIRED
tls_query_padding_blocksize: 128
edns_client_subnet_private : 1
round_robin_upstreams: 1
idle_timeout: 10000

listen_addresses:
  - 127.0.0.1@5301
  - 0::1@5301

upstream_recursive_servers:
  - address_data: <IP address>
    tls_auth_name: "<Hostname for verification>"
  - address_data: <IP address>
    tls_auth_name: "<Hostname for verification>"
  - [...]
1
Replace this with the IP address of your chosen provider.
2
Replace this with the hostname of your chosen provider.
3
Add as many of these providers as you wish.

You can choose any public DNS-over-TLS provider you wish. I recommend looking at the Stubby man page or reading the comments in the default configuration file to learn what all of these options do.

Finally, enable and run Stubby with sudo systemctl enable --now stubby.service. You can verify that it is working by running dig @localhost -p 5301 google.com on the same machine.

In Pi-hole, set ::1#5301 and/or 127.0.0.1#5301 as a Custom Upstream DNS Server to start using it! Stubby is now listening for DNS queries at the loopback address on port 5301. When Pi-hole forwards queries to it, it will encrypt them and send them to servers you configured in stubby.yml.

DoT Downstream - stunnel

This part is absent from most of the other guides on this subject, and it requires a few more steps than the upstream side.

Getting a Certificate

Using DoT on the downstream means you will need your own TLS certificate, just like you would need for an HTTPS website. It’s best if you have one that is actually signed by a certificate authority and is issued for the fully-qualified domain name of your DNS server. You can get a certificate like this from Let’s Encrypt using Certbot.

We’ll get a certificate and use it for both DoT and Pi-hole’s web interface. First, we’ll install certbot, and because you will probably need to use a DNS-01 challenge, we’ll also install the appropriate DNS challenge plugin for your domain:

sudo snap install --classic certbot
sudo snap set certbot trust-plugin-with-root=ok
sudo sudo snap install certbot-dns-<PLUGIN>

Even though Certbot currently recommends installing via snap, if your distribution has a certbot package and the DNS plugin you need, that’s probably also fine.

Note

In order to get a certificate from Let’s Encrypt, you need to have registered a domain name (e.g., example.com) and your Pi-hole should have a hostname under that domain name (e.g., pihole.example.com). Your Pi-hole doesn’t actually need to be accessible from the internet. The DNS-01 challenge will create public records under example.com temporarily to prove you own the domain. Install the Certbot DNS plugin corresponding to where you registered the domain.

Next, create this script and make it executable. Note the configurable variables at the top and change them as appropriate.

pihole-certbot-deploy.sh
#!/usr/bin/env bash

set -e

# Into which folder do you want to deploy the pihole's certificates?
# For a standard installation, this would be /etc/pihole.
# For a docker container, this will be the path bind mounted to /etc/pihole 
# in the container.
DEST=/path/to/etc-pihole

# If using pihole in docker, put the location of your docker-compose.yml
# file here. If not using docker, set to the empty string.
COMPOSE=/path/to/docker-compose.yml
#COMPOSE=""

# Enter the user and group name that the certificates will be chowned to
# after installation, and the permissions of any files that contain the
# private key
OWNER=myuser
GROUP=mygroup
PRIVKEYPERM=0600

######################################################################

# Certbot passes the live path of the renewed certificate in this variable
[[ -d "$RENEWED_LINEAGE" ]] || exit 1
SOURCE=$RENEWED_LINEAGE

# Extraneous files
cp "$SOURCE/fullchain.pem" "$DEST/tls.crt"
chown $OWNER:$GROUP "$DEST/tls.crt"
cp "$SOURCE/chain.pem" "$DEST/tls_ca.crt"
chown $OWNER:$GROUP "$DEST/tls_ca.crt"

# This one matters: combine full chain and key to one pem file
cat "$SOURCE/fullchain.pem" "$SOURCE/privkey.pem" > "$DEST/tls.pem"
chown $OWNER:$GROUP "$DEST/tls.pem"
chmod $PRIVKEYPERM "$DEST/tls.pem"

# Restart the container
if ! [ -z "$COMPOSE" ]; then
docker-compose -f "$COMPOSE" down >/dev/null
docker-compose -f "$COMPOSE" up -d >/dev/null
fi
1
Replace this with the path in which to save the certificate/key.
2
Replace this with the path to the Pi-hole Docker compose file, or blank if not using Docker.
3
Replace this with the user and group that should own the certificate/key.

Now we’ll get a certificate and deploy it to Pi-hole’s web interface. Run this command, filling in all of the options as appropriate.

sudo certbot certonly -d <pihole-domain-name> <...dns-plugin-options...> --deploy-hook </path/to/pihole-certbot-deploy.sh>

You’ll need to look up the specific options required for your DNS plugin.

Now, when certbot runs to create or renew the certificate, the new certificate and key will be copied into the location where Pi-hole can use it for its web interface. The certificate is also still available in the usual location of /etc/letsencrypt/live/<pihole-domain-name>.

Setting up stunnel

For the downstream end, we’ll use stunnel. Unlike Stubby, stunnel is not a dedicated DNS forwarder. It’s a TLS proxy, which receives unencrypted connections and forwards them as encrypted, or vice-versa. It’s protocol-agnostic and simply wraps or unwraps TCP packets in TLS encryption, so it’s very versatile. In this case, we want stunnel to act as a server, receiving encrypted connections and forwarding them unencrypted to Pi-hole on the same machine.

Install stunnel using sudo apt install stunnel4, then create the following configuration file:

/etc/stunnel/pihole-DoT.conf
setuid = stunnel4
setgid = stunnel4
foreground = yes

;#######################################
;# SERVER - receive DoT queries
;#######################################

; Binds to both ipv4 and ipv6, connects to localhost
[DoT-server]
accept = :::853
connect = 53
cert = /etc/letsencrypt/live/<pihole-domain-name>/fullchain.pem
key = /etc/letsencrypt/live/<pihole-domain-name>/privkey.pem
ciphers = DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305
ciphersuites = TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
sslVersionMin=TLSv1.2
sslVersionMax=TLSv1.3
1
Fill in the domain name you used for the certificate here.

Finally, enable and run stunnel with sudo systemctl enable --now stunnel@pihole-DoT.service. Now stunnel is listening for encrypted connections on port 853, and forwarding the traffic to Pi-hole after decrypting it. Pi-hole doesn’t need any additional configuration because from its perspective, it’s receiving unencrypted DNS traffic as usual.

You can test that this works by running dig @<your-pi-hole-ip> -p 853 +tls google.com either from the machine running Pi-hole or another computer on your LAN.

Advertising the Service to Clients

In order for DoT on the downstream to be useful, devices on your network need to know they can use it. There are two main ways you can let devices know that DoT is avaiable:

  1. Special DHCP or router advertisement options, much like how regular DNS servers are advertised to clients, and
  2. Special SVCB-type DNS records which clients can use to find DoT information from an unencrypted DNS server.

But I never used either of these. As soon as I turned on stunnel, a lot of the devices on my LAN just automatically started using it. So I wouldn’t worry about DHCP options or SVCB records. In my experience, devices that are capable of using DoT can automatically detect its availability.

Why I’m Not Using DoT Right Now

Now that you’ve done all of this, should you have? I’m a big fan of encrypting evyerthing, but DNS-over-TLS is one instance where, at least in its current state, I believe the benefits aren’t worth the complexity.

With Stubby on the upstream end, I noticed more dropped or unanswered forwarded queries in Pi-hole. Not a lot, not enough to noticeably impact the experience of any end device, but still more. So there is some small performance impact. I was also only getting limited privacy benefits. My Pi-hole only forwards queries to my router, which runs Unbound as a recursive DNS resolver. Because I’m not using another public DNS server upstream, the forwarded queries from my Pi-hole are never even outside of my LAN to be potentially snooped on.

Technically, the downstream end worked flawlessly. I was especially pleased with how many of my end devices were ready to automatically start using DoT as soon as it was available. But there was one big downside here as well: because stunnel stood between DoT clients and Pi-hole, from Pi-hole’s perspective, all of the DoT queries were coming from ‘localhost’, not from the original client. So having DoT enabled seriously hurt my ability to use Pi-hole’s logs and dashboard to monitor the traffic on my LAN. And like the upstream, DoT on the downstream only encrypted the traffic while it was on my LAN anyway.

There are two things that could happen someday that would make me change my mind. First, DoT could become supported natively by dnsmasq, and eventually Pi-hole itself. This isn’t out of the question; DoT is supported already by bind and unbound. If this happened, then there would be no extra components required, the setup would be easier, and performance wouldn’t suffer. Also, Pi-hole would be able to distinguish between DoT clients in its logs and dashboard. At that point, in my mind, there would be no reason not to use DoT.

Second, clients could start to prefer encrypted DNS to the point where having it is a practical necessity. Some devices and programs already prefer a third-party encrypted DNS provider over the local network DNS if encryption is not available. For example, Firefox uses DNS-over-HTTPS by default unless the local DNS server blocks the canary domain. Someday, these default preferences could become stronger and stronger until the only way to get your devices to use your ad-blocking DNS server is if that server supports encrypted DNS. At that point, for me, the downsides of not having DoT would outweight the downsides of having it.

Hopefully someday I’ll be able to enable DoT without any of the downsides.