Monday, January 2, 2017

How to Edit Hosts File on Mobile Device

Much like our windows system, there are many situations when we need to modify the hosts file on our mobile devices. Reason being we need to control the access to our websites using hosts file. Modifying hosts file on the device is quite easy if the device is rooted but it is not easily done when the device is not rooted. Let's take a look at both of these scenarios:

Case 1: When the device is rooted


This is the simplest use case where we can pull the hosts file of the Android device onto our system and then after modifying it we can push it back to the device. Following ADB commands can be used to achieve this:

  • adb devices # to see if the device is connected and responding properly. 

         Now pull the hosts file from your device to your system using the following command:

  • adb pull /system/etc/hosts d:\test\hosts

"test" in the above command is a target folder on your windows machine where the hosts file would be downloaded. Open the saved host file and edit it in any text editor. Save the file. Now the final step would be to upload it back to the device.

  • adb push d:\test\hosts /system/etc/  # d:\test is the location where the updated hosts file resides.

That's it. You are done, your hosts file has been updated successfully.


Case 2: When the device is not rooted


Changing the hosts file is not possible when the device is not rooted. So how to get away with this situation? The solution is to route your mobile device's traffic through a system that has the desired hosts file. Here are all the required steps:

Prerequisites: Fiddler or Charles must be installed on your system. These tools would help in setting up proxy on the system.

Steps:

  • Set up the proxy on your system. Follow the steps mentioned here to configure proxy using Charles. Steps remain the same if Fiddler is used instead of Charles.
  • Modify the host file on your system as per your need. Save the file.
  • All set, the proxy has been set and the host file has been modified.
  • Open the required URL in your mobile device and it will obey the rules defined in the host file of your system.

1 comment: