# How to use Hosts File on Linux to Block Ads Tracking, Malware Domains & Annoyances

**URL:** <https://onehack.st/t/how-to-use-hosts-file-on-linux-to-block-ads-tracking-malware-domains-annoyances/64962>\
**Category:** Tools & Scripts\
**Tags:** tools, tips-tricks, browsers\
**Created:** [January 4, 2020, 2:23am UTC](https://onehack.st/t/how-to-use-hosts-file-on-linux-to-block-ads-tracking-malware-domains-annoyances/64962 "2020-01-04T02:23:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheJoker](https://onehack.st/user_avatar/onehack.st/thejoker/32/22660_2.png) [@TheJoker](https://onehack.st/u/TheJoker)\
**Post date:** [January 4, 2020, 2:23am UTC](https://onehack.st/t/how-to-use-hosts-file-on-linux-to-block-ads-tracking-malware-domains-annoyances/64962/1 "2020-01-04T02:23:02Z")

</div>

![image](https://onehack.st/uploads/default/original/3X/9/6/96416d355625f9d82f046ea9250895da679d0161.png)

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 .

On macOS you can use GasMask which wasn’t updated in ages, but it still works. However, it still can’t deal with HTTPS sources. 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:

```auto
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

```

 ![image](https://onehack.st/uploads/default/original/3X/d/c/dc15ac3e9bfc7dc3a73695e9a455a37ce760edbc.jpeg)

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

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

 ![image](https://onehack.st/uploads/default/original/3X/a/3/a34585515a5260e816a235e8fb3bc6d7ab8a98c3.jpeg)

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

2. Execute the script with `hblock`

 ![image](https://onehack.st/uploads/default/original/3X/2/6/2698174fe2e8589b1f28cbd8d40fdc163358e32a.jpeg)

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

```auto
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

```

 ![image](https://onehack.st/uploads/default/original/3X/0/c/0c835fdfaa827760ed9118660be99f1c1d2cf86f.jpeg)

# Fore more info check out the official project on [GitHub](https://github.com/hectorm/hBlock). [This](https://filterlists.com/) is a great directory of filter and host lists.
