How to edit 'etc/hosts' file? 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:


How to change the &#39;hosts&#39; file to redirect URLs? -> Link in the answer is broken
Guide to editing /etc/hosts -> Not found a way to edit hosts file.
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 &lt;ESC&gt;: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!