[HOWTO] USB/RNDIS Tethering (root required)

Search This thread

whitslack

Senior Member
Jun 23, 2010
196
18
First, let me apologize if solutions to this problem have already been posted on this forum and elsewhere. I developed this solution completely independently, and I am sharing it here in hopes that it may prove useful to someone else.

This pair of scripts will allow you to share your phone's Internet connection with a computer via a USB cable. It definitely works when the computer is running Linux (assuming you have RNDIS support in your kernel), and it should work as well when the computer is running Windows. However, it will not work with Mac, as OS X does not have an RNDIS driver.

First, upload these two scripts to your /sdcard:

/sdcard/usb_tether_start.sh:
Code:
#!/system/bin/sh

prevconfig=$(getprop sys.usb.config)
if [ "${prevconfig}" != "${prevconfig#rndis}" ] ; then
	echo 'Is tethering already active?' >&2
	exit 1
fi

echo "${prevconfig}" > /cache/usb_tether_prevconfig
setprop sys.usb.config 'rndis,adb'
until [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done

ip rule add from all lookup main
ip addr flush dev rndis0
ip addr add 192.168.2.1/24 dev rndis0
ip link set rndis0 up
iptables -t nat -I POSTROUTING 1 -o rmnet0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
dnsmasq --pid-file=/cache/usb_tether_dnsmasq.pid --interface=rndis0 --bind-interfaces --bogus-priv --filterwin2k --no-resolv --domain-needed --server=8.8.8.8 --server=8.8.4.4 --cache-size=1000 --dhcp-range=192.168.2.2,192.168.2.254,255.255.255.0,192.168.2.255 --dhcp-lease-max=253 --dhcp-authoritative --dhcp-leasefile=/cache/usb_tether_dnsmasq.leases < /dev/null

/sdcard/usb_tether_stop.sh:
Code:
#!/system/bin/sh

if [ ! -f /cache/usb_tether_prevconfig ] ; then
	echo '/cache/usb_tether_prevconfig not found. Is tethering really active?' >&2
	exit 1
fi

if [ -f /cache/usb_tether_dnsmasq.pid ] ; then
	kill "$(cat /cache/usb_tether_dnsmasq.pid)"
	rm /cache/usb_tether_dnsmasq.pid
fi

echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -D POSTROUTING 1
ip link set rndis0 down
ip addr flush dev rndis0
ip rule del from all lookup main

setprop sys.usb.config "$(cat /cache/usb_tether_prevconfig)"
rm /cache/usb_tether_prevconfig
while [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done


To start USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_start.sh'"
Grant the superuser request on the phone if one appears.

If you're on Linux, you'll see a new network interface appear (probably called "usb0"). Bring the link up on that interface (ip link set usb0 up), run a DHCP client, and you're all set!

If you're on Windows, it will probably Just Work™.


To stop USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_stop.sh'"
Again, grant the superuser request on the phone if one appears.


That's it!
 

breizhim

Member
Feb 28, 2008
10
1
OMG !!!!

You rock ! This solution is the FIRST one which works on my international i9300 device (not a 'Sprint' version) with cm10.1.

I can't understand why ... but i'll keep your script on my ext SDCard.

I'll prupose this script to french cm10.1 users.

Thank you!
 

evandrix

New member
Feb 18, 2008
1
0
OS X does not have an RNDIS driver? really?

...OS X does not have an RNDIS driver...

Please have a look at HoRNDIS: USB tethering driver for Mac OSX (it even supports modern/recent OSX versions!) @ hxxp://joshuawise.com/horndis (unable to embed inline URL link due to new user restriction)

First, let me apologize if solutions to this problem have already been posted on this forum and elsewhere. I developed this solution completely independently, and I am sharing it here in hopes that it may prove useful to someone else.

This pair of scripts will allow you to share your phone's Internet connection with a computer via a USB cable. It definitely works when the computer is running Linux (assuming you have RNDIS support in your kernel), and it should work as well when the computer is running Windows. However, it will not work with Mac, as OS X does not have an RNDIS driver.

First, upload these two scripts to your /sdcard:

/sdcard/usb_tether_start.sh:
Code:
#!/system/bin/sh

prevconfig=$(getprop sys.usb.config)
if [ "${prevconfig}" != "${prevconfig#rndis}" ] ; then
	echo 'Is tethering already active?' >&2
	exit 1
fi

echo "${prevconfig}" > /cache/usb_tether_prevconfig
setprop sys.usb.config 'rndis,adb'
until [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done

ip rule add from all lookup main
ip addr flush dev rndis0
ip addr add 192.168.2.1/24 dev rndis0
ip link set rndis0 up
iptables -t nat -I POSTROUTING 1 -o rmnet0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
dnsmasq --pid-file=/cache/usb_tether_dnsmasq.pid --interface=rndis0 --bind-interfaces --bogus-priv --filterwin2k --no-resolv --domain-needed --server=8.8.8.8 --server=8.8.4.4 --cache-size=1000 --dhcp-range=192.168.2.2,192.168.2.254,255.255.255.0,192.168.2.255 --dhcp-lease-max=253 --dhcp-authoritative --dhcp-leasefile=/cache/usb_tether_dnsmasq.leases < /dev/null

/sdcard/usb_tether_stop.sh:
Code:
#!/system/bin/sh

if [ ! -f /cache/usb_tether_prevconfig ] ; then
	echo '/cache/usb_tether_prevconfig not found. Is tethering really active?' >&2
	exit 1
fi

if [ -f /cache/usb_tether_dnsmasq.pid ] ; then
	kill "$(cat /cache/usb_tether_dnsmasq.pid)"
	rm /cache/usb_tether_dnsmasq.pid
fi

echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -D POSTROUTING 1
ip link set rndis0 down
ip addr flush dev rndis0
ip rule del from all lookup main

setprop sys.usb.config "$(cat /cache/usb_tether_prevconfig)"
rm /cache/usb_tether_prevconfig
while [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done


To start USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_start.sh'"
Grant the superuser request on the phone if one appears.

If you're on Linux, you'll see a new network interface appear (probably called "usb0"). Bring the link up on that interface (ip link set usb0 up), run a DHCP client, and you're all set!

If you're on Windows, it will probably Just Work™.


To stop USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_stop.sh'"
Again, grant the superuser request on the phone if one appears.


That's it!
 

Top Liked Posts

  • There are no posts matching your filters.
  • 6
    First, let me apologize if solutions to this problem have already been posted on this forum and elsewhere. I developed this solution completely independently, and I am sharing it here in hopes that it may prove useful to someone else.

    This pair of scripts will allow you to share your phone's Internet connection with a computer via a USB cable. It definitely works when the computer is running Linux (assuming you have RNDIS support in your kernel), and it should work as well when the computer is running Windows. However, it will not work with Mac, as OS X does not have an RNDIS driver.

    First, upload these two scripts to your /sdcard:

    /sdcard/usb_tether_start.sh:
    Code:
    #!/system/bin/sh
    
    prevconfig=$(getprop sys.usb.config)
    if [ "${prevconfig}" != "${prevconfig#rndis}" ] ; then
    	echo 'Is tethering already active?' >&2
    	exit 1
    fi
    
    echo "${prevconfig}" > /cache/usb_tether_prevconfig
    setprop sys.usb.config 'rndis,adb'
    until [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done
    
    ip rule add from all lookup main
    ip addr flush dev rndis0
    ip addr add 192.168.2.1/24 dev rndis0
    ip link set rndis0 up
    iptables -t nat -I POSTROUTING 1 -o rmnet0 -j MASQUERADE
    echo 1 > /proc/sys/net/ipv4/ip_forward
    dnsmasq --pid-file=/cache/usb_tether_dnsmasq.pid --interface=rndis0 --bind-interfaces --bogus-priv --filterwin2k --no-resolv --domain-needed --server=8.8.8.8 --server=8.8.4.4 --cache-size=1000 --dhcp-range=192.168.2.2,192.168.2.254,255.255.255.0,192.168.2.255 --dhcp-lease-max=253 --dhcp-authoritative --dhcp-leasefile=/cache/usb_tether_dnsmasq.leases < /dev/null

    /sdcard/usb_tether_stop.sh:
    Code:
    #!/system/bin/sh
    
    if [ ! -f /cache/usb_tether_prevconfig ] ; then
    	echo '/cache/usb_tether_prevconfig not found. Is tethering really active?' >&2
    	exit 1
    fi
    
    if [ -f /cache/usb_tether_dnsmasq.pid ] ; then
    	kill "$(cat /cache/usb_tether_dnsmasq.pid)"
    	rm /cache/usb_tether_dnsmasq.pid
    fi
    
    echo 0 > /proc/sys/net/ipv4/ip_forward
    iptables -t nat -D POSTROUTING 1
    ip link set rndis0 down
    ip addr flush dev rndis0
    ip rule del from all lookup main
    
    setprop sys.usb.config "$(cat /cache/usb_tether_prevconfig)"
    rm /cache/usb_tether_prevconfig
    while [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done


    To start USB tethering:
    Code:
    adb shell "su -c 'sh /sdcard/usb_tether_start.sh'"
    Grant the superuser request on the phone if one appears.

    If you're on Linux, you'll see a new network interface appear (probably called "usb0"). Bring the link up on that interface (ip link set usb0 up), run a DHCP client, and you're all set!

    If you're on Windows, it will probably Just Work™.


    To stop USB tethering:
    Code:
    adb shell "su -c 'sh /sdcard/usb_tether_stop.sh'"
    Again, grant the superuser request on the phone if one appears.


    That's it!