Internet Connection Sharing using iptables

Networking

It is very easy to setup an internet connection sharing in Linux system using iptables. This method can be used to share an internet connection from a Linux system(I used Fedora Core 6, but it should work on other distributions that support iptables). Another method of doing this is using a proxy server like squid.

Enable IP forwarding

Run as root

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

iptables

Run command as root

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
service iptables save

Configuration

The configuration should be like this

Router

Connected to the internet provider
IP : 192.168.1.1

Internet Connected System

eth0 (LAN Card 1)

Connected to router

  • IP : 192.168.1.10
  • Netmask : 255.255.255.0
  • Gateway : 192.168.1.1 (IP of the router)

eth1 (LAN Card 2)

Connected to the other system

  • IP : 192.168.0.20 (Not the same network as the first card)
  • Netmask : 255.255.255.0
  • Gateway : 192.168.1.1 (IP of the router)

Second System

LAN Card connected to the first system

  • IP : 192.168.0.30
  • Netmask : 255.255.255.0
  • Gateway : 192.168.0.20 (IP of the second Card in the first system)

Disclaimer

Linux networks is not a subject I am an expert on. So take my advice with a pinch of salt. The above procedure worked for me. YMMV.

Status

Some results of various commands are shown here. Check to see if it matches the result on your system.

# iptables -t nat -L POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  anywhere             anywhere
# cat /proc/sys/net/ipv4/ip_forward
1
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

32 comments

  1. @flape
    I use Fedora 7 – it works for me here. I had a bit of trouble setting it up at first – but in a few attempts it worked fine.

  2. Hi
    Can anbody tell if i share internet through this. then how can i do port forwarding for VNC and WEB Server located on windows client. what rules should be add in IPtables

  3. I tried it on fedora 7 (kernel 2.6.21). It didn’t work… Is there something else I need to download and install?

  4. Hi guys, nice tutorial, but i’m wondering if a different setup would still work. This is my current system:

    – 1 external wireless router that shares the internet connection to the entire building (and to which I have no access to and uses DHCP to provide IPs to the clients);
    – 1 server machine with 1 wireless card (connecting to the external router (again, DHCP)), and one internal (wired) network card, which I would setup with a static IP address;
    – 1 wireless router (my own, full access), which I can connect to the server’s static IP network card;
    – 1 laptop, which I wish to connect to the internal router and still have internet from the external router.

    Is this possible, and if so, how do I set it up?

    Thanks in advance,
    Pedro

  5. please help me I would to setup and configuration internet server by IP table and web server and send mail Server on linux redhat Enterprise 4 or fedora 3 or 5
    please help step by step ditail

  6. Is there anyone who knows how to set up this in similar situatuion: My laptop is connected to internet via eth1, but eth1 is ethernet emulation card and eth0 is other notebook.

  7. This worked for me on FC6, if I use a PC as the 2nd box. Use a x-over cable, not straight through.

    My xbox wouldn’t work though. I’m looking into this right now, as I’ve had network problems with the xbox in the past.

    I also want to have the 2nd NIC on my FC6 box run to a switch, then connect other machines to the switch. This hasn’t worked so far. The xbox is more important.

    I’ll post here again if I find anything out. Thanks BinnyVA!

  8. Thanks a lot Binny VA it help me so much, continue sharing your idea to every body, am new in Redhat 9, when i use the command it work fine.

  9. Dear BinnyVA
    I use centos 5 . I do all step and check all result . My result just like your result and i can ping yahoo or other lan station from centOs but other station can’t connect to internet .
    Also i use rp-pppoe-3.10.tar for my ADSL connection .
    Thank u

  10. has anyone had any success sharing a connection from linpus lite (fedora) to a windows machine using a netbook? anyone have any good ideas for this?

    — beth

  11. Hi
    Thanks for the tips. I am getting wireless connection. and i want to share it through RJ45 Network card. Can you please guide for that?
    Thanks in advance.

  12. Hi Binny

    What about internet sharing between two computers (both running Fedora 9), where one is connected through GPRS bluetooth modem (RFCOMM).
    In this case what are the gateway/DNS and LAN settings for the two computers….
    Where ‘A’ is connecting to the internet through GPRS where it is assigned a Local address and a remote address by the ISP and ‘B’ is connecting to internet through ‘A’ computer

    Alok

  13. Nearly works … I can ping between networks, but something seems to be blocking HTTP traffic in Fedora 10. Is there some kind of additional step for forwarding ports?? =/

  14. Actually, ignore last comment – works great with Fedora 10! I simply used the wrong device name in the MASQUERADE line. Doh! =/

    Only issue remaining for me is DNS. I have access to everything by IP but not by domain name.

  15. To access by name

    1. Setup your DNS – /etc/named.conf

    2. Configure the httpd for virtualhost – /etc/httpd/conf/httpd.conf

    Sample virtualhst
    ———————

    NameVirtualHost *:80

    DocumentRoot /var/www/html
    ServerName http://www.sample.info
    ErrorLog logs/sample.info-error_log
    CustomLog logs/sample.info-access_log common

  16. Can anyone tell me on how to share connection from ubuntu to kubuntu?
    Because this isn’t working, and the SMB and NFS servers are not installed on my kubuntu computer, so it’s not connecting, and it doesn’t have connection so I keep trying to connect my kubuntu computer from my ubuntu laptop.

  17. Thanks master 😀

    My computers are now connected with internet connection.

    The internet connected system use PCLinuxOS Minime 2008 and the second system use PClinuxOS minime 2010.

    Your tutorial works like a charm 😀

    Thank you very much

  18. Hi , i have try this on ubuntu with no luck.
    i can do with vise versa from windows as the host but i want to use linux as the host pc. am using a regular cable do i need a crossover , since its working from win to linux i dont think but am just asking anyway. i will install centos and see if it works

  19. does this work for suse? I have tried it but for some reason it hasn’t worked for me yet I have tried a few different configurations.

    Thanks,
    Matt

Leave a Reply to vginov Cancel reply

Your email address will not be published. Required fields are marked *