41

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.

berbt
  • 533
  • 1
  • 4
  • 7

6 Answers6

13

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. ). 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 the device or not editing any file within /system.

Izzy
  • 91,166
  • 73
  • 343
  • 943
  • Thanks. 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. – berbt May 09 '17 at 23:19
  • 1
    What is more, you don't have to install it – you can simply boot the image (fastboot boot twrp.img). Which leaves unlocking the bootloader as the only device modification (otside the one you just want to perform, of course ;) – Izzy May 10 '17 at 06:55
  • 1
    My steps to modify it using TWRP: 0. boot into recovery 1. mount system 2. Advanced> File manager > navigate to file and click button "chmod 755" 2. advanced > terminal > vi /system/etc/hosts 3. make your changes (5. ESC, :wq to save and exit vi) – karfau Aug 31 '20 at 14:00
  • @karfau your comment could be turned into an Answer – Henrique de Sousa Oct 28 '20 at 01:03
  • @karfau your TWRP steps from the Mount System to the Advanced FIle Manager are not clear - I mount the system but it doesn't go anywhere - returns me back to the same option list (I downloaded the twrp img for the device). It would be helpful if you expanded on your method some more here - Please, pretty please? – Martin Sansone - MiOEE Nov 16 '20 at 02:43
11

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

Jivan Pal
  • 212
  • 2
  • 6
  • 3
    The Hosts Go app does work to add a few hosts entries. The documentation could be a lot better though. You can tell the author is not a native English speaker. In short you need to: run the app, add extra hosts entries, turn on "host change switch" and then click the big start button. That starts host name resolution. You should be able to use those hosts in chrome now, for example. The stop button take you back to normal host name resultion – Gary Nov 20 '19 at 02:48
  • @Jack Not sure what you mean, I experience no such issues. – Jivan Pal Jan 04 '22 at 14:22
  • @Jack I've never had that issue, perhaps something peculiar about your network? If you're trying to resolve hostnames that are understood by your home router (e.g. mycomputer.lan), perhaps set the DNS server within Hosts Gos to your router's IP address. – Jivan Pal Jan 04 '22 at 21:24
  • @Jack Alternatively, if you are just saying "I cannot access 192.168.0.1 over a VPN, so why bother trying this? It obviously won't work for my use case", then realise that this is not an actual VPN; your home network is still accessible. To be sure, have you actually tried out this solution, or are you just writing it off immediately because of the word "VPN"? – Jivan Pal Jan 05 '22 at 17:31
  • This is what I do: (1) Open Hosts Go (2) Edit host entries say 192.168.0.230, rad.bad.zzz (3) Turn on the hosts go swtich (4) Make sure the DNS switch is off (5) Hit start button. (6) See VPN button in top status bar. Navigate in chrome to http://192.168.0.230:5000 . See something. Navigate in chrome to http://rad.bad.zzz:5000 . "This site can't be reached" – 0fnt Jan 13 '23 at 04:07
  • @0fnt Very odd; I still regularly use this app in the exact way you describe, on a device running Android 9, with no issues, in order to acces things behind my home NAT whilst that device is also behind that NAT. – Jivan Pal Jan 13 '23 at 14:44
  • @JivanPal Thanks for responding! I found this thing called personal DNS Filter and while it looks like the interface designers graduated from the same school as Hosts Go :P, the app works fine for me and has quite a bit more features. 5/5. – 0fnt Jan 14 '23 at 03:44
11

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?

Irfan Latif
  • 20,353
  • 3
  • 70
  • 213
  • It seems that Personal DNS Filter doesn't allow for setting custom name resolution: it can only block names. – L. Levrel Nov 17 '20 at 10:32
2

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 .

Sandstorm
  • 21
  • 2
  • If you are willing to modify system partition than you might as well just root the phone and save the tedious steps you mentioned. There is no benefit in this approach compared to directly editing the hosts file with root access. – Firelord Mar 26 '21 at 05:27
  • The root-ing app didn't work on my phone, and this approach worked. So it is usefull to anyone in my situation. And furthermore there was someone asking for details on how to do it with TWRP. – Sandstorm Mar 29 '21 at 08:36
0

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.

Squidman instructions

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

-2

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.

Requirements:

  • Developer options enabled in phone
  • adb installed in computer

Steps

Apply the following settings in Developer options:

  • Root access -> ADB only
  • Android debugging -> on

Connect 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
berbt
  • 533
  • 1
  • 4
  • 7
  • 3
    Huh? On LOS root is only available if you flash the additional root package, which OP doesn't want to (i.e. "Root access = ADB only" cannot be achieved). So how should your steps work without root if they require root? Could you please clarify? – Izzy May 09 '17 at 12:46
  • adbd cannot run as root in production build. – iBug May 09 '17 at 13:18
  • Also, how can you get nano? cat is all that you've got. Android does not even ship with a basic vi. – iBug May 09 '17 at 13:19
  • @Izzy just borrowed a phone with stock Android to confirm and yes, it seems to be a LOS thing. I didn't flash the root package. I'll modify my answer and the question title to reflect this fact. – berbt May 09 '17 at 13:43
  • @iBug same as my previous comment: most likely a LOS thing. nano is in /system/xbin/nano. vim--version (in /system/xbin/ too, no vi alias) says it was "Compiled by tpruvot@CyanogenMod". – berbt May 09 '17 at 13:55
  • I am running LOS 18.1 and I was able to edit the /etc/hosts file by using the above method - ie. enabling root access for developer mode and running adb remount command. Cannot understand why the post has been voted down. I agree it may not work on all ROMs but worked OK for me. – gdesilva Jun 12 '23 at 08:50