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


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

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


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


#!/bin/sh # see for alternatives url="" cd /etc || exit 1 curl --retry 20 --retry-all-errors --retry-connrefused --retry-delay 5 "$url" \ | grep '^0\.0\.0\.0' > || exit 1 sed -i '/0\.0\.0\.0 0\.0\.0\.0/d' [ -f hosts.editable ] || exit 1 cat hosts.editable > hosts.tmp && mv hosts.tmp hosts && rm

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:   localhost.localdomain     localhost
    ::1         localhost6.localdomain6   localhost6


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:


Patch /usr/libexec/appsupport/ 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”


appsupport-attach cat /etc/hosts

Here the same as a .patch file (not tested):

--- /usr/libexec/appsupport/ 2023-01-25 20:52:12.000000000 +0000 +++ /usr/libexec/appsupport/ 2023-02-11 23:47:29.737036812 +0000 @@ -121,6 +121,7 @@ /usr/libexec/appsupport/ "$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/ $PRIVILEGED "$CONTAINER_CONFIG_PATH/30-net_config"