Skip to content

Instantly share code, notes, and snippets.

@mrenouf
Created August 11, 2012 23:01
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mrenouf/3327703 to your computer and use it in GitHub Desktop.
Save mrenouf/3327703 to your computer and use it in GitHub Desktop.
USB reverse tethering script (only tested on Ubuntu 12.04 with Galaxy Nexus)
#!/bin/bash
ADB="/home/mrenouf/bin/adb"
# We need root on the host to mess with networking
if [[ $(whoami) != "root" ]]; then
echo "You must be root to run this script!"
exit 1
fi;
# We need root on the device to mess with networking
$ADB -d root
# Keep NetworkManager from messing with the adapter
grep 'usb0 inet manual' /etc/network/interfaces
if [ ! $? ]; then
echo 'Configuring usb0 for manual control'
echo 'iface usb0 inet manual' >> /etc/network/interfaces
restart network-manager
fi
# Turn on usb networking, without dnsmasq or dhcp
echo 'Enabling usb network interface on the device'
$ADB -d shell setprop sys.usb.config rndis,adb
echo 'Waiting for adb to restart on the device...'
$ADB -d wait-for-device
echo 'Setting up usb networking on device'
$ADB -d shell 'ip addr add 192.168.200.2/30 dev rndis0;\
ip link set rndis0 up; \
ip route delete default; \
ip route add default via 192.168.200.1; \
setprop net.dns1 192.168.200.1'
echo 'Setting up usb interface on the host'
ip addr flush dev usb0
ip addr add 192.168.200.1/30 dev usb0
ip link set usb0 up
# Turn off the firewall if one is active
echo 'Checking for ufw firewall'
which ufw && ufw status || ufw disable
echo 'Enabling NAT and IP Forwarding'
# Start forwarding and nat (use existing default gw)
iptables -F -t nat
iptables -A POSTROUTING -t nat -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 'Starting dnsmasq'
dnsmasq --interface=usb0 --no-dhcp-interface=usb0
echo 'Connection is active! Press any key to shutdown.'
read
echo 'Attempting to shut down reverse tethering'
killall dnsmasq
ip link set usb0 down
iptables -F -t nat
echo 0 > /proc/sys/net/ipv4/ip_forward
echo 'Disabling usb networking on host'
$ADB -d shell setprop sys.usb.config adb
$ADB wait-for-device
$ADB shell ip route delete default
echo 'Disable and re-enable Wifi to return the device to normal'
@uranio-235
Copy link

Enabling usb network interface on the device
Waiting for adb to restart on the device...
Setting up usb networking on device
ip: can't find device 'rndis0'
ip: SIOCGIFFLAGS: No such device
ip: RTNETLINK answers: No such process
ip: RTNETLINK answers: Network is unreachable
Setting up usb interface on the host
Device "usb0" does not exist.
Cannot find device "usb0"
Cannot find device "usb0"
Checking for ufw firewall
Enabling NAT and IP Forwarding
Starting dnsmasq
Connection is active! Press any key to shutdown.

and do not work

@ssundarraj
Copy link

reverse-tether.sh: line 12: /home/mrenouf/bin/adb: No such file or directory
Enabling usb network interface on the device
reverse-tether.sh: line 24: /home/mrenouf/bin/adb: No such file or directory
Waiting for adb to restart on the device...
reverse-tether.sh: line 26: /home/mrenouf/bin/adb: No such file or directory
Setting up usb networking on device
reverse-tether.sh: line 29: /home/mrenouf/bin/adb: No such file or directory
Setting up usb interface on the host
Device "usb0" does not exist.
Cannot find device "usb0"
Cannot find device "usb0"
Checking for ufw firewall
/usr/sbin/ufw
Status: inactive
Enabling NAT and IP Forwarding
Starting dnsmasq

dnsmasq: unknown interface wlan0
Connection is active! Press any key to shutdown.

Tested on Nexus 4. Not working.

@damonsmith
Copy link

@Srirams6 - the error message says /home/mrenouf/bin/adb doesn't exist. That's not your home dir or username is it? You need to at least change that first line to the location of your adb executable then try again.

@propertunist
Copy link

anyone know how to do this on fedora 21, since firewallD is the firewall, not IPTABLES?

@dcerisano
Copy link

So this script is awesome ... Spent hours trying to write one, but this one just does it.
Worked right away on LXLE to Nexus 4. You are right about it not wanting to let go.

Using it now to test an RTSP streaming app (a screen caster).
Doubleplus like!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment