Iptables postrouting with SNAT for sender IP address

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

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

This means that for a destination with IP xxx.xx.xx.8 (public IP), I want its source IP address to be xxx.xx.xx.238.

The above rule does not work. Any suggestions. Thanks.

+6
source share
1 answer

I got the solution myself, as shown below: I added a new IP address in the sub-interface (eth0: 0) with my NAT IP address. For example, xxx.xx.xx.238 with eth0: 0. Now I added a new rule to 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 indicates that the NAT IP address should be xxx.xx.xx.238.

+7
source

Source: https://habr.com/ru/post/951016/


All Articles