Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I make iptables NAT all packets, except those, that have a target address from 192.168.254.0/24 subnet?

share|improve this question
add comment

2 Answers

If I understand you well, this should be fairly easy using the option -d with negation. For example, a rule like this one:

$ iptables -A POSTROUTING ! -d 192.168.254.0/24 -j SNAT --to-source new_source_ip

Of course, you can change the rule according to your needs. This changes the source IP of all packets destined to any destination except the indicated subnet.

share|improve this answer
add comment

In my opinion following order of iptables commands would work:

1. iptables -t nat -A POSTROUTING -s 192.168.254.0/24 -o eth0 -j RETURN
2. iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -o eth0 -j MASQUERADE

rule 1 will match your network and won't MASQUERADE it rule 2 will masquerade all but generally it's not good idea to give 0.0.0.0 wildcard (I'm not even sure if it will work). You should selectively add all networks that you want to masquerade and rest just won't be masqueraded.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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