Ad blocking solution using the /etc/hosts file. Weekly auto-updated from MVPS .
This is a proven way of blocking ads, banners, 3rd party cookies, 3rd party page counters etc. by adding blocking rules to the /etc/hosts system file.
There is already another similar app - noADShosts - while my app is inspired by it, it shares no code and has some advantages.
Features:
- The hosts file from MVPS is first sanitized (for only comments and rerouting to 0.0.0.0), which should minimize security risks even if the site itself is compromised.
- The adblock lists gets updated weekly (using a systemd cron job). You can adjust this period by editing /etc/systemd/system/update_hosts_adblock.timer , but these changes may be overturned if this package gets updated.
- You can manually update the list whenever you want to by running update_hosts_adblock as root (devel-su).
- Your own rules are preserved and you can still edit them and add new ones by editing /etc/hosts.editable file. Mind that for your changes to come to effect, you need to re-run update_hosts_adblock .
- There should be no risks in deleting this package when you want to stop using it. It just recreates your /etc/hosts file using the /etc/hosts.editable (backup) file during uninstallation.
- If you ever (accidentaly or on purpose) delete your /etc/hosts file, run update_hosts_adblock command as root and the file will be recreated with your default Jolla config. Therefore should not be worried about replacing other packages dealing with /etc/hosts.
- If there is no internet connection on the next weekly update, your hosts file will simply stay as it is and wait for the next week.
- Starting from version 0.3, it also supports the Android /system/etc/hosts file. From version 0.4 it also supports the /opt/alien/system/etc/hosts file. /system/etc/hosts.editable and /opt/alien/system/etc/hosts.editable allow custom rules for these hosts files.
Notes:
- It takes 0.5-1.5MB in the filesystem.
- Every (weekly or manual) update fetches approximately 500KB, so be careful if you don't connect through WiFi - it may reduce your FUP limit on the mobile internet.
Even if I haven't run into any problems, there might be some, so use with caution (you have been warned).
Please tell me if you experience any problems.
If you like this solution, please consider a small donation:
Donate in EUR
Donate in USD
Comments
nick8325
Tue, 2020/03/17 - 12:02
Permalink
The package is currently not working - it does not modify the hosts file. This is because update_hosts_adblock checks that that the downloaded MVPS hosts file is at least 12000 lines long, but it is currently a little shorter. To fix it, edit the file /usr/bin/update_hosts_adblock and change the line:
to e.g.:
P.S. thanks for the super-useful package!
claustn
Sun, 2019/12/29 - 12:15
Permalink
It would be nice to have the possibility to temporarily disable the blocked hosts. Otherwise certain websites does not work at all.
zwieberl
Wed, 2019/02/06 - 15:40
Permalink
No sure if this is still maintained or not, but I get windows-lineendings in the new hosts-file, which makes it not work.
After doing
to it, it works.
EDIT: Probably even easier is to switch to
REMOTE_HOSTS="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
which contains MVPS plus a lot of other lists
olf
Sun, 2016/11/06 - 08:35
Permalink
Thank you for this proper ad-blocker for SailfishOS, as this was very much needed. And the (new) feature of patching the Android hosts file makes using AdAway (from F-Droid) superfluous.
But obviously Hosts Adblock 0.4.1 blocks fewer advertising servers than AdAway, so you may enhance Hosts Adblock by consulting more extensive hosts list(s).
AdAway uses per default:
# This file is generated from the following sources:
# http://winhelp2002.mvps.org/hosts.txt
# https://adaway.org/hosts.txt
# https://hosts-file.net/ad_servers.txt
# https://pgl.yoyo.org/adservers/serverlist.phphostformat=hosts&showintro=0&mimetype=plaintext
From these sources AdAway creates /opt/alien/system/etc/hosts (example uploaded to pastebin.com, truncated from 2 MB to 53 KB by cut&paste-ing), merging these lists while removing duplicate entries.
So using one of Steven Black's compiled host name lists (as recommended below) is far less hassle to implement (also thanks to your tidy /usr/bin/update_hosts_adblock shell script), they are collections of the hosts lists AdAway uses and a little more, work very well (also for Android in-app ads etc.) and look reasonable and trustworthy.
As AdAway is unable to patch /etc/hosts and /system/etc/hosts running as root under AlienDalvik, your Hosts Adblock for SailfishOS comes very handy. :)
olf
Sun, 2016/11/06 - 21:11
Permalink
So I did:
[nemo@Sailfish ~]$ diff /usr/bin/update_hosts_adblock.orig /usr/bin/update_hosts_adblock
7,8c7,8
<
< REMOTE_HOSTS="http://winhelp2002.mvps.org/hosts.txt"
---
> # Selecting the "unified" hosts blocking list from <https://github.com/StevenBlack/hosts> (picking the basic list, as the extended ones tend to block too eagerly):
> REMOTE_HOSTS="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
79c79
< echo "# Autogenerated by the sailfishos-hosts-adblock app from mvps.org" >> $1
---
> echo "# Autogenerated by the sailfishos-hosts-adblock app with the \"unified\" hosts blocking list from <https://github.com/StevenBlack/hosts>" >> $1
Working nicely. :)
See update_hosts_adblock.patched@pastebin for the full file.
incognito
Fri, 2016/10/07 - 04:47
Permalink
Thanks nodevel, quite an useful script.
A suggestion for future updates - MVPS hosts file is not enough, it won't weed out quite some number of Android in-app ads and a lot of more local-oriented ads in general. I suggest using StevenBlack's Unified hosts file (includes MVPS, adaway and couple of other lists) instead. It is frequently updated and readily available from: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts .
The best part? Since it includes the MVPS list the 'check_mvps' function will pass so no changes are needed, apart from replacing the value of REMOTE_HOSTS to point to the aforementioned list (and in the interim anybody can do it by editing the /usr/bin/update_hosts_adblock script ).
(Not sure if my last comment went through, repeating it just in case)
Brooks
Fri, 2016/08/26 - 11:30
Permalink
So far, great app, works perfect on jolla c.
Many thanks
delilat
Wed, 2016/03/16 - 00:14
Permalink
I agree, a Whitelist would be awesome.
objectifnul
Sun, 2016/03/13 - 23:24
Permalink
Instructions in /etc/hosts.editable seem to be ignored. BTW I don't know if I'm supposed to use «127.0.0.1 banned.site» or «0.0.0.0 banned.site» (I've tried both without success)
If I append hosts.editable instructions to hosts it's ok (until next automatic update of course).
----
EDIT: My bad, I didn't check carefully enough. What actually happens is this: instructions in /etc/hosts.editable are incorporated into /etc/hosts every time an automatic or forced update is performed. That's just perfect.
nodevel
Mon, 2016/03/14 - 13:36
Permalink
Thanks for noticing, I should have made it clearer in the description. The info is now added.
giskard
Fri, 2016/01/22 - 13:16
Permalink
How can I do to whitelist an host? If I comment its row in /etc/hosts, it will be overwritten by the next update.
nodevel
Sun, 2016/03/13 - 01:21
Permalink
Hi,
sorry, I did not notice your comment.
You currently cannot whitelist a host. I'll try looking into it for a next update, but no promises. Thanks for the idea, though.
MooCrumpus
Wed, 2015/12/23 - 09:43
Permalink
How to uninstall it?
nodevel
Wed, 2015/12/23 - 13:20
Permalink
Like any other app, nothing special :)
Either find the package in Warehouse and uninstall, or uninstall it via terminal (zypper/pkcon).
objectifnul
Sat, 2015/12/05 - 11:41
Permalink
Nice. "editing /systemd/system/update_hosts_adblock.timer" should be "editing /etc/systemd/system/update_hosts_adblock.timer"
Note: conflicts with noADShosts. If already installed, remove it first, according to BloodyFoxy's instructions
nodevel
Sat, 2015/12/05 - 14:33
Permalink
Thanks, typo corrected.
Regarding the conflict with noADShosts, starting from version 0.3, it should be even more robust in recognizing either missing or already fetched (from MVPS) /etc/hosts file and recreate it from scratch. So you should be fine with simply removing the noADShosts package.
I even hope the package management would resolve the conflicts/dependencies automatically, but it looks like you need to remove the conflicting package manually, if installing through Warehouse.
olf
Sat, 2016/11/05 - 21:42
Permalink
You should be able to trigger uninstalling No Ads Hosts by explicitly stating that Hosts Adblock conflicts with the noadshosts RPM in your Spec file (other RPMs from Warehouse are successfully doing that).
eson
Sat, 2015/12/05 - 08:38
Permalink
Thanks a lot! I've been doing this manually for "years", and updating is so easy to forget.