dt.iki.fi

AdBlocking on Sailfish OS via /etc/hosts

Table of Contents

There are some apps in Openrepos which do not seem to work anymore for SFOS 4.x. But you don't really need an app, all you need is a systemd timer & service and a simplistic shell script.

Gain command line access to your phone and become root.

console
> cd /etc/systemd/system > nano hosts.timer hosts.service > systemctl daemon-reload > systemctl enable hosts.timer

hosts.timer:

ini
[Unit] Description=Update /etc/hosts [Timer] WakeSystem=false OnCalendar=Sat *-*-* 16:00:00 RandomizedDelaySec=1h AccuracySec=1h Persistent=true [Install] WantedBy=timers.target

So once a week on Saturday at roughly 16:00. Adjust to your liking.
See man systemd.timer for further options.

hosts.service:

ini
[Unit] Description=Update /etc/hosts [Service] # Un/comment as required: #ExecStart=/home/defaultuser/.local/bin/hosts ExecStart=/home/nemo/.local/bin/hosts

~/.local/bin/hosts:

bash
#!/bin/bash # see https://github.com/StevenBlack/hosts/ for alternatives url="https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts" cd /etc || exit 1 curl --retry 20 --retry-all-errors --retry-connrefused --retry-delay 5 "$url" | grep '^0\.0\.0\.0' > hosts.new || exit 1 sed -i '/0\.0\.0\.0 0\.0\.0\.0/d' hosts.new cat hosts.editable hosts.new > hosts.tmp && mv hosts.tmp hosts && rm hosts.new

The script removes some useless fluff and then combines this data with hosts.editable into the new hosts file.
/etc/hosts is only overwritten when all this succeeds.

Make sure that

  • ~/.local/bin/hosts is marked executable: chmod +x ~/.local/bin/hosts
  • The file /etc/hosts.editable exists and contains the default contents of /etc/hosts (before any manipulation). Here it is:
      127.0.0.1       localhost.localdomain localhost
      ::1     localhost6.localdomain6 localhost6

Caveat-up-

It seems that a full reboot is required for the new hosts file to take effect for the sailfish-browser.

Android App Support-up-

It also works for Android Apps, explained here:

console
> mv /system/etc/hosts /system/etc/hosts.old > echo 'lxc.mount.entry = /etc/hosts system/etc/hosts none bind,create=file 0 0' >> /var/lib/lxc/aliendalvik/extra_config