Hostsblock

an ad- and malware-blocking cronscript for Linux

News

To participate in the ongoing discussion on hostsblock, check out its Arch Linux forum or its Arch User Repository page.

Version 0.12.3 (03.06.2013)
Version 0.12.2 (02.06.2013)
Version 0.12.1 (31.05.2013)
Version 0.12 (31.05.2013)
Version 0.11.3 (31.05.2013)
Debian Package Now Available! (19.02.2013)

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).

Version 0.11.2 (09.12.2012)
Version 0.11.1 (13.09.2012)
New Webpage (30.08.2012)

In order to centralize resources, I'm migrating all of hostsblock's web presence to github.com.

Description

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.

Features

Screenshots

Beginning output of hostsblock. Click to enlarge. End output of hostsblock. Click to enlarge. Output of hostsblock-urlcheck. Click to enlarge.

Dependencies

Hostsblock only requires a few utilities that are standard on most Linux distros, including GNU bash, curl, GNU grep, GNU sed, and GNU coreutils.

Optional dependencies

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:

Installation

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:

  1. Make sure you have all of the required dependencies above, and that they are in your path.

  2. Download the latest stable source file from the AUR or get the unstable version

  3. Extract the source files from the archive, and then enter the extracted hostsblock directory in a terminal.

  4. 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
    
  5. 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.

  6. 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.

  7. 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.

Configuration

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:

If you want to run hostsblock with a dns caching server, e.g. dnsmasq:

  1. Install dnsmasq (e.g. via pacman -S dnsmasq).

  2. Configure dnsmasq as a local caching server.

  3. Add the following to /etc/dnsmasq.conf:

    addn-hosts=/etc/hosts.block
    
  4. 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.

If you want to run hostsblock without a dns caching server:

  1. Make sure you back up your existing /etc/hosts file to /etc/hostsblock/hosts.head:

    $ cp /etc/hosts /etc/hostsblock/hosts.head
    
  2. 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"
    

Todo

Drop me any suggestions in the Arch Linux Forum that you might have, or feel free to fork via github. Other ideas: