47

I'm using Nexus 5 on Android 4.4. Kitkat. I would like to edit /etc/hosts file and add redirect mappings in order to block few ads.

I need to know how to access/modify/replace /etc/hosts file.


I have already checked below similar Q/A:

1
  • 1
    If you're trying to do web development, you might not need to edit the /etc/hosts file, you can use remote debugging with Chrome.
    – Flimm
    Commented Jul 7, 2015 at 10:37

4 Answers 4

46

This is actually a compilation of answers and my own expirience.

So, get/install a copy of ADB, then type

adb devices -l # make sure your gadget is listed
adb shell # run a shell there
su # become the root (don't miss confirmation request!)
mount -o rw,remount /system # allow to write
vi /system/etc/hosts ## edit the file in place - do what you whant, then <ESC>:wq ##
mount -o ro,remount /system # get things back to normal
exit # unroot
nslookup YourBlockedAdSite.Net # check if it works
exit # good bye

You're done.

  • Things after # are comments and may be omited from your input. If you cut'n'paste them, it should be safe enough too.
  • It's supposed you have root access on your device (or the su wont work). Actually, you'll be unable to edit the file if you have no root anyway.
  • It's supposed you have a busybox installed (otherwise you may miss things like vi and nslookup). Well, you may still try to play with adb pull/adb push then.
  • It's quite common the adb remount fails to work and this is a workaround.

    • Note: When you run su in the shell, you may be asked on your gadget to confirm!
7
  • 9
    Does this work if the device has not been rooted? Commented Oct 23, 2015 at 20:35
  • 2
    neither su neither vi commands are not present in my case: S6 edge
    – albanx
    Commented Nov 9, 2015 at 9:50
  • 2
    Sure, the phone must be rooted first (any linux must be "rooted" to edit files in /etc, actually). I'd recommend SuperSU by Chainfire application and its busybox - it will give you both su and vi . The question "How To Root My Phone" is out of the scope here.
    – jno
    Commented Nov 9, 2015 at 15:18
  • 2
    Old good echo saves me: echo "1.2.3.4 domain.tld" >> /system/etc/hosts Commented Nov 7, 2016 at 15:35
  • echo is good only if you have to add a line. or to replace the whole file. in case of any more advanced edit, you'll have to use vi/sed/awk/you-name-it tool.
    – jno
    Commented Nov 9, 2016 at 13:12
17

Note : In-order to edit hosts file your device should be rooted

You can edit etc/hosts file using ES File Explorer or any File explorers with root access.

OR

Check this method

0

Root needed

Use FTPServer app to explore files via PC/Mac. Just copy out /system/etc/hosts edit it on your PC and copy and paste the hosts file to the same dictionary on your phone.

1
  • How you want to copy the file to that location on android? Commented Mar 27, 2016 at 13:21
-1

save the contents of hosts blacklist into file called "hosts" [no extension at end]

copy it to internal storage on the phone

then via cmd:

adb shell
su
cp /storage/emulated/0/hosts /system/etc/
exit

DONE, reboot phone

1
  • The system partition needs to be remounted in rw mode for your solution to work. That said, in Android versions supported by Magisk it is better and easy to use systemless hosts which avoids making changes to system partition.
    – Firelord
    Commented Oct 17, 2021 at 5:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .