Author Topic: Editing hosts file  (Read 1628 times)

gidds

  • Sr. Member
  • ****
  • Posts: 324
    • View Profile
Editing hosts file
« on: January 12, 2023, 02:27:16 pm »
Has anyone successfully changed their /etc/hosts file on the Astro (with rooted Android)?

My Gemini has a large hosts file giving the addresses of all the devices on my LAN, and blacklisting a huge range of ad/tracking/malware sites*. On my new Astro, I've got most of the way to doing the same:
  • sudo /system/bin/mount -o rw,remount /’ to make the root filesystem writable.
  • sudo vi /etc/hosts
  • Edit as appropriate.
  • Save the result. ⬅︎ Fails with ‘No space left on device’ error!
df’ reports that the / filesystem is indeed almost full: 1.4G used, 4.3M free. But that should be enough to save a short (few KB) hosts file, shouldn't it? (I can rename the file, and vi creates a .swp file, so I know the filesystem is writable.)

Is there any way to enlarge that filesystem a bit? (Preferably, without wiping everything… I can find several web pages claiming to do that on the emulator, but not on a physical Android device.) Or is there something else going on?


(* pointing them to 0.0.0.0. That's not a full substitute for a decent ad-blocker in your browser, of course, but it adds another layer of defence that applies to almost all apps.)

(⁑ When done, remember to repeat that command, but with ‘ro’ instead of ‘rw’, to set it back to read-only. — On the Gemini, the last ‘/’ was ‘/system’ in both commands, but that's not mounted separately on the Astro.)
   Andy/
Psion 3a → Psion 5 → Psion 5mx → Gemini → Astro

gidds

  • Sr. Member
  • ****
  • Posts: 324
    • View Profile
Re: Editing hosts file
« Reply #1 on: January 17, 2023, 09:44:27 am »
To answer my own question:

This is easily fixed by enabling the Magisk built-in module ‘Systemless Hosts’.

Magisk was already installed on my phone. (I had patched the boot image with Magisk, as per the procedure here, and the first time I tried to run su it triggered a full install of the Magisk app.) I started the Magisk app, opened the Modules tab, and the ‘Systemless Hosts’ module was already listed; I just had to enable it and reboot.

After doing that, /etc/hosts is fully writable!  :)



(There seems to be a lot going on under the covers… First, /etc is a symbolic link to /system, so /etc/hosts really means /system/etc/hosts. Second, it looks like Magisk mounts a huge number of separate filesystems — e.g. one for every executable in /system/bin, and loads of others I don't understand. Enabling the ‘Systemless Hosts’ module causes one more filesystem to be mounted, on /system/etc/hosts itself. And that filesystem is writeable — no need to remount it each time — and seems to have plenty of space.)

One caveat: only /etc/hosts is writable; no other files in that directory are writable (because they're on a different filesystem). This means that if you use e.g. vi to edit the file, it will give an error because it can't create a swap file in that directory. The workaround for that is simply to tell it not to use a swap file, with vi -n.



(Another idea I had was to make (/system)/etc/hosts a symbolic link to a file that I could write — but I couldn't get that to work. I eventually found a location to write a hosts file that could be read by all users, and symlinked to it, but it had no effect: the system just didn't use it for looking up hostnames (as confirmed by commands such as ping). I'm not sure why. I suspect that the hosts file must be a regular file and not a symlink; I've seen that claim on platforms such as macOS and Docker, though not on Android.)



Anyone interested in controlling hostname lookup should also be aware of AdAway, which is an app that takes care of mapping host names for you.

It can work by setting up a hosts file (if you have root), or by setting up a local VPN (which works even if you don't have root); it can run a local webserver (if you redirect to 127.0.0.1); and it has a huge list of adservers etc. that it updates regularly.

I tried it, and it seems to work well. (In fact, it was through researching an error it initially gave me that I discovered the Systemless Hosts module.)

(I probably won't be keeping it, though, as I have a fair number of personal entries to add for to devices on my LAN or at work, and although AdAway can do that, it's quite fiddly. Also, I already have a hosts file on my desktop machines, so it's easy just to copy that across whenever I update it.)
   Andy/
Psion 3a → Psion 5 → Psion 5mx → Gemini → Astro