I'm using LineageOS 14.1 and don't want nor need to root it. How can I remount /system
rw and edit /system/etc/hosts
?
All answers in "How to edit 'etc/hosts' file?" expect a rooted phone.
I'm using LineageOS 14.1 and don't want nor need to root it. How can I remount /system
rw and edit /system/etc/hosts
?
All answers in "How to edit 'etc/hosts' file?" expect a rooted phone.
Unless I missed something, that is not possible. Without root access only the mechanism of flashing an update (via recovery or OTA) can modify /system
. To be accepted, such an update archive must be signed with the same key as the ROM – which is something only the developer of the ROM (here: the LOS team) can do.
There might be a way via a custom recovery which lets you edit all file systems directly (see e.g. twrp). But without knowing what device it is, it cannot be said if a matching recovery exists (you can check that in the Devices section of their website for official images, and search XDA for unofficial ones). Apart from that, it's either rooting the device or not editing any file within /system
.
twrp
looks like a very good solution. It has everything needed for small manual changes or full hosts
replacements. I'm running the latest stable which ships with BusyBox: vi
, cat
, chmod
and chown
are there.
fastboot boot twrp.img
). Which leaves unlocking the bootloader as the only device modification (otside the one you just want to perform, of course ;)
vi /system/etc/hosts
3. make your changes (5. ESC, :wq
to save and exit vi)
On non-rooted phone you cannot actually edit hosts
file but it's possible to use VPN apps like Virtual Hosts or Personal DNS Filter (both are open-source, I've no affiliation with either) which intercept DNS traffic and look up a custom hosts
file before making queries to configured upstream DNS server.
Or if bootloader is unlockable, editing hosts
file is possible through custom recovery.
For more alternatives see: How to always resolve a domain name to a fixed IP without rooting?
It is possible to use a VPN profile* to affect hostname resolution. There are a number of apps now available on the Play Store that provide a convenient way to do this, such as Hosts Go. Note that, since this technique requires the use of a VPN profile, you won't be able to use an actual VPN in conjunction with this technique.
*As of Android 4.0 (API Level 14) — https://developer.android.com/guide/topics/connectivity/vpn
mycomputer.lan
), perhaps set the DNS server within Hosts Gos to your router's IP address.
Commented
Jan 4, 2022 at 21:24
I've seen some comments saying TWRP isn't explained clearly. This way you will only need developer options enabled.
So first get the file in your computer
adb pull /system/etc/hosts hosts
Edit the pulled hosts and push it again to the phone into a folder you have write permissions
adb push hosts /sdcard
Reboot the phone into recovery mode (TWRP), then go to mount and check "system" (DON'T check the "Mount system partition read-only"), go back and advanced -> File manager. Now you have a file manager with the system partition writable. Rename /system/etc/hosts to something like hosts_old, then move you edited hosts file /sdcard/hosts to /system/etc .
You can use a proxy and use your computer's /etc/host file.
On macOS I use Squidman, but you can easily find other alternatives for other OS.
If someone goes for squidman, all you have to do is add the following line to the Template section of it's configuration:
#hosts file
hosts_file /private/etc/hosts
Once squidman is running, everything you have to do is configure your mobile device to use a Manual proxy entry pointing to the IP address of the computer running squidman in the same network.
Then you are ready to go.
And if you're running squidman and wants to access localhost, remember to comment out the following line in squidman config:
#http_access deny to_localhost
Warning: this will work only if ADB can get root access, which isn't the case for most ROMs. It works in in LineageOS 14.1.
Developer options
enabled in phoneadb
installed in computerApply the following settings in Developer options
:
Root access
-> ADB only
Android debugging
-> onConnect the phone to the computer. Grant permission in your phone if asked. In the computer, launch a terminal and run
$ adb root
Grant permission in your phone. Remount with
$ adb remount
Start a remote rooted shell
$ adb shell
In the adb shell, edit your hosts file
# nano /etc/hosts
When ready, disconnect
# exit
nano
? cat
is all that you've got. Android does not even ship with a basic vi
.
nano
is in /system/xbin/nano
. vim--version
(in /system/xbin/
too, no vi
alias) says it was "Compiled by tpruvot@CyanogenMod".