Android – USB tethering with a Linux PC

I find it convenient to share the internet connection of my Android phone with my Linux laptop. I used to do it over WiFi but WiFi tethering is causing me no end of headaches. Not only was the WiFi connection dropping all the time, I’m now unable to use it at all. I’m not sure what’s going on but I think it stopped working after an Ubuntu software update.

Anyways, these WiFi issues led me to research USB tethering as an alternative. I figured out two USB tethering methods that work with my Linux PC. The first only works with a rooted phone while the second should work without root. I also explored using the phone as a WiFi adaptor for the PC and sharing the PC’s LAN connection with the phone.

So here’s what I could figure out. I hope this can be of use to somebody.

Tethering with a rooted phone

To share the internet connection of a rooted Android phone with a Linux PC:

And you’re done.

Note that the shell script above is similar to this one from mzet but uses the new “adb ppp” command instead of sending the ppp stream over a socket.

Tethering with an unrooted phone

I think I came up with a way of tethering an unrooted Android phones using Slirp. Slirp is an old program that let someone access the internet through a dialup Unix account. It implements PPP and NAT in user space and is designed to work without root access.

I prepared a build of Slirp for Android. It let me tether my phone which is already rooted. But I see no reason why it should require a rooted phone at all.

In order to use Slirp for tethering:

Let me know if that works for you.

Using the phone as a WiFi adaptor

Interestingly both these tethering methods work the same whether the phone is using WiFi or Mobile Data. As a result an Android phone can serve as a external WiFi adaptor of sorts for the PC.

Reverse tethering

I’m also trying to figure out if (in the absence of wireless connectivity) the phone can be connected to a PC to access the internet through a wired ethernet connection. This kind of reverse tethering could be useful if we need to tweet from our phones inside a nuclear bunker or a Faraday cage don’t you think? ;-)

So far I can’t get this working very well. The problem is that some apps don’t recognize the connection. The Android browser for instance complains that there is “No network connection” and then proceed to load the page anyway.

I think the problem is that on the Java side of Android there’s no class for reporting the state of a ppp connection to the apps. There are in fact only two NetworkStateTracker subclasses MobileDataStateTracker and WifiStateTracker. If you see a way to work around this problem please let me know.

If you’d like to try reverse tethering anyways:

  • Install the Android SDK for Linux into:
    /opt/android-sdk-linux_x86
  • Enable USB debugging on the phone
  • Disable WiFi and Mobile Data on the phone by long pressing the power button and selecting Airplane mode
  • Plug the phone to the PC with an USB cable
  • Run this reverse tethering shell script on the PC
  • Let the script automatically enable NAT routing on the PC and setup a ppp connection with the phone…
About these ads

10 Responses to “Android – USB tethering with a Linux PC”

  1. Alex Says:

    Thanks for all this information!

    I’m trying to use the “non-rooted” procedure, but I bump into problems. Every step runs without any error message, it seems that the ppp connection is well set up (see output from ifconfig below), but still my linux box can’t access the internet. My phone’s connection is good though. I’m running Ubuntu 11.04 and my phone is running Android 2.2. I’m a new linux user so I don’t know what more info would be relevant to you, but don’t hesitate to ask for more details.

    #ifconfig
    eth0 Link encap:Ethernet HWaddr 00:12:3f:e2:ca:1f
    UP BROADCAST MULTICAST MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
    Interrupt:18

    eth1 Link encap:Ethernet HWaddr 00:13:ce:2b:39:05
    inet6 addr: fe80::213:ceff:fe2b:3905/64 Scope:Link
    UP BROADCAST MULTICAST MTU:1500 Metric:1
    RX packets:5202 errors:0 dropped:0 overruns:0 frame:0
    TX packets:5742 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:4573992 (4.5 MB) TX bytes:849386 (849.3 KB)
    Interrupt:17 Base address:0xa000 Memory:dfcfd000-dfcfdfff

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:6712 errors:0 dropped:0 overruns:0 frame:0
    TX packets:6712 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:1023992 (1.0 MB) TX bytes:1023992 (1.0 MB)

    ppp0 Link encap:Point-to-Point Protocol
    inet addr:10.0.2.15 P-t-P:10.64.64.64 Mask:255.255.255.255
    UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
    RX packets:33 errors:0 dropped:0 overruns:0 frame:0
    TX packets:66 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:3
    RX bytes:1436 (1.4 KB) TX bytes:3663 (3.6 KB)

    Thanks for your help

  2. Alex Says:

    I found the problem! I removed the “usepeerdns” option in order to use my PC’s resolv.conf and not the phone’s (if my understanding is good).

    Thanks a lot for this very helpful page!!

  3. fede Says:

    Brilliant! Works out of the box (but for the sdk path) HTC Desire/Cyanogen 7.10 and Arch linux.
    Thanks a million !

  4. Nathan Says:

    Reverse-tethering works like a charm ! Thanks a lot for this feature ! Even my own app works ! But Google Play doesn’t want to install applications :( Any idea to fix this ? (and a few others apps…)
    HTC Desire with Cyanogen 7.1 and Ubuntu + Debian.

  5. Sayooj Says:

    It didnt work. Android Samsung charge 2.3.6 with Macbook pro.

    The script went well with minor tweaking on the sdk path.

    Once everything is done, I turned off my Wifi connection and tried hitting the browser with URL http://www.google.com and it showed me not connected to internet.

    Am I missing anything ? Do i Have to change anything ? Or is it just coz of MacBook Pro ?

  6. Sayooj Says:

    Is there any way to Tether over bluetooth without rooting ? Android’d internet connection to a Laptop or any device connected thru bluetooth?

  7. E Verbeek Says:

    For some reason, my Cyanogenmod 7 install on a Nexus One gets an incorrect DNS server entry when using the reverse-tether script.

    I fix this manually by running:

    # setprop net.dns1

    To see which nameserver is configured:

    # getprop | grep dns

  8. Gowtham Saran D Says:

    in my ubuntu 12.04 usb tethering is working normally without any configurations.

    but wifi tethering not working(who cares when we have alternative)

    Right now i am really enjoying to work in my linux netbook without any hanging issues or slowness..

  9. Jeremy Knight Says:

    I am having same problem as verbeek

    BUMP for justice

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: