Enable “Systemless hosts” from within the Settings screen in Magisk.
This creates and then overrides the default hosts
file. You can then add your entries to /data/adb/modules/hosts/system/etc/hosts
.
However you can’t easily edit the file on Android itself via Termux or ADB because when your su
you don’t seem to be able to install Vim or similar, so I create a local version of a hosts
file to use and then upload it in a roundabout way to avoid lack of permissions issues.
# Push file from local machine to phone:
$ adb push <local-machine-path-to-hosts> /sdcard/Download/
# Move (overwrite) the existing systemless hosts file:
$ adb shell
$ su
# cp /sdcard/Download/hosts /data/adb/modules/hosts/system/etc/
You then need to restart the phone (manually or below) for it to take effect.
# adb reboot