Android - How do you set up internet pass-through (reverse-tether) on linux?

The following works for a while, but only for the browser.

When you plug in your phone via usb and choose Internet pass-through, you should get a new RNDIS device (usb0 or usb1). For ubuntu, edit /etc/network/interfaces and add the following lines:

iface usb0 inet dhcp
iface usb1 inet dhcp

This will assign an IP automatically when the device is added. If a network address is not assigned automaticly to usb0 or usb1 you have to do it manually.

Then you need to set up NAT on your linux computer. Something like:

sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo echo 1 > /proc/sys/net/ipv4/ip_forward 

You also need a DNS server:

sudo apt-get install bind9

This works for a few minutes, but after a while the phone gives up looking for HTC Sync and disconnects.

For rooted phones

You can connect permanently if you have a rooted phone (fre3vo worked for me). Execute the following with adb.

Enable usb mode for network and adb:

adb shell echo 6 > /sys/devices/platform/msm_hsusb/usb_function_switch

Assign an IP to the usb0 device on the phone:

adb shell ifconfig usb0 192.168.99.5 netmask 255.255.255.0 up

Add a default route. The IP should be the IP assigned to usb0 in linux:

adb shell route add default gw 192.168.99.1 dev usb0

Set the DNS server. This is google's open DNS server, but it can be the IP of your linux computer if you have a DNS server:

adb shell setprop net.dns1 8.8.8.8

A made this small bash to automate all. Just plug your phone in charging mode to your computer and run this bash.

#!/bin/bash
/opt/android-sdk-update-manager/platform-tools/adb shell 'echo 6 > /sys/devices/platform/msm_hsusb/usb_function_switch'
sleep 1
/opt/android-sdk-update-manager/platform-tools/adb shell ifconfig usb0 192.168.99.5 netmask 255.255.255.0 up
sleep 1
/opt/android-sdk-update-manager/platform-tools/adb shell setprop net.dns1 8.8.8.8

get_ip ()
{
ifconfig usb0 | grep inet | awk '{print $2}' | sed 's/addr://' | grep .
}

echo "waiting for IP on computer usb0"
while [[ `get_ip` < 192 ]];do sleep 2; done
ip=`get_ip`
echo "IP adress is $ip "
/opt/android-sdk-update-manager/platform-tools/adb shell route add default gw $ip dev usb0
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE