Configure your ubuntu desktop as an internet gateway

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:
local sharing subnet:
local gateway:

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 -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 netmask gw
sudo route del default gw
sudo route add default gw

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:



No comments yet.

Leave a Reply