Context
Contents
I am using LineageOS 14.1 which has Android 7.1.2 as base. My phone is not rooted, because there is no reason for me to have root access. As i wanted to keep it that way, I used TWRP to change the „host“-file. Unfortunately this broke the host file lookup. So I am documenting here a correct way to do it.
Preparations
First of all we want to use ADB (Android Debugging Bridge). This way you can have root shell access to Your phone from Your PC.
You need to download the Android SDK from https://developer.android.com/studio/#downloads. Unzip it somewhere you like.
Then connect your phone with you PC using USB. Go to your settings, scroll down to development settings. Activate Root access for ADB and USB-debugging.
Now open a command-line on your PC and navigate to the Folder you extracted the Android SDK.
adb root
adb shell
Making the Modification
The „hosts“-file is located under /system/etc/hosts
. You can not start right away. /system
is mounted as read-only partition. To modify it you need to make it writable. You use the following mount
command for that.
mount /system -o remount,rw
Now you can write some stuff to the „hosts“-file. I chose to use one from someone who cares http://someonewhocares.org/. Obviously this only works if your phone has a active internet connection.
wget http://someonewhocares.org/hosts/zero/hosts >/system/etc/hosts
After you ‚re done, make sure to mount the system partition read-only again.
mount /system -o remount,ro
Now you should test it by running using the ping
command with one of the hosts you just added.
If it works, you can disable USB-debugging and ADB root access. You should reboot your phone to check if this works too.
Problems I encountered
Back to the problems I mentioned at the beginning of this Article. When I first tried to modify my „hosts“-file, I got stuck by ping
and every other application ignoring the file.
This was because of SELinux detecting the modification and blocking access to the modified file. I think I changed the inode somehow. You should check the kernel messages with dmesg |grep host
command. To see if the SELinux is complaining.
I was able to fix this problem with restorecon
command. It seams to be part of SELinux and is able to restore security contexts.
restorecon /system/etc/hosts
Here is some Documentation about the restorecon
command. https://fedoraproject.org/wiki/SELinux/restorecon
I could not reproduce the error I got, when I was copying the a new hosts file using the recovery. Maybe you are always fine when you overwrite the content of the file with a command like this.
cat newhostsfile >/system/etc/hosts