Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android hosts file usage

I have the following problem:

I make a custom hosts file to test some features of my application and then push it to my android emulator. The thing is that these settings do not take effect immediately. I have to wait about 10 minutes before they become active.

So my question is: how to make the new hosts file active instantly? I have many different settings to test and I can't wait 10 minutes every time.

like image 277
dominos Avatar asked Nov 13 '10 22:11

dominos


2 Answers

Java maintains its own internal DNS cache. The operating system will reflect the new hosts file immediately (verify that with ping on the command line) but you'll need to tell java not to cache anything. Add these lines to your test application:

System.setProperty("networkaddress.cache.ttl" , "0"); System.setProperty("networkaddress.cache.negative.ttl" , "0");

For more information on these properties, see here: http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html

like image 175
Jason LeBrun Avatar answered Sep 26 '22 19:09

Jason LeBrun


I just edited my hosts file on my rooted Samsung Galaxy S, and the changes took effect immediately. Perhaps the problem you're seeing is something to do with ADB?

I did this:

  1. Using Busybox, copy the hosts file to an editable location with cp /etc/hosts /mnt/sdcard/hosts.new;
  2. Edit /mnt/sdcard/hosts.new using the pre-installed text editor, adding the two entries I need. I used IP, then unqualified hostname, then FQDN, eg 192.168.2.81 siva siva.myinventeddomain.org.au, but other formats should in theory work too;
  3. In BusyBox again, su to root;
  4. /system is ro by default, so I had to make it rw with mount -o remount,rw /system;
  5. To save typing later, cd /etc (whereupon the shell prompt showed /system/etc rather than /etc, which makes me suspect symlink shenanigans);
  6. Back up the default hosts file (which contained only 127.0.0.1 localhost) with mv hosts hosts.old;
  7. Install new hosts file with mv /mnt/sdcard/hosts.new hosts;
  8. Execute sync (merely because I am paranoid - this shouldn't be necessary);
  9. Remount /system fs ro with mount -o remount,ro /system;
  10. Exit BusyBox;
  11. Fired up web browser (FireFox) and entered siva in the combined URL/search field thingy (siva being one of the two hosts entries I added).

Prior to these changes, step 11 resulted in a stupid Google search for 'siva' or something; immediately after them, I get my LAN httpd vhost's front page, as I expect.

There was well under 10 minutes elapsed between it working and it not working.

The link to Sun's Java doco may or may not be relevant (probably it isn't). Android doesn't contain a Java VM at all, let alone Sun's one. It runs a different VM called Dalvik - see Wikipedia entry: http://en.wikipedia.org/wiki/Dalvik_%28software%29

The fact that you can program Android phones in a language that looks a lot like Java is beside the point.

like image 22
pseudomatty Avatar answered Sep 24 '22 19:09

pseudomatty