13

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

Community
  • 1
  • 1
Navakanth
  • 834
  • 1
  • 12
  • 24
  • Exactly the same problem, unfrortunately, can't upvote twice! – Nick Roz Jan 28 '16 at 09:46
  • Does it work when using 192.168.x.x from emulator? In my case - yes, but dns is still unresolved – Nick Roz Feb 01 '16 at 09:14
  • My friend faced a similar issue, the problem was in hosts file because of windows notepad. Instead of editing hosts file in notepad, we used 'echo' command in adb shell to add host entry into the hosts file. – Hardik Mar 22 '17 at 01:48

5 Answers5

11

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
hai
  • 111
  • 2
6

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

Raghav
  • 8,772
  • 6
  • 82
  • 106
2

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.

Nick Roz
  • 3,918
  • 2
  • 36
  • 57
1

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.

Aurangzeb
  • 1,537
  • 13
  • 9
  • 'ipconfig /all' is the command (on cmd) that would help you find the ip address of your virtual adaptor (I think that's the command, though I don't use windows much), also you can find ip of emulated device in the device settings, that should help you find the subnet – Aurangzeb Feb 04 '16 at 04:59
  • In that very case there was no problems with IP addresses – Nick Roz Feb 04 '16 at 08:13
0

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

Zaha
  • 846
  • 10
  • 21