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.
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/disabled.hosts and /etc/hostsctl/enabled.hosts, respectively. hostsctl operates by merging these separate files together into /etc/hosts.
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 /etc/hostsctl directory.
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 install.sh script.
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 /etc/hosts.
$ sudo hostsctl updatehostsctl 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 correctlyIf 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.0To see which hosts are disabled, or to see a list of explicitly enabled hosts, run the appropriate command.
$ hostsctl list-enabled
$ hostsctl list-disabledhostsctl 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 mergeSimilarly, this behaviour allows the host definitions to easily be stored elsewhere.
$ sudo hostsctl fetch-updates && hostsctl export > ~/myhosts| file | by |
|---|---|
| hosts | StevenBlack/hosts |
| hosts | AdAway |
| hosts | add.2o7Net |
| hosts | add.Spam |
| hosts | SpotifyAds |
| hosts | UncheckyAds |
First try to restore the /etc/hosts file.
$ sudo hostsctl restoreIf 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!
- Steven Black hosts repo for inspiring me to create this.
- Ty-Lucas Kelley github-templates
