Search code examples
centosiptablesportforwardingnat

iptables forward and nat ruls


anyone knows how can we make conection between with 2 computer if one computer in center of this 2 computer

in better case i want connect to linux pc together , but this 2 pc can not see each other so there is a solution i have one other linux pc this can see another 2 pc ! so there is possible any iptables rule for forward packet with this pc

i want connect from pc 1 to pc 3 on port 5050 then pc1 can connect to pc2 and pc2 can connect to pc3 on port 5050 then in replay step pc3 must replay to pc2 the pc2 must forward received packet to pc1

for better understanding see :

1 --> 2 --> 3
1 <-- 2 <-- 3

all of pc OS is Centos 5.7

i think iptables can do it , but i could not find any reference or help in google for do that, so any guys can help me ?


Solution

  • if you need one port:

    1 (addr 1.1.1.1)
    2 (addr 1.1.1.2,3.3.3.2)
    3 (addr 3.3.3.3)
    

    on 2:

    iptables -t nat -A PREROUTING -s 1.1.1.1 -p tcp --dport 5050 -j DNAT --to-destination 3.3.3.3
    iptables -t nat -A PREROUTING -s 3.3.3.3 -p tcp --dport 5050 -j DNAT --to-destination 1.1.1.1
    iptables -t nat -A POSTROUTING -d 3.3.3.3 -p tcp --dport 5050 -j SNAT --to 3.3.3.2
    iptables -t nat -A POSTROUTING -d 1.1.1.1 -p tcp --dport 5050 -j SNAT --to 1.1.1.2