Sharing connection - IPTABLES

6,833

edit /etc/sysctl.conf and uncomment:

# net.ipv4.ip_forward=1

And the iptables part should look something like below, where eth0 is internet and eth1 is LAN:

iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

iptables -A FORWARD -i eth1 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT

If you have 2 physical network interfaces in server then it should work, you cant use your server as internet gateway if it only has 1 physical interface

Share:
6,833

Related videos on Youtube

Luck20
Author by

Luck20

Updated on September 18, 2022

Comments

  • Luck20
    Luck20 over 1 year

    I've been trying to configure IPTABLES in my server so I can share the internet from the server with another machine. The server IP is 192.168.10.1 (eth0), the connection is coming from 192.168.0.6 (eth0:2), the client is 192.168.10.10 (eth1 in the machine). I've tried using the commands from

    https://help.ubuntu.com/community/Internet/ConnectionSharing

    with the correct changes, but I can't make it work, the client can ping 192.168.0.1 (where the internet is coming from), but it can't access the internet at all.

    Server:

    eth0 192.168.10.1 eth0:2 192.168.0.6 Currently has connection with both the client and the internet

    Router:

    192.168.0.1

    Client:

    eth1 192.168.10.10 Can ping both the router and the server

    • cafebabe1991
      cafebabe1991 almost 8 years
      The questions seems quiet confusing. Are you talking about connecting to each other over ssh or just opening a port on your system so others can connect to it?
    • Luck20
      Luck20 almost 8 years
      Sorry, linked the wrong page, the correct one is help.ubuntu.com/community/Internet/ConnectionSharing
  • Luck20
    Luck20 almost 8 years
    Yes, I only have one physical interface, now I'm aware of the problem, thanks.
  • Gillespie
    Gillespie about 4 years
    Are you sure you don't mean iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE? Otherwise you are NATing packets going into the private LAN, not coming out of it
  • s.paszko
    s.paszko about 4 years
    iptables v1.6.1: unknown option "state"