Block Ads & Trackers System-wide in Ubuntu Linux using hBlock

Last updated: March 3, 2025 — Leave a comment

This tutorial shows how to block ads, trackers, and malware domains system-wide in Ubuntu and other Linux desktop using hBlock.

hBlock is a free open-source script that collects a large list of domains that serve ads, tracking scripts and malware from multiple sources. It blocks them by mapping all those domain names to 0.0.0.0 using hosts file, thus it works system-wide.

NOTE 1: Disable ads may hurt the websites that you prefer, as many sites rely on displaying ads to keep alive.

NOTE 2: Without installing hBlock, advanced users may visit its website to get the latest list of ads domains and block by manually editing the hosts file.

Step 1: Install hBlock

To get hBlock, press Ctrl+Alt+T to open up a terminal window, then copy and paste the whole commands below and run as single:

curl -o /tmp/hblock 'https://raw.githubusercontent.com/hectorm/hblock/v3.5.0/hblock' \
  && echo 'bb8f632fcb101ea017fb00a8d02925dfc7d5a6d075e2cb30e4bc293e5c0628b1  /tmp/hblock' | shasum -c \
  && sudo mv /tmp/hblock /usr/local/bin/hblock \
  && sudo chown 0:0 /usr/local/bin/hblock \
  && sudo chmod 755 /usr/local/bin/hblock

The commands will first using curl to download the script from Github, then verify file integrity, install (move) to /usr/local/bin directory, set the ownership and read/read/executable permissions.

The sudo command requires user authentication, you need to type password (no asterisk feedback) when it’s blinking at “[sudo] password for USER”.

As time goes by, the project may release newer versions when you see this tutorial. You may go to the project website if the command above is outdated.

Step 2: Block / Unblock Ads and Trackers

Before start using hBlock, it’s better to backup the /etc/hosts first if you have custom settings there. To do so, run command:

sudo cp /etc/hosts /etc/hosts.backup

To start blocking ads and trackers, simply run the command below in terminal:

hblock

It will start downloading list of ads domains and tracking scripts from Github and apply to block them. As it will make changes to ‘/etc/hosts’ file, you need to type user password (when it asks) to grant permission.

If everything goes well, you may now open any website in web-browser or any app that contain advertisements to if it works!

To temporarily disable hBlock, use command:

hblock -S none -D none

And, you may re-run hblock to start it again.

For choice, user may create and add domain names into ~/.config/hblock/allow.list file (create if not exist) to unblock certain domains. For more about how to use the script, run hblock --help.

Add Toggle button to Turn ON/OFF hBlock (GNOME only)

Some websites or apps may refuse to work well with ads disabled. Without running the command above again and again, GNOME users may install an extension to add a toggle button in top-right Quick Settings menu.

NOTE: The Extension so far only works in GNOME 45/46/47, meaning for Ubuntu 24.04/24.10, recent Fedora Workstation, etc distributions.

For Ubuntu, first search for & install “Extension Manager” from App Center (filter by Debian package).

Then, launch the tool and navigate to “Browser” tab. Finally, search then install “Blocker” extension.

Or, open the extension page in EGO:

Then use the ON/OFF switch to install/uninstall the extension. You need to install browser extension first if it prompts and refresh.

Uninstall hBlock

To uninstall hBlock, you may first disable blocking ads by running command in terminal (Ctrl+Alt+T):

hblock -S none -D none

Then, delete the script:

sudo rm /usr/local/bin/hblock

If need, restore /etc/hosts from the backup file:

sudo mv /etc/hosts.backup /etc/hosts

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*