Block domains using hosts file entries.
pip3 install hostblock
pip3 install -U git+https://github.com/nul-one/hostblock.git
Run hostblock apply
to add list of undesired hosts to your /etc/hosts
file resolving to 0.0.0.0
. It will ask for sudo password.
Hostblock will automatically create a json file ~/.hostblock
where it keeps your local blacklist and whitelist. When using hostblock apply
your local whitelist will be subtracted from your blacklist and the result will be then put into your /etc/hosts
.
Here is how you can control your local list:
hostblock ab HOST1 HOST2 ...
this will add single or multiple hosts to your blacklisthostblock rb HOST1 HOST2 ...
remove single or multiple hosts from your blacklisthostblock cb
remove (clear) all hosts from your blacklisthostblock lb
list all hosts currently in your blacklisthostblock aw HOST1 HOST2 ...
this will add single or multiple hosts to your whitelisthostblock rw HOST1 HOST2 ...
remove single or multiple hosts from your whitelisthostblock cw
remove (clear) all hosts from your whitelisthostblock lw
list all hosts currently in your whitelisthostblock list
list hosts from blacklist that do not appear in whitelisthostblock count
show counts for blacklist and whitelist
Commands that don't change config files such as lw
, lb
, list
and count
can be followed by a list of configuration files. In that case default config file or --config
option will be ignored and unified data will be displayed. Config files will not be touched in either case:
hostblock count first.config second.config third.config
The apply
option can be followed by list of configs in similar manner. If so, unified config data will be applied to /etc/hosts
or other selected hostname:
hostblock apply .hostblock first.config second.config third.config
In example above, we listed the default .hostblock
config file. If we didn't, it wouldn't be used.
To import a list of hosts, just pass it as argument to ab
or aw
command.
Here is an example bash script that will import lists from http://someonewhocares.org
and https://github.com/notracking/hosts-blocklists
lists into blacklist of multiple config files and then apply those including your default one to your hosts file:
#!/bin/bash
set -e
echo "Updating 'notracking/hosts'..."
curl -s https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt \
| fdump -p '^address=/(.*)/.*$' '{0}' \
| xargs hostblock -c ~/.hostblock.notracking ab
echo "Updating 'notracking/domains'..."
curl -s https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt \
| fdump -p '^address=/(.*)/.*$' '{0}' \
| xargs hostblock -c ~/.hostblock.notracking ab
echo "Updating 'someonewhocares/hosts'..."
curl -s http://someonewhocares.org/hosts/zero/hosts \
| fdump -d "130.211.230.53" -p '^0\.0\.0\.0 ([a-zA-Z0-9\._-]+)\s.*' '{0}' \
| xargs hostblock -c .hostblock.someonewhocares ab
hostblock count ~/.hostblock*
echo "Applying to /etc/hosts..."
hostblock apply ~/.hostblock*
The above example uses fdump tool for easy filtering. You may install the tool with pip3 install fdump
or use grep/awk alternatives in it's place.
I use the above script as a daily cron-job. I only add new things manually into my default config file to keep it separate.
To merge your friend's whitelist and blacklist, see the following example:
hostblock --config friends.hostblock lb | xargs hostblock ab
hostblock --config friends.hostblock lw | xargs hostblock aw
- Do not use sudo when running hostblock commands. Hostblock will ask for sudo password when required to modify hosts file. If you use
sudo
then hostblock will think you are root user and will look for settings file in/root/.hostblock
thinking that's your home dir. If you really need to use sudo, make sure to specify proper hostblock config with--config
option. - When adding duplicate domains to your blacklist or whitelist, hostblock will keep only single entry, so no worries!