[Q] Why Android ignores /system/etc/hosts file?

oktam

New member
Mar 9, 2009
4
2
0
Hi,
i edited /system/etc/hosts file but it looks that my edit is ignored by Android.

In "hosts" file i added line with text "1.1.1.1 customdomain"

And then i used "ping customdomain" command in Terminal Emulator but i got error "unknown host am".
 

Vlad_z

Senior Member
Mar 25, 2011
506
178
0
That's how some AdBlock apps on market are working.

They just download huge (700K) host file and most of ads are history. (also in ads based applications)

PS: try something with your local host 127.0.0.1 first... you have to reboot your device after modification.
 
Last edited:

oktam

New member
Mar 9, 2009
4
2
0
I doesn't ignore the hosts file, did you edit it with r/w permissions?
AFAIK, i did. I edited it using "FS File Explorer" with r/w turned on... Then i restarted phone and my change is still there.

That's how some AdBlock apps on market are working.
They just download huge (700K) host file and most of ads are history. (also in ads based applications)
PS: try something with your local host 127.0.0.1 first... you have to reboot your device after modification.
Yes, i found posts about these ad blocks when i read all these forums about this...

I can ping "localhost", but i cant ping this host i added there...

EDIT:
I added record "127.0.0.1 bbb", then i restarted phone and i tried to run "ping bbb", but i got error msg about unresolved hostname aswell...

I have Nexus S and latest CyanogenMod 7.0.3 if it can help...
 
Last edited:

oktam

New member
Mar 9, 2009
4
2
0
I got it!

I was playing etc... And i found that i need to put 1 empty line at end of file to make system to reload file!
No need to restart phone at all, i can ping new hostname immediatelly.

Now i can ping new hostname. But when i open it in Android browser, i get new exciting error msg:
Sorry The requested server does not have a DNS entry!

Any ideas now?
 

cfrockit

Member
Jun 6, 2011
31
17
0
mathdotrandom: Block ads on PC & Android with an uber HOSTS file!

AFAIK, i did. I edited it using "FS File Explorer" with r/w turned on... Then i restarted phone and my change is still there.


Yes, i found posts about these ad blocks when i read all these forums about this...

I can ping "localhost", but i cant ping this host i added there...

EDIT:
I added record "127.0.0.1 bbb", then i restarted phone and i tried to run "ping bbb", but i got error msg about unresolved hostname aswell...

I have Nexus S and latest CyanogenMod 7.0.3 if it can help...
This approach and file "Block ads on PC & Android with an uber HOSTS file!" has work flawlessly on multiple device all with various firmwares and versions of Android.
 
  • Like
Reactions: furga

azjakec

New member
Nov 29, 2011
1
1
0
Hi, I've included some domains I want blocked in my hosts file so as to redirect them to "127.0.0.1" I've made sure the permissions are set correctly. I've made sure there is an extra linebreak at the end of the file. I've rebooted. In the terminal a PING request to the domain returns from the localhost. And yet I can still access the domain through the web browser.

What gives?

PS: I have a Nexus One 2.3.6 retail that I rooted using the DoomLord zerg rush exploit without unlocking the bootloader

And I have cleared the browser cache
 
Last edited:
  • Like
Reactions: gcbxda

shizat63

Senior Member
Aug 28, 2010
78
22
28
This issue hasn't been fixed yet. Chrome still ignores the HOSTS file in \system\etc

Apparently the HOSTS file must be in UNIX file format. One way to do this is use Notepad++ and convert the file by going to EDIT --> EOL Conversion --> Unix.

If you ping a domain in the hosts file and it goes to 127.0.0.1 or 0.0.0.0 then your hosts file is formatted correctly.

But Chrome will ignore the domains in the hosts even if it's formatted correctly. Perhaps Google intentionally does this to prevent anyone from blocking their tracking tools?

I would bet there are millions of Android devices that have a hosts file but it's not working as everyone thinks it should be.

EDIT - Solution found: turn off "Reduce data usage" in Chrome
 
Last edited:

pwmosquito

New member
Apr 28, 2014
1
0
0
chrome local domain fail

EDIT - Solution found: turn off "Reduce data usage" in Chrome
Unfortunately it does not work for me on my GT-I9305 (I9305XXUEML5).

For the record here is what I did:

Added domain.local to /system/etc/hosts:
Code:
[email protected]:/ # mount -o rw,remount -t ext4 /dev/block/mmcblk0p13 /system
[email protected]:/ # echo "127.0.0.1 domain.local" >> /system/etc/hosts
[email protected]:/ # mount -o ro,remount -t ext4 /dev/block/mmcblk0p13 /system
[email protected]:/ # grep domain.local /system/etc/hosts
127.0.0.1 domain.local
I can now ping domain.local:
Code:
[email protected]:/ # ping domain.local
PING domain.local (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.220 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.186 ms
64 bytes from 127.0.0.1: seq=2 ttl=64 time=0.242 ms
^C
--- domain.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.186/0.216/0.242 ms
Then I set up port forwarding in chrome: chrome://inspect/#devices -> Port forwarding... -> 8080 x.x.x.x:8080

After this I can telnet into domain.local from my GT-I9305:
Code:
[email protected]:/ # telnet domain.local 8080
GET / HTTP/1.0


HTTP/1.0 200 OK
Date: Mon, 28 Apr 2014 11:47:42 GMT
Server: openresty
X-Powered-By: PHP/5.4.12
Connection: close
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>
<html>
...
</html>
Connection closed by foreign host
So far so good.

Unfortunately if I open chrome on my mobile and enter domain.local:8080/ I get "Oops! Google Chrome could not connect to domain.local"
 

schnitz76

Member
Jan 18, 2015
6
0
0
The extra line was what I needed to fix my problem with my hosts file. As soon as I saved and refreshed it went to the site I needed it to.

I got it!

I was playing etc... And i found that i need to put 1 empty line at end of file to make system to reload file!
No need to restart phone at all, i can ping new hostname immediatelly.

Now i can ping new hostname. But when i open it in Android browser, i get new exciting error msg:
Sorry The requested server does not have a DNS entry!

Any ideas now?
 

K.v

New member
Nov 20, 2015
1
0
0
Hosts file is working when i use directly from chrome and default browser, but when i use google app and then it is showing to choose chrome or internet browser it is not blocking site in hosts file after my chrome selection.
Plz.....i need help....
 

airowe

New member
Aug 2, 2017
1
0
0
Was anyone able to get Android to read your hosts file? I can't even ping the site properly using the terminal emulator.

I've turned off Chrome data saver, made sure the file was saved with Unix line endings and left a blank line at the end of the /system/etc/hosts file.
 

cadbren

New member
Sep 15, 2017
1
0
0
I did. I copied the hosts file to my computer, edited it then pushed it back to android with rw permission.
It didn't work though until I added the blank line below my last entry (why?), just hit enter after adding your last/only domain name entry and then save.