How to use "/etc/hosts" file as only DNS on Android? On my Android-based device I need to block access to all websites except ones are in a whitelist. I know that on desktop Windows I can list allowed hosts in the hosts file and then switch the DNS in the network adapter from automatically obtained to 127.0.0.1. And now I can access only websites from the hosts file.

I've tried the same thing by adding allowed hosts in the system/etc/hosts file  on Android and then I've added the line

new_domain_name_servers="127.0.0.1 $new_domain_name_servers"


to the beggining of the file /etc/dhcpcd/dhcpcd-hooks/20-dns.conf as described by the link.

And then I've restarted my device.

But I still can access all websites, so the solution doesn't work. And even if I just add a website to be resolved as 127.0.0.1 in the hosts file and clear browser cache, the website is still opened.

PS: I'm interested only in a solution which could be done on rooted device only (Android 4.0.4)
Add allowed hosts to /etc/hosts and define these firewall rules to block any outgoing DNS queries on standard UDP port 53:

~# iptables -I OUTPUT -p udp --dport 53 -j REJECT


* May also use less rude DROP target in place of REJECT

To be on safe side also block TCP port:

~# iptables -I OUTPUT -p tcp --dport 53 -j REJECT


iptables is part of stock Android releases. You need to add these rules on every boot. Use init.d script or define init service: How to run an executable on boot?