How to set the Android network interface using shell commands

android network configurationIn this post, I’d like to tell you how to set the Android network preferences on Android devices, that is, IP Address, Netmask, Gateway and DNS. This process can be easily done using the Android GUI, specifically in the Settings/Ethernet menu configuration. However, sometimes it is useful to know the shell commands in order to add them in boot scripts. The commands that you’ll find below, has been tested with the Android Ltouch7 multi-touch, but I’m confident that there settings should work also with other devices too.

The basic steps are the following:

1. Using the miniUSB-USB cable, connect it to the Android Ltouch7 and respectively to the PC (on Windows, make sure that you’ve previously installed the Google USB driver).

2. Open a terminal (or a command prompt on Windows) and type the “adb shell” command. If the cable and the Google USB driver are correctly set up, you will login into the Android device. In case the adb program gives an error, verify that Android device has the debugging preferences enabled.

3. The main commands that are necessary for setting the network are the following:

ifconfig eth0 [IP] netmask [NETMASK]
route add default gw [GATEWAY] dev eth0

where you have to substitute [IP] with the static IP Address you want, [NETMASK] with the network netmask and [GATEWAY] with your network’s gateway address. The last parameter of the route command, i.e., eth0, specifies the network interface to modify (that might vary)

4. The last step refers to setting the DNS, that is the addresses of the servers that convert web address like for instance www.biemmeitalia.net into the machine readable 62.149.142.97.

Dns settings are set in the global preferences, in particular: net.eth0.dns1, net.eth0.dns2. Sometimes, you can set them directly by using net.dns1 and net.dns2 properties.
Just type

setprop net.eth0.dns1 8.8.8.8
setprop net.eth0.dns2 4.4.4.4

for setting the Google DNS into the global preferences of your Android device. If you need to verify whether you’ve correctly set them, type the command

getprop

and you’ll get the whole list of global preferences together with their values.

5. Finally, check if the network is up and working. Use the following command in the shell terminal: netcfg. You might find, among other information, one line that tells you the current status of a specific Ethernet interface (eth0 in this example). UP means that is working. The last column might be different depending on your system

eth0 UP [IP ADDR] [NETMASK] 0x00001043

That’s it!

If you like this post, please share it 🙂