|
| 1 | +## Configuring DNS-Over-HTTPS using `dnscrypt-proxy` [^guide] |
| 2 | + |
| 3 | +To utilize DNS-Over-HTTPS (DoH) or other encrypted DNS protocols with Pi-hole, preventing man-in-the-middle attacks between Pi-hole and upstream DNS servers, the following sections explain how to install the flexible and stable [dnscrypt-proxy](https://github.com/DNSCrypt/dnscrypt-proxy) tool. |
| 4 | + |
| 5 | +As an alternative tool to this end, consider [cloudflared](https://github.com/cloudflare/cloudflared), for which a [guide](cloudflared.md) exists as well. |
| 6 | + |
| 7 | +### Installing `dnscrypt-proxy` |
| 8 | + |
| 9 | +Raspberry Pi OS and Debian as well as Ubuntu come with packages for `dnscrypt-proxy`, which makes its installation a breeze: |
| 10 | + |
| 11 | +```shell |
| 12 | +sudo apt update |
| 13 | +sudo apt install dnscrypt-proxy |
| 14 | +``` |
| 15 | + |
| 16 | +### Configuring `dnscrypt-proxy` |
| 17 | + |
| 18 | +By default, `FTLDNS` listens on the standard DNS port 53. |
| 19 | + |
| 20 | +To avoid conflicts with `FTLDNS`, edit `/usr/lib/systemd/system/dnscrypt-proxy.socket`, ensuring `dnscrypt-proxy` listens on a port that is not in use by other services, such as port 5053. |
| 21 | + |
| 22 | +Specifically, the following settings in `/usr/lib/systemd/system/dnscrypt-proxy.socket`, let dnscrypt-proxy listen on localhost on port 5053: |
| 23 | + |
| 24 | +``` |
| 25 | +ListenStream=127.0.0.1:5053 |
| 26 | +ListenDatagram=127.0.0.1:5053 |
| 27 | +``` |
| 28 | + |
| 29 | +Also edit `/etc/dnscrypt-proxy/dnscrypt-proxy.toml`, updating the following settings: |
| 30 | + |
| 31 | +```toml |
| 32 | +# Use systemd socket activation: |
| 33 | +listen_addresses = [] |
| 34 | + |
| 35 | +# Populate `server_names` with desired DoH/DNSCrypt upstream DNS servers listed in https://dnscrypt.info/public-servers/. |
| 36 | +# Example for Cloudflare malware-blocking DNS: |
| 37 | +server_names = ['cloudflare-security'] |
| 38 | +``` |
| 39 | + |
| 40 | +### Restarting Services |
| 41 | + |
| 42 | +Run the following commands to restart `dnscrypt-proxy` and `FTLDNS`: |
| 43 | + |
| 44 | +```shell |
| 45 | +sudo systemctl restart dnscrypt-proxy.socket |
| 46 | +sudo systemctl restart dnscrypt-proxy.service |
| 47 | +sudo systemctl restart pihole-FTL.service |
| 48 | +``` |
| 49 | + |
| 50 | +### Reviewing Service Status |
| 51 | + |
| 52 | +Run the following commands to review the status of each restarted service: |
| 53 | + |
| 54 | +```shell |
| 55 | +sudo systemctl status dnscrypt-proxy.socket |
| 56 | +sudo systemctl status dnscrypt-proxy.service |
| 57 | +sudo systemctl status pihole-FTL.service |
| 58 | +``` |
| 59 | + |
| 60 | +Each service is expected to be in active (running) state. |
| 61 | +Review the log files shown if a service didn't restart successfully. |
| 62 | + |
| 63 | +### Configuring Pi-hole |
| 64 | + |
| 65 | +Log into the Pi-hole admin web interface. |
| 66 | +Navigate to Settings and from there to the DNS tab. |
| 67 | + |
| 68 | +Under "Upstream DNS Servers" on the left, uncheck all boxes for public DNS servers. |
| 69 | +Under "Upstream DNS Servers" on the right, check only the box for "Custom 1 (IPv4)" and fill the box with the IP address and port `dnscrypt-proxy` listens on, such as `127.0.0.1#5053`: |
| 70 | + |
| 71 | + |
| 72 | +Click on `Save` at the bottom. |
| 73 | + |
| 74 | +### Updating `dnscrypt-proxy` |
| 75 | + |
| 76 | +Since you installed `dnscrypt-proxy` via APT, updating `dnscrypt-proxy` is a matter of running the following commands: |
| 77 | + |
| 78 | +```shell |
| 79 | +sudo apt update |
| 80 | +sudo apt upgrade |
| 81 | +``` |
| 82 | + |
| 83 | +### Uninstalling `dnscrypt-proxy` |
| 84 | + |
| 85 | +To uninstall `dnscrypt-proxy`, run the command `sudo apt remove dnscrypt-proxy`. |
| 86 | +Update the Pi-hole DNS settings to use another upstream DNS server. |
| 87 | + |
| 88 | +[^guide]: Guide based on [this guide by Fabian Foerg | ffoerg.de](https://ffoerg.de/posts/2024-01-28.shtml) |
0 commit comments