I have 2 interfaces eth0( and eth1( And I do nat for all packets in this way.

iptables -t nat -A POSTROUTING -s -j MASQUERADE

Now I want to keep source port for natted packets, like this: -> nat ->

Is it possible to do this with iptables?


By default MASQUERADE (like SNAT) tries to retain the original port of the machine establishing the connection. But of course, if two hosts use the same one then it will have to choose another one for the second host.

You can also explicitely set the the port range per protocol with :

/sbin/iptables -t nat -A POSTROUTING -p tcp -j MASQUERADE --to-ports <i>[-<n>]

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.