Iptables Postrouting with SNAT for a paritcular destination IP

Shyamkkhadka picture Shyamkkhadka · Aug 5, 2013 · Viewed 41k times · Source

I want to define a rule in iptables for postrouting in ubuntu as below:

$IPTABLES -t nat -A POSTROUTING  -o $EXTIF -p tcp -d xxx.xx.xx.8 -j SNAT --to-source xxx.xx.xx.238

It means for destination with IP xxx.xx.xx.8 (a oublic IP) , I want its source IP to be xxx.xx.xx.238.

The above rule is not working . Any suggestions. Thank you.

Answer

Shyamkkhadka picture Shyamkkhadka · Aug 7, 2013

I got the solution myself as below: I added a new IP in sub interface(eth0:0), with my required NATting IP. For example xxx.xx.xx.238 with eth0:0. Now I added a new rule in IPTABLE as

$IPTABLES  -t nat -A POSTROUTING  -o eth0.0 -p tcp -d xxx.xx.xx.8 -j SNAT --to-source xxx.xx.xx.238

.

When accessing the application in IP xxx.xx.xx.8, It shows the NATting IP to be xxx.xx.xx.238.