Friday, August 28, 2009

Sharing internet using iptables

I am explaining how two share internet from one connection to other, here i am using one connection on my ethernet port to share with the wireless add hoc network.

settings at ethernet port (eth0)
IP : 192.168.1.9
MASK : 255.255.255.0
Gateway : 192.168.1.1

my laptop is configured to get internet from modem connected through LAN port and IP address of Modem is 192.168.1.1

settings for my Wifi Adaptor(wan0)
IP : 192.168.0.1
MASK : 255.255.255.0
Gateway : 192.168.1.1

now after this i have created an add hoc network so the systems connecting to my WiFi add hoc network must have following configuration
IP : 192.168.0.[2-254]
MASK : 255.255.255.0
Gateway : 192.168.0.1
DNS  : 192.168.0.1

Now configuration part is done, next we need to setup sharing, first of all we will enable ip forwarding by issuing following command as root user
sysctl -w net.ipv4.ip_forward=1
To enable it in system startup, edit the file /etc/sysctl.conf and set
net.ipv4.ip_forward = 1
now for sharing do the following commands. this should again be done as root user
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
service iptables save

I use open DNS for better resolution, you can also tryout same
208.67.222.222
208.67.220.220

1 comment: