Using Synology DNS Server to Block Adverts

by 13th September 2021Platforms, Security, Synology, Technological Thoughts

Edited 23rd September 2021: I’ve now modified the script so that it can pull in a list of remote allowlists. I’ve also had issues with the and have modified this.

I am constantly irritated by the number of adverts and clickbait that I see whenever I use apps or traverse the web, and have looked for effective ways block them.

There are lots of extensions for various web browsers that you can use, and whilst these allow a very targeted approach to removing adverts, they only work within the browsers themselves.

Another option is to block domains entirely. This can be fraught with dangers, as you can effectively kill websites and apps completely with this approach. However, by using safe, sanitised blocklists, false positives can be reduced.

Using the Synology DNS Server as a filter

I use my Synology DNS Server to route traffic both for my intranet and the internet and tried using the AdGuard DNS servers as a solution. However, I found that these were incredibly slow, resulting in significant lag for websites and applications.

I then came across a simple blog post by Gerzon ( which explained how to set up the Synology DNS Server to block adverts.

This works extremely well, but has certain limitations in its flexibility, as the script was designed to pull a single blocklist and apply it.

An effective way to block adverts and malicious websites

To make the Synology DNS Server into an effective filter for advertising domains and other malicious websites, I have written a shell script below which gives you greater flexibility. The script itself:

  • Allows you to create a list of blocklists to apply to your Synology DNS Server.
  • Merges, deduplicates and sorts the blocklists.
  • Formats the blocklists appropriately for the Synology DNS Server.
  • Allows you to create an allowlist of permitted domains, which if found in the merged blocklist, will be removed.
  • Reloads the DNS Server.

Synology DSM Configuration

  1. Log into your Synology NAS as an administrator.
  2. Install the DNS Server package on your Synology.
  3. Open the DNS Server application.
  4. On the Zones tab, Click on the Create button and select the option to create a new Master zone with the following values:
    • Domain type: “Forward Zone”
    • Domain name: “”
    • Master DNS Server: your Synology IP address (e.g. “”)
    • Serial format: “Date (YYYYMMDDNN)
  5. On the Resolution tab,make sure that the “Enable resolution service” check box is ticked and you have added external DNS servers to resolve any unknown domain names.
    In the example below, I have selected Cloudflare ( as my first DNS resolver and Google ( as my second.
Creating the settings
Synology DNS Resolution Settings

Installing and Executing the Script using Telnet or SSH

    1. Download the adblock files or copy the code below.
    2. Create your own or copy the blocklist.txt and allowlist files to your Synology website. Alternatively, you can copy them to:
    3. Edit the, updating the Configurable Parameters section (lines 18-20) for Blocklist and Allowlist to point to your own files (either on your Synology website or locally) and save it.
    4. Log in to your Synology NAS using Telnet (or preferably SSH!).
    5. Copy the script to your NAS in the following directory:
      cp /var/packages/DNSServer/target/script/
    6. Give the script execute permissions and make the owner the DNSServer
      chmod 755 /var/packages/DNSServer/target/script/
      chown DNSServer:DNSServer /var/packages/DNSServer/target/script/
    7. To run the script, you then just need to call it:

Scheduling a Task in Synology

Schedule a Task in Synology

Scheduling a Task in Synology

 You can also schedule this to regularly update the blocklist using a task in Synology. Once configured, you can leave the script to run when scheduled and always be up to date.

  1.  Log into your Synology NAS as an administrator.
  2. Open the Control Panel.
  3. Click on the Task Scheduler icon.
  4. Click on the Create button and select the option to create a Scheduled Task > User-defined script.
  5. Name your task and set the user to run the task as “root”.
  6. Click on the Schedule tab and set the schedule you want to run. Whilst you can run it every day, I have set mine to run monthly as it reduces the load on both your server and the blocklist servers.
  7. Click on the Task Settings tab and enter the following for the User-defined script:
    bash /var/packages/DNSServer/target/script/
  8. Click on the OK button to create the scheduled task.

Taboola, Google and Amazon Ads

One of the main advert serving sites that was annoying me was Taboola, which seems to serve up clickbait news articles constantly (particularly on IMDB), so I created my own blocklist for this. I have also added a few Google and Amazon advert domains to this list as well.

Feel free to copy the list below (I have also included it in the code download) and use it as your own blocklist. Script

#(C)2021 Europoint Communications ltd
#Title       :
#Version     : V1.03
#Author      : Matthew Cunliffe (
#Description : Script to create a domain name based blocklist for use with Synology DNSServer
#Dependencies: Synology DNSServer package, sed, wget, sort, comm, blocklist.txt, allowlist.txt
#Usage       : ./
# Version history:
#2021.09.13 Bash script to pull domain names of URLs to be blocked by Synology DNSServer and install them
#2021.09.17 Added in the option to include a remote allow list to be processed, but it expects it in a limited Adblock format
#2021.09.22 Corrected the creation
#2021.09.23 Added option to download multiple allowlists

#Script to be placed in /var/packages/DNNSServer/target/script/

#Configurable Parameters

#####Do Not Change Below This Line#####
ParseBlockList ()
	#Retrieve the list of files to download and merge them into the blocklist.txt file
	wget -q -i ${Blocklist} -O blocklist.txt
	#Sort the file alphabetically and remove any duplicates
	sort -u -o blocklist.txt{,}
	#If the blocklist file exists and there is an allowlist set, then parse the allow list.
	if [ -s "blocklist.txt" ] && [ -n "${Allowlist}" ]
	#If the blocklist file still exists, then tidy it up.
	if [ -s "blocklist.txt" ]
		#Remove any comments in the file and format the file for use with Synology DNSServer.
		sed -i -e '/^#/d' -e 's/.*/zone \"&\" { type master; notify no; file \"\"; };/' blocklist.txt
		return 1
	#If the blocklist file is still ok, then exit with a success statement.
	if [ -s "blocklist.txt" ]
		return 0
		return 1

ParseAllowList ()
	#Retrieve the list of files to download and merge them into the allow.txt file.
	wget -q -i ${Allowlist} -O allowlist.txt
	#Check if an allowlist has been created and if so, remove the allowed domains.
	if [ -s "allowlist.txt" ]
		#Retrieves the Remote Allow list and processes it to remove Adblock data formats and appended values.
		#Then appends the file to the allowlist.
		sed -i -e '/\[Adblock Plus/d' -e '/^\!/d' -e '/^#/d' -e 's/^@@||//' -e 's/\^\*\$.*//' -e 's/\?.*//' -e 's/\*\$.*//' -e 's/0\.0\.0\.0 //'  -e 's/127\.0\.0\.1 //'  allowlist.txt
		#Sort the file alphabetically and remove any duplicates.
		sort -u -o allowlist.txt{,}	
		#Remove everything in the allowlist from the blocklist.
		comm -23 blocklist.txt allowlist.txt > inter.txt; mv inter.txt blocklist.txt; rm allowlist.txt;

UpdateBlockList () 
	#Backup the old database, rename the final file as the database and make sure it is owned by the DNSServer
	gzip -f blocklist.db 
	mv blocklist.txt blocklist.db
	#If the blocklist file is ok and the old one has been backed up, then exit with a success statement.
	if [ -s "blocklist.db" ] && [ -s "blocklist.db.gz" ]
		chown DNSServer:DNSServer blocklist.db
		return 0
		return 1

RestartDNS () 
	#Restart DNS

ModifyZoneDataFile () 
	#Acknowledgements to DMajo and Steven T Black for the original code used for this function 
	#Modify Zone Data File if required
	#Include the new zone data
    if [ -f blocklist.db ] && [ -f ]
        grep -q 'include "/var/packages/DNSServer/target/named/etc/zone/data/blocklist.db";' || echo 'include "/var/packages/DNSServer/target/named/etc/zone/data/blocklist.db";' >>
	#Rebuild the master
    Now=$(date +"%Y%m%d")
	if [ -f "$ZoneMasterFile" ]
		rm -f "$ZoneMasterFile"

	  # rebuild the zone master file with the updated serial number
	  { echo '$TTL 86400     ; one day';
		echo '@ IN SOA (';
		echo '  '${Now}'00   ; serial number YYYYMMDDNN';
		echo '  86400        ; refresh 1 day';
		echo '  7200         ; retry 2 hours';
		echo '  864000       ; expire 10 days';
		echo '  86400 )      ; min ttl 1 day';
		echo '  IN NS';
		echo '  IN A';
		echo '* IN A'; } > "$ZoneMasterFile"

#Move to the directory containing the data for the DNS files
cd ${ZoneDataDir}
#Call functions to execute
if [ $?==0 ]
	echo "Unable to update Blocklist"
	exit 1
if [ $?==0 ] 
	exit 0
	echo "Unable to update and restart DNS"
	exit 1

Blocklist.txt Example

Allowlist.txt Example

Taboola, Google and Amazon Blocklist

#Taboola Blocklist

#Google BlockList

#Amazon BlockList
Matthew Cunliffe

Matthew Cunliffe


Matthew is an IT specialist with more than 24 years experience in software development and project management. He has a wide range of interests, including international political theory; playing guitar; music; hiking, kayaking, and bouldering; and data privacy and ethics in IT.


Submit a Comment

Your email address will not be published. Required fields are marked *

Share this post