I have a 3g mobile stick and in certain circumstances I would need to route internet traffic of a PC/server or a particular subnet through this interface. In a windows OS we could simply achieve this by enabling internet sharing on the interface itself. But in the case of ubuntu we can easily enhance ICS (internet connection sharing) feature to a higher level as internet gateway. This way your linux machine can be turned into a gateway which is way cool, impotent isn’t it?
Here is the scenario in which I have tested this:
Internet <<==>> ppp0 (Huawei mobile 3g) <> Ubuntu 12.04 gateway <> eth0 <<==>> Client PC / subnet
Ubuntu eth0 ip: 10.132.180.16 local sharing subnet: 10.13.20.0/24 local gateway: 10.13.20.1
Step 1) Configure iptables
Configure iptables for NAT translation so that packets can be correctly routed through the Ubuntu gateway.
sudo iptables -A FORWARD -o ppp0 -i eth0 -s 10.13.20.0/24 -m conntrack --ctstate NEW -j ACCEPT sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT sudo iptables -t nat -F POSTROUTING sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
save iptables settings
sudo iptables-save | sudo tee /etc/iptables.sav
Edit /etc/rc.local and add the following lines before the “exit 0? line:
iptables-restore < /etc/iptables.sav
Step 2 ) Enable routing
Configure the gateway for routing between two interfaces by enabling IP forwarding:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
edit /etc/sysctl.conf and uncomment: (line 28)
Step 3 ) Client setup
do the following changes in the routing table to set the ubuntu gateway as the default internet gateway:
sudo route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.13.20.1 sudo route del default gw 10.13.20.1 sudo route add default gw 10.132.180.16
modify your dnsserver to point to either the ip of the gateway or a public dns
edit /etc/resolv.conf and add the following line on top:
- February 7, 2014 @ 05:35:27 [Current Revision] by admin
- February 7, 2014 @ 05:35:27 by admin