I went nuts trying to find a simple tutorial to set up port forwarding in Iptables. So I’ve got this here. Non-linux people, skip it.
First ensure IP Forwarding is enabled:
/etc/sysctl.conf
net.ipv4.ip_forward=1
In a running system
echo 1 > /proc/sys/net/ipv4/ip_forward
Then, setup iptables for SNAT
For static IP
iptables -t nat -A POSTROUTING -o $EXTERNAL_INTERFACE(eth0,ppp0,etc) -j SNAT –to xxx.xxx.xxx.xxx
where xxx.xxx.xxx.xxx = the static IP assigned to you by the ISP
For dynamic IP
iptables -A POSTROUTING -t nat -o $EXTERNAL_INTERFACE -j MASQUERADE
Add some security
iptables -A INPUT -j DROP -m state –state NEW,INVALID -i $EXTINTERFACE
iptables -A FORWARD -j DROP -m state –state NEW,INVALID -i $EXTINTERFACE
Now to add the forwarding rules!! Finally!
iptables -A PREROUTING -t nat -p tcp –dport $PORTNUMBER -i $EXTINTERFACE -j DNAT –to-destination aaa.aaa.aaa.aaa:bbbb
where aaa.aaa.aaa.aaa:bbbb = IP of internal machine : port number (192.168.110:6900, for example)
iptables -I FORWARD 1 -d aaa.aaa.aaa.aaa -p tcp –dport $PORTNUMBER -j ACCEPT
To forward a range of ports to one machine:
–dport 6920:6980
and remove any port from the –to-destination,
just
–to-destination 192.168.4.75
to add multiple rules, for more than one machine, remember to change the FORWARD 1 to FORWARD 2 and so on.
Once done, don’t forget to save everything
iptables-save > /etc/sysconfig/iptables
Random