Thursday, May 12, 2011

How to share 3G internet in a local area network

Let's аssume that you have a computer connected to the Internet via 3G modem, and you want to share it to another computer connected in a Ethernet local network.

The scheme of the connection will be like this:
Internet ---> | PC1 (the gateway) | ---> PC2 (switch) .. etc

Initial conditions:

Both computers are connected over a network.
We have 3G modem and Ethernet network card in PC1 :
ppp0 - connected to Internet
eth0 - connected to LAN

and eth0 in PC2

Customize PC1

Set up the Ethernet card (eth0) as follows:
IP: 192.168.0.1
Netmask: 255.255.255.0
or type in the console:
ifconfig eth0 192.168.0.1 netmask 255.255.255.0
ifconfig eth1 up

Allow the packets

To do this, edit /etc/sysctl.conf: insert a line
net.ipv4.ip_forward = 1

Run the following line in the terminal:
sudo sysctl-w net.ipv4.ip_forward = "1"
in order to apply this rule before reboot.

Install iptables.
sudo apt-get install iptables
Type the following rule (for the transfer of Internet second computer) and save it.
iptables-t nat-A POSTROUTING-o ppp0-j MASQUERADE

Install dnsmasq
sudo apt-get install dnsmasq
Run it:
sudo service dnsmasq start

Customize PC2

IP: 192.168.0.2
Netmask / Mask: 255.255.255.0
Gateway / Gateway: 192.168.0.1
DNS: 192.168.0.1

Now the second computer should now be connected to the Internet.
If after restarting iptables rules are not restored, add
iptables-t nat-A POSTROUTING-o ppp0 -j MASQUERADE
in any startup script (rc.local for example)