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:

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 remount,rw /system # allow to write
vi /system/etc/hosts ## edit the file in place - do what you whant, then <ESC>:wq ##
mount -o remount,ro /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!
  • 6
    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
  • 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

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

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 Mar 27 '16 at 13:21

protected by Community Nov 15 '15 at 16:56

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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