Internet Connection Sharing using iptables

Written by BinnyVA on April 16, 2007 – 4:26 pm -


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


Run command as root

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


The configuration should be like this


Connected to the internet provider
IP :

Internet Connected System

eth0 (LAN Card 1)

Connected to router

  • IP :
  • Netmask :
  • Gateway : (IP of the router)

eth1 (LAN Card 2)

Connected to the other system

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

Second System

LAN Card connected to the first system

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


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.


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

# iptables -t nat -L POSTROUTING
target     prot opt source               destination
MASQUERADE  all  --  anywhere             anywhere
# cat /proc/sys/net/ipv4/ip_forward
# 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 | Digg it | reddit | StumbleUpon

Posted in Command Line, Fedora, Networking, Tutorials | 32 Comments »

32 Comments to “Internet Connection Sharing using iptables”

  1. flape says:

    Well works on Fedora 5 and Fedora 6, but not on Fedora 7 :(

  2. BinnyVA says:

    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.

  3. Gurdeep Singh says:

    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

  4. samer says:

    Its not working on F7 any help please 😀

  5. Worked a dream. Ran the linux commands and it worked straight away – Fedora Core 7 with Windows clients.
    Many many thanks!

  6. James says:

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

  7. Abhi says:


    I tried it on my fedora 7 but didn’t worked :(

    i didn’t get which router address we need to add?

  8. Pedro says:

    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,

  9. khomkhay says:

    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

  10. vijay says:

    I m trying with fedora 8 but its not happening please help.

  11. vijay says:

    I m trying with fedora 8 but its not happening please help.

  12. BinnyVA says:

    I now have Fedora 8 – it worked perfectly.

  13. joshua says:

    BinnyVA: what did u do on fedora 8 to make it work :)

  14. BinnyVA says:

    Just follow the given instructions – it worked without any trouble for me.

  15. DigitalBrain Ntavyo says:

    This is real pretty cool im using Centos 5 and Fedora 8 it work fine

  16. Natalie says:

    I’ve done it before and it is easy so there is nothing wrong with your advice thanks for taking the time to write them so clearly.

  17. nildon says:

    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.

  18. willis says:

    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!

  19. jhp says:

    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.

  20. Ali says:

    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

  21. emanz says:

    your a God sent ……….tnx for helping us…it really works……

  22. beth says:

    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

  23. Gino says:

    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.

  24. Alok says:

    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


  25. Greg says:

    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?? =/

  26. Greg says:

    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.

  27. vginov says:

    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
    ErrorLog logs/
    CustomLog logs/ common

  28. linzylinz says:

    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.

  29. Masdin says:

    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

  30. Thank you for that information. The method of Internet connection sharing could be used on multiple levels of LAN.

  31. Alex quinones says:

    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

  32. matt says:

    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.


Leave a Comment