Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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

In my opinion following order of iptables commands would work:

1. iptables -t nat -A POSTROUTING -s -o eth0 -j RETURN
2. iptables -t nat -A POSTROUTING -s -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 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

Your Answer


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.