41

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:

45

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
    Does this work if the device has not been rooted? – itslittlejohn Oct 23 '15 at 20:35
  • 2
    neither su neither vi commands are not present in my case: S6 edge – albanx Nov 9 '15 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 Nov 9 '15 at 15:18
  • 1
    Old good echo saves me: echo "1.2.3.4 domain.tld" >> /system/etc/hosts – Oleg Neumyvakin Nov 7 '16 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 Nov 9 '16 at 13:12
16

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.

  • How you want to copy the file to that location on android? – Musa Haidari Mar 27 '16 at 13:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.