Posted inGuides / Linux / Privacy / Security

HOW TO: Use the Hosts File on Linux to Block Ads, Tracking, Malware Domains and Annoyances

 

Welcome to the internet, it’s full of advertisers trying to sell you stuff you don’t need, trackers trying to steal and sell your data and malicious content waiting to hijack your device.

There are a few steps you can take to minimise these threats. Like taking advantage of the built in hosts file on your computer and a browser ad-blocker like uBlock Origin .

If you’d rather not maintain a hosts file, you can also use AdGuard DNS to block ads and trackers at the DNS level.

On macOS, see the updated Gas Mask and hosts file guide for current compatibility caveats and manual editing instructions. On Linux, a great option is hBlock.

hBlock is a POSIX-compliant shell script, designed for Unix-like systems, gets a list of domains that serve ads, tracking scripts and malware from multiple sources and creates a hosts file (alternative formats are also supported) that prevents your system from connecting to them.

1. Install hBlock:

curl -o /tmp/hblock 'https://raw.githubusercontent.com/hectorm/hblock/v2.1.2/hblock' \
  && echo 'd711ae3b0c6affce4237e32856d1b0e6fcff1e84523e5c8bbb25e79b4074cfe2  /tmp/hblock' | shasum -c \
  && sudo mv /tmp/hblock /usr/local/bin/hblock \
  && sudo chown root:root /usr/local/bin/hblock \
  && sudo chmod 755 /usr/local/bin/hblock

 

1.1 If you’re using Arch/Manjaro hBlock is available in AUR

 

2. Run hblock -h to see the script arguments and how can you modify hBlock

 

3. Backup your original hosts file in /etc/hosts just in case. hBlock will modify the original file.

4. Execute the script with hblock

 

5.  The following commands will schedule a daily update of the hosts file:

curl -o '/tmp/hblock.#1' 'https://raw.githubusercontent.com/hectorm/hblock/v2.1.2/resources/systemd/hblock.{service,timer}' \
  && echo '70964235a03152d4bc68096a0b99cc59e3f77595b99330f8c55dcca79d7164ff  /tmp/hblock.service' | shasum -c \
  && echo '79ecc28c13b2489400bd5ddc0ee61ddaf6c3225acb1d54b5cb4026f822ae60e8  /tmp/hblock.timer' | shasum -c \
  && sudo mv /tmp/hblock.{service,timer} /etc/systemd/system/ \
  && sudo chown root:root /etc/systemd/system/hblock.{service,timer} \
  && sudo chmod 644 /etc/systemd/system/hblock.{service,timer} \
  && sudo systemctl daemon-reload \
  && sudo systemctl enable hblock.timer \
  && sudo systemctl start hblock.timer

 

Fore more info check out the official project on GitHub. This is a great directory of filter and host lists.

Support FSMdotCOM on Liberapay