Hostsblock
an ad- and malware-blocking script for Linux
Install / Use
/learn @gaenserich/HostsblockREADME
Hostsblock
An ad- and malware-blocking utility for POSIX systems
Contents
- Description: Features
- Installation: Dependencies, Arch Linux, Other POSIX
- Configuration: Edit
hostsblock.conf, Enable Timer, Enable Postprocessing - Usage: Configuring
sudo, Manual Usage, UrlCheck Usage (examples) - FAQ
- News & Bugs: Upgrading to 0.999.8
- License
Description <a name="description"></a>
Hostsblock is a POSIX-compatible script designed to take advantage of the [/etc/hosts][h] file to provide [system-wide blocking][0] of internet advertisements, malicious domains, trackers, and other undesirable content.
To do so, it downloads a configurable set of blocklists and processes their entries into a single [HOSTS][h] file.
Hostsblock also provides a command-line utility that allows you to configure how individual websites and any other domains contained in that website are handled.
Features <a name="features"></a>
-
Enhanced security - Runs as an unprivileged user instead of root. New: Includes systemd service files that heavily sandbox the background process.
-
System-wide blocking - All non-proxied connections use the HOSTS file (Proxied connections can be modified to use the HOSTS file)
-
Compression-friendly - Can download and process zip- and 7zip-compressed files automatically. (Provided that
unzipandp7zipare installed) -
Non-interactive - Can be run as a periodic background job without needing user interaction.
-
Extensive configurability - Allows for custom deny & allow listing, redirection, ~~post-processing scripting~~ (now provided via systemd configuration), etc.
-
Bandwith-efficient - Only downloads blocklists that have been changed, using http compression when available.
-
Resource-efficient - Only processes blocklists when changes are registered.
-
High performance blocking - Only when using dns caching.
-
Redirection capability - Enchances security by combating DNS cache poisoning.
-
Extensive choice of blocklists included - Allowing the user to choose how much or how little is blocked/redirected.
Installation <a name="installation"></a>
Dependencies <a name="depends"></a>
- curl
- A POSIX environment (which should already be in place on most Linux, *BSD, and macOS environments, including the following commands:
sh(e.g. bash or dash,chmod,cksum,cp,cut,file,find,grep,id,mkdir,mv,rm,sed,sort,tee,touch,tr,wc, andxargs.
Optional dependencies for additional features
- sudo to enable the user-friendly wrapper script (highly recommended)
Unarchivers to use archive blocklists instead of plain text:
- [unzip][unzip] (for zip archives)
- [p7zip][7zip] (for 7z archives) must include either
7z,7za, or7zrexecutables!
A DNS caching daemon to help speed up DNS resolutions:
If you use 127.0.0.1 as your blocking redirect address (redirecturl in hostsblock.conf), a pseudo-server that serves blank pages to remove boilerplate page and speed up page resolution on blocked domains:
Note that the default configuration gets no benefit from having a pseudo-server
Arch Linux <a name="archinstall"></a>
If you have yaourt installed: yaourt -S hostsblock or yaourt -S hostsblock-git
Or use one of the AUR packages: hostsblock, hostsblock-git
Don't forget to enable and start the systemd timer by running this:
$ sudo systemctl enable --now hostsblock.timer
For Other POSIX Flavors and Distros <a name="posixinstall"></a>
The Best and Easiest Way
Please check with your distribution to see if a package is available. If there is not, ask for it or contribute your own!
If you are a package maintainer, let me know so that I can post the instructions here.
The Easy Way
First download the archive here or with curl like so: curl -O "https://github.com/gaenserich/hostsblock/archive/master.zip"
Unzip the archive, e.g. unzip hostsblock-master.zip
Execute the install.sh script as root, which will guide you through installation.
Configuration <a name="config"></a>
By default, the configuration files are included in the /var/lib/hostsblock/config.examples/ directory. Copy them over to /var/lib/hostsblock/ to customize your setup.
Editing hostsblock.conf <a name="hostblockconf"></a>
Most of the hostsblock configuration is done in the [hostsblock.conf][conf]. This file is commented really well, so please read through it before first use:
# CACHE DIRECTORY. Directory where blocklists will be downloaded and stored.
#cachedir="$HOME/cache" # DEFAULT
# WORK DIRECTORY. Temporary directory where interim files will be unzipped and
# # processed. This directory will be deleted after hostsblock completes.
#
# #tmpdir="/tmp/hostsblock" # DEFAULT
# FINAL HOSTSFILE. Final hosts file that combines together all downloaded blocklists.
#hostsfile="$HOME/hosts.block" # DEFAULT
# REDIRECT URL. IP address to which blocked hosts will be redirect, either 0.0.0.0 or
# 127.0.0.1. This replaces any entries to 0.0.0.0 and 127.0.0.1. If you run a
# pixelserver such as pixelserv or kwakd, it is advisable to use 127.0.0.1.
#redirecturl="0.0.0.0" # DEFAULT
# HEAD FILE. File containing hosts file entries which you want at the beginning
# of the resultant hosts file, e.g. for loopback devices and IPv6 entries. Use
# your original /etc/hosts file here if you are writing your final blocklist to
# /etc/hosts so as to preserve your loopback devices. Give hostshead="0" to
# disable this feature. For those targeting /etc/hosts, it is advisable to copy
# their old /etc/hosts file to this file so as to preserve existing entries.
#hostshead="0" # DEFAULT
# DENYLISTED SUBDOMAINS. File containing specific subdomains to denylist which
# may not be in the downloaded denylists. Be sure to provide not just the
# domain, e.g. "google.com", but also the specific subdomain a la
# "adwords.google.com" without quotations.
#denylist="$HOME/deny.list" # DEFAULT
# ALLOWLIST. File containing the specific subdomains to allow through that may
# be blocked by the downloaded blocklists. In this file, put a space in front of
# a string in order to let through that specific site (without quotations), e.g.
# " www.example.com" will unblock "http://www.example.com" but not
# "http://subdomain.example.com". Leave no space in front of the entry to
# unblock all subdomains that contain that string, e.g. ".dropbox.com" will let
# through "www.dropbox.com", "dl.www.dropbox.com", "foo.dropbox.com",
# "bar.dropbox.com", etc.
#allowlist="$HOME/allow.list"
# CONNECT_TIMEOUT. Parameter passed to curl. Determines how long to try to
# connect to each blocklist url before giving up.
#connect_timeout=60 # DEFAULT
# RETRY. Parameter passed to curl. Number of times to retry connecting to
# each blocklist url before giving up.
#retry=0 # DEFAULT
# MAX SIMULTANEOUS DOWNLOADS. Hostsblock can check and download files in parallel.
# By default, it will attempt to check and download four files at a time.
#max_simultaneous_downloads=4 # DEFAULT
# BLOCKLISTS FILE. File containing URLs of blocklists to be downloaded,
# each on a separate line. Downloaded files may be either
# plaintext, zip, or 7z files. Hostsblock will automatically
# identify the file type.
#blocklists="$HOME/block.urls"
# REDIRECTLISTS FILE. File containing URLs of redirectlists to be downloaded,
# each on a separate line. Downloaded files may be either
# plaintext, zip, or 7z files. Hostsblock will automatically
# identify the file type.
#redirectlists="" # DEFAULT, otherwise "$HOME/redirect.urls"
# If you have any additional lists, please post a bug report to
# https://github.com/gaenserich/hostsblock/issues
Enable the systemd service <a name="enabletimer"></a>
Don't forget to enable and start the systemd timer with:
$ sudo systemctl enable --now hostsblock.timer
Configure Postprocessing <a name="enablepostprocess"></a>
Hostsblock does not write to /etc/hosts or manipulate any DNS caching daemons anymore. Instead, it will just compile a hosts-formatted file to /var/lib/hostsblock/hosts.block. To make this file actually do work, you have one of two options:
OPTION 1: Using a DNS Caching Daemon (Here: dnsmasq)
Using a DNS caching daemon like dnsmasq offers better performance.
To use hostsblock together with dnsmasq, configure dnsmasq as DNS caching daemon. Please refer to your distribution's manual. For ArchLinux read the following: Wiki section.
After that, add the following line to dnsmasq.conf (usually under /etc/dnsmasq.conf) so that dnsmasq will reference the file:
addn-hosts=/var/lib
