Writing

Configure hosts File in Android

Alright, today I would like to work a sample web site and browse it not only from my Android Emulator but also from my Android G1 device. Since I did not make the web site public yet, I need to play with "hosts" file in order to direct specific url to my specific intranet IP on Android browser.

The steps that I follow to accomplish my goal are provided below;

For Android Emulator;
1. From the terminal invoke the adb command with remount option otherwise you will get "failed to copy 'hosts' to '/system/etc/hosts': Read-only file system".

adb remount

2. In the terminal pull the default hosts file from /system/etc/hosts or /etc/hosts which is the symlink of the file not only for backup purpose but also to get the file to modify.

adb pull /system/etc/hosts /home/player/hostBackup/

3. Get a copy of the the hosts file in your computer and modify it according to your needs.

4. Now we can push the file directly to the emulator.

adb push /home/player/hostBackup/hosts /system/etc/

5. That's it, if you wish you can check it out from you shell.

adb -e shell
# cat /system/etc/hosts


For Android G1 physical device;
1. In the device scenario, you need to start root accessed adb.

adb root

2. After gaining the root access successfully, just follow the steps 1-5 in for emulator section.

This procedure is working on both Windows and Linux with Android SDK 1.5_r2, just change the file paths for Windows version and it's all done.

Happy coding...

Comments

Anonymous · 15 April 2011

I did this and verified the hosts file is updated on my N1 - however, it doesn't seem to use the IP addresses of the hosts I have added to this file. Is there some trick to flush the DNS cache or how to get the new host IP addresses to resolve?

jackace · 3 June 2010

I am not sure if solution is required anymore, but the trick is use -partition-size 128 (or more) while launching emulator( you can also pass this parameter in eclipse as default parameter) and follow the above mentioned steps.

sacoskun · 20 March 2010

Hello Rahul, If you modified the correct hosts file, the browser should follow the directions in the hosts file. Make sure you are running the same instance of the emulator which you changed the hosts file of. sarp

Chi · 20 May 2011

the modify will be invalid when reboot the emulator, can this be resolved?

Unknown · 20 March 2010

thnks for your information i done all the steps then seen the host file it was updated but from the android browser still i was not able to access virtual host which i created.????

Unknown · 3 May 2010

thanks for your information. i am using mac 10.6 and trying to follow mentioned instructions. i succesfully pulled the host file and edited according to need but while pushing back i am getting error "failed to copy '/Users/Mac2/Desktop/hosts2' to '/system/etc/hosts': Out of memory". how to resolve this issue??

Anonymous · 1 May 2010

Same as Tushneem... ugh. This is annoying. And the "Terminal Emulator" app seems pretty much useless to me.

sacoskun · 10 May 2010

Hello Rahul, I haven't tested this on Mac but it works on both Linux and Windows. sarp

Unknown · 3 February 2011

Hello.I am sorry, but you forgot to say that need to change the IP address 127.0.0.0 to 10.0.2.2 in the hosts file.

Anonymous · 9 August 2011

i done all the steps then seen the host file it was updated but from the android browser still i was not able to access virtual host which i created. I can check the host file entry by using Cat commant and it displays me updated host entry. Also i am using the same instance of android.

Anonymous · 18 May 2010

I tested it on KUbuntu and also have problem with "'/system/etc/hosts': Out of memory". Also tried to perform cat >> hosts after doing remount and device is full.