Share 3g internet from Linux PC to android device via wifi & usb-internet

MohanSai
  7 years ago
  2

Searched the net thoroughly for sharing 3g usb modem internet from a linux desktop to an android device
(1) via usb
(2) via wifi
There were lot of confusing answers with some suggesting installation of some apps on the android device, rooting the device etc. etc. But, I finally found a way to do it on unrooted device without need for any app. The following tutorial is intended to be a one-stop solution for anyone and also a quick reference for myself.
 
Before proceeding further, we need to install the following additional software packages on linux PC
(1) hostapd
(2) isc-dhcp-server
(3) guidedog
 
In this tutorial, wifi access point interface is "ra0" and android device's usb-internet interface is "enp0s29f0u1". Please check yours and replace it accordingly. Interfaces details can be found by issuing command "ifconfig -a". Also note that when you first connect your android device to PC, its interface is "enp2s0" or something like that. Change your usb connection to connected as storage on android device, and then run "ifconfig -a". Now in addition to "enp2s0" as mentioned before, you will notice a new interface which for this tutorial is "enp0s29f0u1". This is the interface we are interested in, with respect to sharing PC's internet via usb.
 
(a) modify contents of your "/etc/NetworkManager/NetworkManager.conf" file to the following
 
[main]
plugins=ifupdown,keyfile
#dns=dnsmasq

[ifupdown]
managed=true
 
(b) edit "/etc/network/interfaces" file and add
 
###
allow-hotplug ra0
iface ra0 inet static
address 192.168.0.1
netmask 255.255.255.0
broadcast 192.168.0.255
 
 
allow-hotplug enp0s29f0u1
iface enp0s29f0u1 inet static
address 192.168.137.1
netmask 255.255.255.0
broadcast 192.168.137.255
 
###
 
(c) edit "/etc/dhcp/dhcpd.conf" file and ensure that the following configurable variables are active
 
############# for sharing Linux PC's internet thru wifi ########
 
ddns-update-style none;
authoritative;
 
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.100 192.168.0.150; 
option broadcast-address 192.168.0.255; 
option routers 192.168.0.1;
default-lease-time 600;
max-lease-time 7200; 
option domain-name "local"; 
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
 
############# for sharing Linux PC's internet thru usb #########
 
authoritative;
 
subnet 192.168.137.0 netmask 255.255.255.0 {
        range 192.168.137.50 192.168.137.150;
        option broadcast-address 192.168.137.255;
        option routers 192.168.137.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
 
########
 
(d) edit "/etc/default/isc-dhcp-server" file and ensure that the target interfaces are mentioned
 
INTERFACES="ra0 enp0s29f0u1"
 
(e) issue the following commands 
sudo ifup ra0
sudo ifconfig ra0 192.168.0.1
 
sudo ifup enp0s29f0u1
sudo ifconfig enp0s29f0u1 192.168.137.1
 
(f) IP TABLES MASQUARADE /NAT
There can be commandlines to achieve this but "guidedog" is a nice simple gui tool. Simply run this tool "gksudo guidedog" and select (1) enable routing and (2) Enable IP masquarade. Apply and OK.
 
(g) Give appropriate permissions for in/out traffic flow in your firewall settings to/from the internal private IP addresses assigned on the respective interfaces only.
 
Please note that for sharing through wifi, the corresponding subnet can be any private IP range, but for sharing internet through usb, android devices restrict themselves to self-assigning only two ip addresses 192.168.0.100 (If you select host computer as Windows XP/ Vista) or 192.168.137.100 (If you select host computer as Windows 7 / 8) in the usb-internet connection dialog on android. So our subnet range for usb-internet interface must be able to provide these ip numbers to android devices.
 
Now, assuming your PC is already connected to internet via 3g usb modem, you can access internet either (1) via wifi by enabling it on your android device or (2)though usb-internet option by selecting Windows 7 or 8 if using the above mentioned subnets. These settings should work for sharing a cable modem internet connection as well. 
 
Comments
MohanSai 7 years ago

@remoulder., In my case Network Manager simply refused to create hotspots. Also regarding reverse tethering, not a single solution on web which mentions clearly that android restricts itself to specific IP numbers and that Linux users have to adapt / configure their desktop to them accordingly.
This tutorial may not be for you. Anyway, after a long period of intermittent trials, this is the only solution for me, not an overkill.


remoulder 7 years ago

Not only does this seems like complete overkill when network manager already has the ability to create wifi hotspots and reverse tethering, but also depends on which version of android is on the phone