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

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

  1. Please check Videos with over 1 billion views.
    These are the Videos that I couldn’t watch because ytadsblocker mistakenly blocked them.

    More. You can use the command: pihole -b -d [domain name] to remove domains that have been added to the block list. Simpler than sqlite3, because I had to rerun the command pihole -b -d to remove the blocked domain because ytadsblocker was unable to remove it.
    Thank you. Hope you update ytadsblocker soon

    1. Hi Darias,

      I’ll take a look to the command pihole -b -d [domain].

      Regarding the videos with over a billion views getting blocked, I’d say that ytadsblocker was blocking too many subdomains and Youtube finally stop working. This is a known problem, at the moment there are not solutions.

      Kind regards

  2. Script still works? Watching YT, and appears ads. Checked ytadblocker log, no adding new ads subdomais to blacklist.
    pihole log, same time when I saw add:
    g 6 16:00:20 dnsmasq[1059525]: query[A] from
    Aug 6 16:00:20 dnsmasq[1059525]: cached is ::
    Aug 6 16:00:20 dnsmasq[1059525]: query[A] r3— from
    Aug 6 16:00:20 dnsmasq[1059525]: forwarded r3— to
    Aug 6 16:00:20 dnsmasq[1059525]: dnssec-query[DS] to
    Aug 6 16:00:20 dnsmasq[1059525]: reply is no DS
    Aug 6 16:00:20 dnsmasq[1059525]: validation result is INSECURE
    Aug 6 16:00:20 dnsmasq[1059525]: reply r3— is
    Aug 6 16:00:20 dnsmasq[1059525]: reply is
    Aug 6 16:00:22 dnsmasq[1065535]: query[A] from
    Aug 6 16:00:22 dnsmasq[1065535]: cached is ::

    And ytaddblocker log:
    [2021-08-06 15:58:58] No new subdomains to added.
    [2021-08-06 16:03:58] Checking /var/log/pihole.log…
    [2021-08-06 16:03:59] No new subdomains to added.

    1. Hi Seniux,

      It’s still working, anyway I’ll take a look to everything from the scracth, since I have not updated the scripts from long time ago.

      I’ll keep you posted.

      Kind regards,

  3. 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,

Responder a Deivid Cancelar la respuesta

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

Logo de

Estás comentando usando tu cuenta de Salir /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s