I have an Ubuntu 12.10 system on my PC and an Android 4.1 system on my Galaxy Nexus. Now I would like to share the Internet connection of my PC via USB with my Phone. I have been searching with Google a lot, but I only found answers how you can share the Internet connection of the Phone with the PC, but I want it the other way around.
3 Answers
This is not possible. But if you have a rooted Android, there is a trick, it's called Reverse USB tethering. Check out this article, author did that on Fedora, but I guess there should be no problems achieving the same on Ubuntu.
A working solution for me on Kubuntu 15.10 & Android 4.2.2 was following
Requirements
Android
- busybox app installed
- USB cable to your computer
- USB Debugging enabled on your android device (to be able to adb from your pc)
Computer
In your /etc/network/interfaces you need to have these two lines otherwise you'll get a
no such device
errorallow-hotplug usb0 auto usb0
adb installed (
apt-get install android-tools-adb
)
Connection
In your android, enable the Settings->Connections->Network connections->Tethering and portable hotspot->USB tethering option, this will create an USB connection to your computer which you can see with
ifconfig
command on your computer. Look @ the interface name which for me isenxf6759e7fbebd
this time.enxf6759e7fbebd Link encap:Ethernet HWaddr a2:61:3c:83:1c:38 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6 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:464 (464.0 B) TX bytes:0 (0.0 B) eth0 Link encap:Ethernet HWaddr 50:e5:49:47:f4:fd inet addr:192.168.178.20 Bcast:192.168.178.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:40894966 errors:0 dropped:0 overruns:0 frame:0 TX packets:86312833 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:20969506400 (20.9 GB) TX bytes:120329513606 (120.3 GB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:30521 errors:0 dropped:0 overruns:0 frame:0 TX packets:30521 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:10868749 (10.8 MB) TX bytes:10868749 (10.8 MB)
Assign an IP address to your computer on this network interface with following command
sudo ifconfig enxf6759e7fbebd 192.168.42.135 netmask 255.255.255.0
where enxf6759e7fbebd is the interface you got on previous step.Enable IP forwaring with
sudo -- sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
Enable nat on your computer with
sudo iptables -t nat -F sudo iptables -t nat -A POSTROUTING -j MASQUERADE
On your computer connect to your android device with
adb shell su
then already having your connection you can check with
busybox ifconfig -a
commandrndis0 Link encap:Ethernet HWaddr FA:08:D6:1A:35:90 inet addr:192.168.42.129 Bcast:192.168.42.255 Mask:255.255.255.0 inet6 addr: fe80::f808:d6ff:fe1a:3590/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:564 (564.0 B)
you can add default route to your computers network address you fixed in point 2 with busybox route add default gw 192.168.42.135 dev rndis0
and that's it (you can check your routes with busybox route
command)
Script
I did a script that I run after enabling the USB Tethering option (point 1 above) that does the work for me
#!/bin/bash
interfaceName=$(ifconfig|grep enx|awk '{print $1}') #Getting interface name
sudo ifconfig $interfaceName 192.168.42.135 netmask 255.255.255.0 # setting Computer IP Address
sudo -- sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward' # Enabling IP Forwarding
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
adb shell "su -c 'busybox route add default gw 192.168.42.135 dev rndis0'"
I have found a great tool https://github.com/vvviperrr/SimpleRT. It is quite simple to use and does not require root permissions on your phone.
I tested SimpleRT using Sony Z3 Compact and Ubuntu 16.10 installed on laptop.
The steps are following:
- Install APK provided on SimpleRT
- Clone SimpleRT Git repo, or just chose "Download ZIP"
- Find your active Internet interface using terminal:
route | grep '^default' | grep -o '[^ ]*$'
- Replace in simple-rt-cli/iface_up.sh LOCAL_INTERFACE value from command above.
- Type "make" in simple-rt-cli directory.
- Run terminal as root and start CLI:
./simple-rt
- Connect your phone using USB cable
Issues (taken from the official page): Some apps do not recognize the reverse tethered internet connection due to ConnectivityManager policy. Just leave WiFi or 3g connection active, connection will go through SimpleRT anyway.