I wonder how it is possible to locally forward a tcp port, bidirectional, i. e. all packets leaving the local machine on port 25 (this port of course is actually dynamically assigned) should be rewritten to have a source port of e. g. 500 (thats the part I dont know how to do), and all ports coming in on port 500 should be forwarded to port 25 (I know how to do this).
In consequence, a daemon listening on port 25 would be happy, while all destinations would believe the daemon is listening on port 500. Correct me if I am wrong.


When you redirect port 25 to port 500, you don't need to redirect port 500 to port 25 again. NAT works with connections and it will handle return packets for you.

For example try to redirect port 23 to port 22 which ssh is listening

iptables -t nat -A PREROUTING -p tcp --dport 23 -j REDIRECT --to-port 22

Now you can connect that device via port 23

ssh foo@mysshserver -p 23

In your case you should add this rule

iptables -t nat -A PREROUTING -p tcp --dport 500 -j REDIRECT --to-port 25

Now all client are able to connect via port 500 to daemon which is listening port 25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.