- a PC that is connected to the Internet and running Ubuntu, where you have root access and a free USB port
- a rooted Android device (in my case the Motorola Droid 3) with a SIM and an active mobile subscription
- a USB cable (I have the one that came with the phone).
#1. Disconnect your Android device's data connection from your mobile carrier by touching the corresponding icon in the notifications tray (2 parallel vertical arrows pointing in opposite directions) or by switching your phone to Airplane Mode.
#2. Launch the Terminal emulator app on your Android device and enter this command:
You should get a response that shows only one network interface, named "lo" or "lo0" or something like that, with an information field that says "inet addr:127.0.0.1". This is the local loopback connection. We will not be using it - just know that it's always there in responses to "ifconfig" and ignore it from now on.
#3. Re-enable your Android device's data connection by reversing what you did at #1. Go back to the Terminal and do another
This time, in addition to the "lo" interface, you should see another interface that is used to communicate with the Internet through your mobile carrier. For me this interface is called "ppp0"; you may have something else, but just remember what it is because you will have to use it later wherever you see me use ppp0 in the commands below.
#4. Now connect your Android device to your Ubuntu PC using the USB cable. Access the USB connection notification in your Android notifications tray and select "PC Mode" as your USB Mode. (If you don't have this option, then I don't know what to say, you will probably have to try them all, but in that case I wouldn't start with the "Mass storage" option - that's the least likely one to work for this.)
#5. Still on the Android, open Settings -> Wireless & networks -> Tethering & Mobile Hotspot and enable "USB tethering". You should get a second, green USB icon on your status bar after tethering is enabled.
#6. Go back to the Terminal app on your Android device and do yet another
This time you should receive information for 3 network interfaces: in addition to "lo" and "ppp0" you should now see a new interface that corresponds to your USB. For me it's called "gether0"; remember what yours is and use it wherever I use gether0 in the commands below.
Also, look at the "inet addr:" specification of your gether0 interface and remember this IP address because we will need it later. (My USB tether interface always gets the same IP address - 192.168.42.129 - so this is what I will be using in my example commands below.)
#7. Now let's look at the PC's network interfaces. Open a terminal emulator (I use Ctrl-Alt-T to do this; depending on what launchers, shortcuts or desktop environments you have, you may need to find it in a menu or do something else to get to it) and type
This response depends a lot on how your PC is set up, but generally I'd expect to see at least a "lo" (local loopback) interface, a "usb0" interface, one or more "eth0", "eth1" etc. interfaces and maybe a "ppp0" interface. The "ppp0" and "eth0" type interfaces will be for your PC's Internet connection and the "usb0" interface will correspond to the USB cable connecting you to the Android device. If you have a "ppp0", that's probably the one you should use in all setup commands to be run on the PC where I will use ppp0. Otherwise if you only have "lo", "usb0" and "eth0", your PC's Internet is probably on "eth0", so use that one in place of my ppp0 in commands run on the PC. (If you have any other combinations without a "ppp0", I don't know what to suggest except try them all one by one, everything you have besides "lo" and "usb0".)
#8. Back to the Android device. What we want here is for the ppp0 interface to remain enabled - because that's the only way the app store will agree to download any apps - but all our Internet communication to actually go through the gether0 interface, i.e. through the USB tether. To do this, we need to change the default route Android apps use to send data, namely to delete the default route that points to ppp0 and add a new default route that points to gether0 and that uses as a gateway the IP address of the Ubuntu PC (an address we will set up on the PC at the end of this process). So run these commands in the Terminal on the Android:
route del default
route add default gw 192.168.42.1 dev gether0
setprop net.dns1 184.108.40.206
Notice that the gateway IP address we will be using is made up of the first 3 numbers from our USB tether interface's IP from step #6, followed by a ".1" instead of whatever the 4th group was in the original IP.
#9. On the PC we want to set up standard Internet connection sharing between usb0 and ppp0 according to the instructions from the Ubuntu manual, so we will run these commands in the terminal window:
sudo su -
(enter your password here to get root access and then do 5 more lines)
iptables -A FORWARD -o ppp0 -i usb0 -s 192.168.42.0/24 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
Here again you can see we're using the first 3 numbers from the USB tether interface's IP, but adding a ".0" and a "/24" at the end to specify an entire class of IPs we're going to accept packets from over the USB, a class that includes our Android's actual IP, 192.168.42.129. (We could probably just use that single IP without a "/24", but whatever, this class stuff is usually the way it's done.)
#10. Finally, we will set our PC's usb0 interface IP to the gateway IP we already told the Android to send everything to, i.e. 192.168.42.1, after which we need to quickly check that the connection is working and jump right into Google Play to download some apps. I say "quickly" because in my case, for whatever reason, after I set the gateway IP on the PC I can only leave the connection unused for about 1 minute before it drops on its own. So don't wait too much after #10.1 to do the rest:
#10.1. Enter this into your PC's terminal:
ip addr add 192.168.42.1/24 dev usb0
#10.2. Check that the connection is up by entering this into your Android's terminal first:
ping -c 3 google.com
If you get 3 responses from Google, you're all set to launch Google Play and download some apps. If there's no response, go back to #10.1 and try again.
Another indication that the PC has dropped the connection is that you get a notification popup on the PC that says "Wired network/ Disconnected". That tells you you need to jump back to #10.1 to get the USB link working again.
Also, you will know you need to do this again if you find Google Play is suddenly refusing to load apps or pages even though it was loading them before - you probably waited too long between clicks and allowed the connection to drop.
#11. To reset all the connections to normal when you're done, make sure to
#11.A. Go to Settings -> Wireless & networks -> Tethering & Mobile Hotspot and disable "USB tethering" before you unplug the USB cable from either device, otherwise the gether0 interface may remain active and interfere with your ability to get back your data link to your mobile carrier.
#11.B. Do steps #1 and #3 again to get your Android to automatically re-establish the proper settings for the link to your mobile carrier.
- In order to streamline this process, especially steps #8-#10, you should probably put these commands and your specific interface names and IP addresses in some scripts - one on the PC, one on the Android device - that you can then launch more easily. I'm a newbie at both Ubuntu and Android, so I have to do some more searching to figure out how to do this properly.
- Keeping the connection up even if you're idle - e.g. while reading app descriptions and deciding what to download - is probably a matter of sending some dummy packets periodically through the USB, which would probably involve another script running in the background - again something I haven't taken the time to figure out how to set up yet.
- Anytime you get paranoid about whether the apps are being downloaded through the USB cable or your mobile data plan, go to your Android terminal and check whether your default network route is still pointing to your gateway PC by running:
This will show you all the routes that are configured on your Android. The last line in the table should say "default", then "192.168.42.1" and end with "gether0". This means the data is still going through the USB and not eating up your mobile data credit.