Bash Script to Block Youtube advertisements in Pi-hole

Image 1. Pi-Hole and Raspberry Pi

I have been using the Raspberry Pi with Pi-hole, and to be honest I could not be happier, Pi-hole has been working with non-stop since then. Nowadays, the only problem i have found is that it is almost impossible to block the Youtube advertisements. So I decide to do a research on this, and found that Google is generating subdomains for each ad with following format:

Reading some forums around Internet i saw that some people have made public lists with all these subdomains, however, in my case none of those public lists worked, since it seems that the subdomain generate by Google depends on the country you are watching the video and I could think it is totally random and maybe never repeats (?).

The following are the public lists with the sub-domains:

I used these lists on my Pi-hole but in two days i never used one of them. Having seen this I decided to create regular expression to extract all the Googlevideo sub-domains that all my devices have been requesting to my Pi-hole:

cat /var/log/pihole.log | egrep "r([0-9]{1,2}).*" | awk '{print $8}' | sort | uniq

The file pihole.log contains all the DNS request, so with the previous command we can get all the sub-domains and forward them to the Blacklist file of Pi-hole /etc/pihole/blacklist.txt, then we can check them in the following section:

Image 2. Pi-Hole: Blacklist

With that being said, we conclude that if we load a video in Youtube, we see an AD and then we extract the Googlevideo sub-domain where the ad comes from and put it in the blacklist, we will stop seeing ads from that sub-domain.


I have create a script in Bash to automate this task and this is how it works:

  • It can be installed as a service with the option: install
  • While the script is being installed it copies all the Pi-hole logs in /var/log and move them to /tmp
  • All the logs are decompressed there and with the previous regular expression all the sub-domains are extracted
  • All the sub-domains are added to the file: /etc/pihole/blacklist.txt
  • Once the installation is done, we can execute: systemctl start ytadsblocker
  • Starting the script as a service it will start to read the file /var/log/pihole.log every 4 minutes to extract all the googlevideo sub-domains. If a sub-domain is found it will be added to the file blacklist.txt
  • The log of the script is the following file /var/log/ytadsblocker.log. There you can check all the new sub-domains that were added.

Since the script has been working on my Pi-hole I am getting a bit less of Youtube advertisements, so i get any issue with this i will inform you updating this post.

If you are interested in this script you can find the code here or you can go to my GitHub to help me to improve the script.  


The installation is pretty easy, just download the script executing:

git clone

Then move to the directory ytadsblocker and give execution permissions to the script:

cd ytadsblocker
chmod a+x

Execute the script passing the parameter ‘install’ :

./ install

Now we just have to start the service:

systemctl start ytadsblocker

4 comentarios sobre “Bash Script to Block Youtube advertisements in Pi-hole

  1. Hi, thanks for your great work.
    I have a question, I can see blacklist entries are growing, but when I tried access one of the url on the browser, I am still face google 404 default error page.
    Is this blocking feature still working?

    1. Hi, Ponco,

      It’s still working, of course. But we still have the same problem, as I’ve commented on this several times: it’s totally impossible to block all the subdomains since Youtube stop working when you have a lot of subdomains in the black list.

      Kind regards,


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s