Skip to content

Instantly share code, notes, and snippets.

@sviridoff
Last active January 13, 2022 11:36
Show Gist options
  • Save sviridoff/42d879f059e63a1ff8f72904d55e099a to your computer and use it in GitHub Desktop.
Save sviridoff/42d879f059e63a1ff8f72904d55e099a to your computer and use it in GitHub Desktop.
Ad-blocking with dnsmasq on Ubuntu

Ubuntu dnsmasq adblock

Ad-blocking with dnsmasq on Ubuntu configuration

Install dnsmasq:

sudo apt update
sudo apt install dnsmasq

Create dnsmasq config:

sudo touch /etc/dnsmasq.conf

Add to it following data:

domain-needed
bogus-priv
cache-size=1000
resolv-file=/etc/resolv.dnsmasq
strict-order
conf-file=/etc/dnsmasq-hosts.conf
expand-hosts

Create dnsmasq resolve file:

sudo touch /etc/resolv.dnsmasq

Add to it:

nameserver 8.8.8.8
nameserver 8.8.4.4

nameserver 208.67.222.222
nameserver 208.67.220.220

nameserver 1.1.1.1
nameserver 1.0.0.1

Stop resolver service:

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

Add following to /etc/NetworkManager/NetworkManager.conf behind [main] caption to disable auto creation of resolv.conf file:

dns=none
rc-manager=unmanaged

Set DNSStubListener=no in /etc/systemd/resolved.conf in order to avoid conflicts with dnsmasq, disable resolver to listen port 53.

You can check with following command if it worked sudo lsof -i:53.

Remove symlink and resolv config:

ls -lh /etc/resolv.conf
sudo rm /etc/resolv.conf

Create daily cronjob:

sudo touch /etc/cron.daily/adblock
sudo chmod +x /etc/cron.daily/adblock

With following inside:

#!/bin/bash

curl -SLso /etc/dnsmasq-hosts.conf https://raw.githubusercontent.com/notracking/hosts-blocklists/master/dnsmasq/dnsmasq.blacklist.txt

systemctl restart dnsmasq

Execute it once:

./adblock

Restart dnsmasq service:

sudo systemctl restart dnsmasq

Check for status:

systemctl status dnsmasq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment