Port range forwarding using iptables

Ranged port forwarding has its uses. I want to port forward port number 1025 to 50000 from Machine A to Machine B’s 1025 to 50000. Here is how I did it with iptables.

Machine A: 10.0.0.1
Machine B: 10.0.0.2

All operations are done in Machine A.

iptables -A INPUT -p tcp -m multiport --dports 1025:50000 -j ACCEPT
iptables -t nat -I PREROUTING -p tcp -m tcp --dport 1025:50000 -j DNAT --to-destination 10.0.0.2:1024-50000
iptables -A FORWARD -d 10.0.0.1/32 -i eth0 -p tcp -m tcp --dport 1024:50000 -j ACCEPT

If you use UDP, alter the above command to UDP and apply it again.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.