🛡️ How to install dnscrypt proxy with ad blocker on Linux

How to install dnscrypt proxy app with ad / malware blocker on Linux operating system with WireGuard or OpenVPN?

DNSCrypt is a protocol for authenticating and encrypting DNS traffic between your device and recursive DNS servers like Google, Cloudflare, ISP / 3rd party servers, or your own DoH server based on Nginx + Bind9.

Dnscrypt-proxy is a free and open source application supporting protocols such as DNSCrypt v2 and DNS-over-HTTPS (DoH).

It also prevents DNS spoofing.

It uses cryptographic signatures to verify that responses come from the selected DNS resolver and have not been tampered with.

Free DNSCrypt-enabled resolvers are available worldwide, including DoH-enabled resolvers:

https://dnscrypt.info/public-servers/

In other words, we are using dnscrypt-proxy to secure communication between the client and the DNS resolver.

How to install dnscrypt-proxy

This article shows you how to install dnscrypt-proxy with ad blocker on Linux with OpenVPN / WireGuard VPN server to secure communication on public / unreliable Wi-Fi network.

Installing dnscrypt proxy on Linux

Run the command according to your Linux distribution as root user:

Debian/Ubuntu пользователи используйте apt/apt-get ##
$ sudo apt install dnscrypt-proxy
## Alpine Linux пользователи используйте apk##
$ sudo apk add dnscrypt-proxy
## CentOS / RHEL пользователи:
## Добавьте EPEL репозиторий на CentOS 8 или RHEL 8
## используйте yum ##
$ sudo yum install dnscrypt-proxy
## Arch Linux используйте pacman ##
$ sudo pacman -S dnscrypt-proxy
## OpenSUSE/SUSE Linux пользователи используйте zypper ##
$ sudo zypper in dnscrypt-proxy
## Fedora пользователи используйте dnf ##
$ sudo dnf install dnscrypt-proxy

Dnscrypt proxy configuration

Edit dnscrypt-proxy.toml in the / etc / dnscrypt-proxy / directory.

For instance:

$ ls -l /etc/dnscrypt-proxy/
$ sudo vim /etc/dnscrypt-proxy/dnscrypt-proxy.toml

First, we need to set up a list of used servers. Let’s use both Google and Cloudflare:

server_names = [ 'google' 'cloudflare']

I can use cloudflare too:

server_names = [ 'cloudflare']

Configure the list of listening local addresses and ports:

listen_addresses = ['127.0.0.1:53', '[::1]:53']

For my VPN 172.168.0.0/24:

listen_addresses = ['127.0.0.1:53', '172.168.0.1:53' ]

If you have an IPv6 connection, use the servers available over IPv6 (IPv4 by default):

ipv6_servers = true

Make sure we are using servers that implement DNSCrypt and DoH protocol.

dnscrypt_servers = true
doh_servers = true

Make sure we enable DNS cache to reduce latency and outgoing traffic:

cache = true

The server must support DNS Security Extensions (DNSSEC):

require_dnssec = true

Adblock lists consist of one template per line. Examples of valid patterns:

##                                                                  
##   example.com
##   =example.com
##   ads.*                        
##   ads*.example.*               
##   ads*.example[0-9]*.com

Blocking rules file path:

blacklist_file="/etc/dnscrypt-proxy/blacklist.txt"

Save and close the file.

Installing adblocker list for dnscrypt proxy

Here is a small bash script to download and update the list.

#!/bin/bash
DEST="/etc/dnscrypt-proxy/blacklist.txt"
#
# Blocks both adware + malware 
# See for other lists https://github.com/StevenBlack/hosts
SRC="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
TMP_B_FILE="$(mktemp)"
TMP_B_FILE_SORTED="$(mktemp)"
wget --timeout=10 --tries=5 -qO- "${SRC}" | grep -Ev "(localhost)" | grep -Ev "https://itsecforu.ru/2020/10/12/%F0%9F%9B%A1%EF%B8%8F-%D0%BA%D0%B0%D0%BA-%D1%83%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%82%D1%8C-%D0%BF%D1%80%D0%BE%D0%BA%D1%81%D0%B8-dnscrypt-%D1%81-%D0%B1%D0%BB%D0%BE%D0%BA%D0%B8%D1%80%D0%BE/#" | sed -E "s/(0.0.0.0 |127.0.0.1 |255.255.255.255 )//" >> "${TMP_B_FILE}"
awk '/^[^#]/ { print $1 }' "${TMP_B_FILE}" | sort -u > "${TMP_B_FILE_SORTED}"
cp -f "${TMP_B_FILE_SORTED}" "$DEST"
rm -f "${TMP_B_FILE}" "${TMP_B_FILE_SORTED}"

Running the script:

# ./update-adblocker.sh

Checking the list:

# more /etc/dnscrypt-proxy/blacklist.txt

Be sure to run the update-adblocker.sh script daily to get an updated list of hosts. Add a new cron job like this:

@daily /path/to/update-adblocker.sh

Besides ads and malware, you can block fake news, gambling, porn, and social media!

How to enable dnscrypt proxy service

Use the systemctl command to enable the dnscrypt-proxy service on Linux:

$ sudo systemctl enable dnscrypt-proxy

Service start:

$ sudo systemctl start dnscrypt-proxy

Service check:

$ sudo systemctl status dnscrypt-proxy

DNSCrypt status:

● dnscrypt-proxy.service - DNSCrypt-proxy client
   Loaded: loaded (/usr/lib/systemd/system/dnscrypt-proxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-10-10 05:44:29 EDT; 4s ago
     Docs: https://github.com/jedisct1/dnscrypt-proxy/wiki
 Main PID: 5108 (dnscrypt-proxy)
    Tasks: 8 (limit: 11328)
   Memory: 13.8M
   CGroup: /system.slice/dnscrypt-proxy.service
           └─5108 /usr/bin/dnscrypt-proxy --config /etc/dnscrypt-proxy/dnscrypt-proxy.toml

Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Wiring systemd TCP socket #1, dnscrypt-proxy.socket, [::1]:53
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Wiring systemd UDP socket #2, dnscrypt-proxy.socket, 127.0.0.1:53
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Wiring systemd UDP socket #3, dnscrypt-proxy.socket, [::1]:53
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Source [public-resolvers] loaded
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: /etc/dnscrypt-proxy/relays.md: open sf-ux5n6prgb5kk4yzf.tmp: read-only file system
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Source [relays] loaded
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Firefox workaround initialized
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: [cloudflare] OK (DoH) - rtt: 18ms
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: Server with the lowest initial latency: cloudflare (rtt: 18ms)
Oct 10 05:44:29 centos-8-cloud.sweet.home dnscrypt-proxy[5108]: dnscrypt-proxy is ready - live servers: 1

Alpine Linux users should use rc-update command and rc-service command:

$ sudo rc-update add dnscrypt-proxy
$ sudo rc-service dnscrypt-proxy

Sidebar