hostsctl allows you to block advertisements, trackers, and other malicious activity by manipulating
/etc/hosts. By taking advantage of curated lists of known bad hosts and providing an interface to easily manipulate host definitions, you can save bandwidth and stay safer online.
How it Works
hostsctl gains flexibility by storing host definitions in different files in the
/etc/hostsctl directory. When hostsctl is first run, it will create this directory and initiate the needed files. If
/etc/hosts exists, it will be copied to
/etc/hostsctl/orig.hosts, ensuring that no existing definitions are lost.
A file containing the list of hosts to be blocked, defined in the configuration, is downloaded and stored at
/etc/hostsctl/remote.hosts. Hosts that are explicitly blocked or unblocked via hostctl are stored at
/etc/hostsctl/enabled.hosts, respectively. hostsctl operates by merging these separate files together into
Note that after using hostsctl, the
/etc/hosts file should not be edited directly. Any manual changes will be lost on the next run. Instead, edit the appropriate file in the
Installation can be performed by executing the provided
install.sh script as root, or by copying
bin/hostctl.sh to your path.
$ sudo ./install.sh
- Archlinux hostsctl package.
Bash completions are automatically installed by the
install.sh script on Arch Linux systems. On other systems, copy the provided
hostsctl.bash-completion file to the appropriate directory.
hostsctl is configured via
/etc/hostsctl/hostsctl.conf. An example configuration is included, and automatically installed by the
This variable defines the source of the blocking hosts file. See below for example URLs.
# Block adware and malware via StevenBlack's host file remote_hosts='https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts'
This variable defines the IP address that blocked hosts should point at. The default value of
0.0.0.0 is recommended. For a discussion concerning this vs
127.0.0.1 see StevenBlack's host project.
# Route blocked hosts to 0.0.0.0 ip='0.0.0.0'
The simplest usage is to run
hostsctl update. This will download the latest version of the remote hosts file, merge it together with other entries in
/etc/hostsctl and output the results to
$ sudo hostsctl update
Enabling and Disabling Hosts
hostsctl supports enabling hosts that would otherwise be blocked by the remote hosts file. Note that when a host is accessible, it is considered to be enabled.
$ sudo hostsctl enable facebook.com # facebook.com will now resolve correctly
If an unwanted host is not already blocked by the specified remote hosts file, hostctl can also disable it for you.
$ sudo hostsctl disable facebook.com # facebook.com now resolves to 0.0.0.0
To see which hosts are disabled, or to see a list of explicitly enabled hosts, run the appropriate command.
$ hostsctl list-enabled $ hostsctl list-disabled
hostsctl is also able to update the remote hosts file and combine the various host definitions in
/etc/hostsctl as separate steps. This may be useful if you wish to view what has changed in the latest version of your remote hosts file before applying those changes.
# Update the remote hosts file $ sudo hostsctl fetch-updates # Export the all entries to stdout and compare them against the current /etc/hosts $ hostsctl export | diff -y --suppress-common-lines /etc/hosts - | less # After confirming the changes, save them to /etc/hosts $ sudo hostsctl merge
Similarly, this behaviour allows the host definitions to easily be stored elsewhere.
$ sudo hostsctl fetch-updates && hostsctl export > ~/myhosts
Example Hosts Files
First try to
restore the /etc/hosts file.
$ sudo hostsctl restore
If the problem persist please open new issue on Github.
The original version of this script is available in the original branch.
Any collaboration is welcome!