Friday, August 5, 2016

linux - Keep source port for outgoing packets




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




iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE




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




192.168.0.1:8080 -> nat -> 10.1.1.15:8080





Is it possible to do this with iptables?


Answer



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 [-]


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...