Navakanth
Navakanth

Reputation: 864

Url mappings in hosts file in emulator is being ignored

I am using genymotion emulator and pulled hosts file from it using

adb remount

adb pull /system/etc/hosts C:\Users\Rkn09\Desktop\hosts

and I added new mapping like 192.168.0.115 xxx.mydomain.com and I pushed it back to emulator using

adb push C:\Users\Rkn09\Desktop\hosts /system/etc/hosts

but when I make a request to xxx.mydomain.com its not redirecting to my local machine in LAN 192.168.0.115. I even checked the hosts file using cat /etc/hosts and it has my mapping, can anyone help me with this issue.

PS: Same process is woking fine in Mac OS X EI Captain, I am getting this problem in windows7

Upvotes: 13

Views: 4878

Answers (5)

Raghav
Raghav

Reputation: 9630

It was CR LF characters for me at the end of host file. The hosts file you are copying to Android Emulator should be having Unix Line Feed.

You can set and detect that via NotePadd ++.

Open the host file in NotePadd ++ and select "Edit >> EOL COnversion >> Unix (LF)"

enter image description here

You con confirm the line endings by going to "View >> Show Symbol >> Show End of Line"

enter image description here

That will show the LF characters

enter image description here

To verify that the host file is working you can go to adb shell and ping the domain.

enter image description here

Upvotes: 8

Zaha
Zaha

Reputation: 946

Same problem for me, In my case the problem was my format file was in widnwos format, so I've used dos2unix.exe tool to change format

enter image description here

I think notepad ++ can also change the format to unix

Upvotes: 0

Aurangzeb
Aurangzeb

Reputation: 1626

Apparently none of the above solutions helped you, that's what I expected. I don't know how to give you a step by step solution, but I hope my answer would help you.

Emulator (usually) resides in a completely different subnet.

Emulation systems create a virtual network adaptor to connect emulator to the host machine, so you need to give address of your 'virtual network adaptor' to the 'hosts file' instead of ip address of 'physical network adaptor' of the 'host machine'.

Here is how it looks on the network

Computer                               Emulator
192.168.0.115
mask: 255.255.255.0

192.168.6.1<-------------------------->192.168.6.2
mask: 255.255.255.0

So for emulator, 192.168.0.115 doesn't exist

so in hosts file, either give an address 192.168.6.1 (or whatever your virtual network adaptor address is) while your web-browser is listening on every possible local address (i.e. 0.0.0.0:80) or change the subnet mask accordingly. I think the former would be a good solution.

Upvotes: 1

Nick Roz
Nick Roz

Reputation: 4230

hosts file should have been written using linux line ending style.

It's impossible to achieve this in Notepad as well as in Wordpad. Try installing another editor which supports this feature and ensure you saved hosts file properly.

(I tried AkelPad, but Nodepad++ offers the same feature as well)

Presumably, no matter what the encoding is. Both Windows-1252 (ANSI) and utf-8 worked.

Having done all the changes push hosts file to device:

adb remount
adb push hosts /system/etc/hosts

Ensure you have correct hosts file and it is loaded and whether it works or not:

adb shell cat /system/etc/hosts
adb shell ping my.domain.com

Open browser from Genymotion device and ensure everything works by typing:

http://my.domain.com

You may add port if needed

http://my.domain.com:1234

You do not need rebooting Android device.


In case problem had not gone make sure it is related to hosts file itself by pinging ip address directly:

adb shell ping 192.168.x.x

If it works, then check line endings in file once more. Some text editors may mix them up and an option to use Unix line ending is quite often related to new files only.

Upvotes: 2

hai
hai

Reputation: 121

I had the same problem. The following resolved it:

  • Make sure you have a blank line after the last entry of the hosts file
  • If you use tabs in the hosts file, replace them with spaces

Restart Android and try again:

adb reboot

Upvotes: 12

Related Questions