wget https://raw.githubusercontent.com/scriptum/hostsblock/master/hostsblock.sh
sudo sh hostsblock.sh install
This is suitable only for Unix-like systems.
Alternative way to remove ads from websites. Of course you may use Add blocker plugin for your web browser but there are disadvantages:
- Ads blocking extensions consumes much CPU and RAM due to compilation of huge CSS table. Using
/etc/hosts
does not consumes your RAM because this is just plain text file. /etc/hosts
works system-wide including messengers, mail/rss clients.- Ads blocking extensions doesn't prevent loading ads content.
/etc/hosts
blocks entire domain and saves your traffic.
Unfortunately /etc/hosts
cannot block content by id, path or tag name like CSS filter does. I recommend combine this script with extensions that block cross-domain JavaScript. Examples: NoScript, uMatrix. After that you will block 99% of annoying advertisements.
sudo hostsblock help
This is a script designed to be as simple as possible that does following:
- Makes backup of your original
/etc/hosts
to/etc/hosts.bak
because original hosts will be modified. Don't worry, this script carefully handles all your manual hosts records. - Installs
/usr/bin/hostsblock
script to manage and update hosts file. - Creates updater script in
/etc/cron.weekly
that updates ads list and generate new/etc/hosts
. It also checks this github page and updates script automatically, if new version is available.
Most magic is done by glibc. If some application (web browser etc) wants to download web page it checks DNS record first using glibc call gethostbyname
. Before sending DNS request, glibc parses /etc/hosts
and tries to find given host name. If record exists in /etc/hosts
, it will overwrite DNS record of your ISP. If you add invalid IP address for some domain, e.g. like this:
0.0.0.0 an.yandex.ru
you will never get content from this domain. Glibc parses /etc/hosts
every time someone calls gethostbyname
, so it's important to keep /etc/hosts
as small as possible for faster lookups. It doesn't waste system memory because of reading information from file, but waste CPU time, so using huge /etc/hosts
list isn't good idea. Latest glibc versions parse hosts in optimized way.