an ad- and malware-blocking cronscript for Linux
To participate in the ongoing discussion on hostsblock
, check out its Arch Linux forum or its Arch User Repository page.
Sadi Yumuşak has been so kind as to create a debian package of this util, available in one of the downloads above (zip or tar).
In order to centralize resources, I'm migrating all of hostsblock
's web presence to github.com.
Hostsblock
is a bash script for Linux designed to take advantage of the HOSTS file available in all operating systems to provide system-wide blocking of internet advertisements, malicious domains, trackers, and other undesirable content. To do so, it downloads a configurable set of blocklists and processes and their entries into a singular HOSTS file.
Hostsblock
also includes hostsblock-urlcheck
, a command-line utility that allows you to block and unblock certain websites and any other domains contained in that website, in the event that the included blocklists don't block enough or block too little on a specific site.
Hostsblock
only requires a few utilities that are standard on most Linux distros, including GNU bash
, curl
, GNU grep
, GNU sed
, and GNU coreutils
.
Hostsblock
allows for additional features if the following utilities are installed and in the path:
Unarchivers, so that archive blocklists can be used instead of just plaintext, e.g.:
A DNS caching daemon to help speed up DNS resolutions, such as:
A pseudo-server that serves blank pages in order to remove the boilerplate page and speed up page resolution on blocked domains. Examples include:
On an Arch Linux system, you can install hostsblock through AUR utilities such as yaourt
. If you are not running an Arch Linux system (which you should), follow these steps:
Make sure you have all of the required dependencies above, and that they are in your path.
Download the latest stable source file from the AUR or get the unstable version
Extract the source files from the archive, and then enter the extracted hostsblock directory in a terminal.
Execute the following as root or with sudo
:
install -Dm755 hostsblock.sh /usr/sbin/hostsblock
install -Dm755 hostsblock-urlcheck.sh /usr/sbin/hostsblock-urlcheck
install -Dm644 rc.conf /etc/hostsblock/rc.conf
install -Dm644 black.list /etc/hostsblock/black.list
install -Dm644 white.list /etc/hostsblock/white.list
install -Dm644 hosts.head /etc/hostsblock/hosts.head
If you will be using hostsblock
without a dns caching daemon, make sure to copy your existing /etc/hosts
file to /etc/hostsblock/hosts.head
and enable this destination file in rc.conf
.
If you want to use hostsblock-urlcheck
as a regular user (without having to change to root each time you use it), enter the following via visudo
as root:
[yourusername] ALL=NOPASSWD: /usr/sbin/hostsblock-urlcheck
Where [yourusername]
is the regular user you allow to use hostsblock-urlcheck
.
Once enabled, you can use this utility by entering:
$ sudo hostsblock-urlcheck "http://www.example.org"
to check if "http://www.example.org" and any domains present on this page are blocked or unblocked and to change them accordingly.
Extensive documentation is found in the rc.conf
file (installed by default under /etc/hostsblock/rc.conf
). Follow the directions therein before running hostsblock
. For more details on specific configurations, follow these instructions:
hostsblock
with a dns caching server, e.g. dnsmasq:Install dnsmasq
(e.g. via pacman -S dnsmasq
).
Add the following to /etc/dnsmasq.conf
:
addn-hosts=/etc/hosts.block
Add the following to /etc/hostsblock/rc.conf
:
postprocess(){
systemctl restart dnsmasq.service
}
Where "systemctl restart dnsmasq.service" is your system's command to restart dnsmasq
.
From now on, whenever hostsblock
runs, or if hostsblock-urlcheck
makes changes, dnsmasq
will automatically be restarted.
hostsblock
without a dns caching server:Make sure you back up your existing /etc/hosts
file to /etc/hostsblock/hosts.head
:
$ cp /etc/hosts /etc/hostsblock/hosts.head
In /etc/hostsblock/rc.conf
, make sure the following variables are entered as shown here:
hostsfile="/etc/hosts"
postprocess(){
/bin/true
}
hostshead="/etc/hostsblock/hosts.head"
Drop me any suggestions in the Arch Linux Forum that you might have, or feel free to fork via github. Other ideas: