Android USB Tethering
Tethering means sharing the Internet connection of an Internet-capable mobile phone with other devices. This sharing can be offered over a wireless LAN (Wi-Fi), or over Bluetooth, or by physical connection using a cable (Wikipedia).
Our focus in this article will be the USB tethering.
Your Android mobile phone can behave as an external network interface usbN, connected via the USB infrastructure. The usbN interface is in turn connected to the mobile phone LAN, providing dhcp server, DNS server, gateway and so on.
That's how your mobile phone can provide an Internet connection to another device.
Android phones are already equipped to provide this functionality. Simply connect the USB cable and go to Settings -> Wireless settings -> Tethering -> Tethering USB.
What you need is some kernel and network configuration on the other side (e.g. your laptop).
Contents |
Tested Devices
- Google Nexus One (Android 2.3.6 build GRK39F)
- LG P350 (Android 2.2.1 build FRG83)
- LG E400 (Android 2.3.6 build GRK39F)
- Motorola Defy (Android 2.3.7 Cyanogenmod 7)
- Samsung Galaxy S i9000
- Samsung Galaxy Nexus i9250 (Android 4.0.1 ITL41D)
- Samsung Galaxy S2
Kernel configuration
General setup --->
[*] Prompt for development and/or incomplete code/drivers
Device Drivers --->
[*] Network device support --->
USB Network Adapters --->
[*] Multi-purpose USB Networking Framework
<M> CDC Ethernet support
<M> CDC EEM support
<M> Simple USB Network Links (CDC Ethernet subset)
<M> Host for RNDIS and ActiveSync devices
[*] Embedded ARM Linux linksYou can either build the options in the kernel or leave them as module.
Testing
root # dmesg
usb 1-7: New USB device found, idVendor=18d1, idProduct=4e13
usb 1-7: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-7: Product: Nexus One
usb 1-7: Manufacturer: Google, Inc.
usb 1-7: SerialNumber: HT9CSP803294
usb 1-7: usb_probe_device
usb 1-7: configuration #1 chosen from 1 choice
usb 1-7: adding 1-7:1.0 (config #1, interface 0)
rndis_host 1-7:1.0: usb_probe_interface
rndis_host 1-7:1.0: usb_probe_interface - got id
rndis_host 1-7:1.0: usb0: register 'rndis_host' at usb-0000:00:1d.7-7, RNDIS device, ea:61:37:88:a2:e5
usb 1-7: adding 1-7:1.1 (config #1, interface 1)
drivers/usb/core/inode.c: creating file '004'
hub 1-0:1.0: state 7 ports 8 chg 0000 evt fe80
uhci_hcd 0000:00:1d.0: reserve dev 2 ep81-INT, period 8, phase 4, 93 us
usb 1-7: link qh32-0001/ffff88021aa4bc80 start 1 [1/0 us]
usb0: no IPv6 routers present
root # ifconfig -aIf you see usb0 (or whatever number you'll get) you are all set.
Connecting
Since the mobile phone LAN changes its addresses, you need a DHCP client to configure the usbN device.
If you are on a laptop, you probably have a DHCP client. If not, emerge net-misc/dhcpcd.
root # emerge --ask net-misc/dhcpcdManually
Simply run dhcpcd after plug/mobile activation.
root # dhcpcd usb0Permanent configuration
Edit your /etc/conf.d/net to have a permanent, automatic activation of the interface.
root # echo "config_usb0=\"dhcp\"" >> /etc/conf.d/netOnce plugged in and activated on the mobile phone side, the usb0 will be up and configured.
Checks
Run the usual checks to verify your connection.
root # ifconfig usb0
root # route
root # cat /etc/resolv.confAdvanced network settings
The DHCP is very quick, but the default settings don't give you as much freedom as you may want.
A possible scenario is that you are in a corporate, protected LAN context that doesn't give you the Internet connection but where you need to stay connected to have access to some Intranet resource.
Or maybe you have a free but limited connection (a public wifi allowing http only, an evil firewall, etc.). Since mobile connections could be expensive, you could want to save your money up using the tethering only when needed.
Here is a handful of examples about the DHCP usage.
If you want to limit the information set by DHPC, you can fine-tune its behaviour.
E.g.:
root # dhcpcd --nogateway --nohook resolv.conf --nohook hostname usb0This will let your default gateway, resolv.conf and hostname as they are, letting you provide extra info by hand.
E.g.:
root # route add -host my_sshd_remote_host_IP dev usb0Finally, you can permanently configure your USB network interface.
E.g.
config_usb0="dhcp"
dhcpcd_usb0="--nogateway --nohook resolv.conf --nohook hostname"
# Special hosts
postup()
{
if [[ "usb0" == ${IFACE} ]]; then
route add -host my_sshd_remote_host_IP dev usb0
fi
}
Generally, you can avoid every DHCP setting (see the man page) but gather them with
root # dhcpcd -U usb0Then you can set what you want in the postup hook.