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.
> 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
:
sh#!/bin/sh # see https://github.com/StevenBlack/hosts/ for alternatives url="https://raw.githubusercontent.com/StevenBlack/hosts/master/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 [ -f hosts.editable ] || exit 1 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
It seems that a full reboot is required for the new hosts file to take effect for the sailfish-browser.
Android App Support
It also works for Android Apps, explained here:
> 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
As of 4.5., a new method needs to be used:
devel-su
Patch /usr/libexec/appsupport/prepare-aliendalvik.sh
. After …"$CONTAINER_CONFIG_PATH/10-bsp_config"
put
echo “lxc.mount.entry = /etc/hosts system/etc/hosts none bind,create=file 0 0” >> “$CONTAINER_CONFIG_PATH/10-bsp_config”
Verify:
appsupport-attach cat /etc/hosts
Here the same as a .patch file (not tested):
diff--- /usr/libexec/appsupport/prepare-aliendalvik.org.sh 2023-01-25 20:52:12.000000000 +0000 +++ /usr/libexec/appsupport/prepare-aliendalvik.sh 2023-02-11 23:47:29.737036812 +0000 @@ -121,6 +121,7 @@ /usr/libexec/appsupport/alien-generate-rootfs-config.sh "$CONTAINER_CONFIG_PATH/40-rootfs_config" /usr/libexec/appsupport/appsupport-generate-config --type lxc --user-id "$ALIEN_USER_ID" --instance "$INSTANCE_NAME" --base-uid "$ALIEN_BASE_UID" "$CONTAINER_CONFIG_PATH/10-bsp_config" || exit 32 +echo "lxc.mount.entry = /etc/hosts system/etc/hosts none bind,create=file 0 0" >> "$CONTAINER_CONFIG_PATH/10-bsp_config" /usr/libexec/appsupport/alien-generate-net-config.sh $PRIVILEGED "$CONTAINER_CONFIG_PATH/30-net_config"