What is the iptables command to achieve the following rule as reported by sudo iptables -t nat -L -n -v

Chain POSTROUTING (policy ACCEPT 133 packets, 10675 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   74  4322 MASQUERADE  all  --  *      *        !        
  • you can see it by yourself by doing iptables -t nat -S POSTROUTING
    – LL3
    Jul 15 '19 at 16:45
  • Thank you. I also found it the hard way ~~~ sudo iptables --table nat -A POSTROUTING -s ! -d -j MASQUERADE ~~~
    – Gary Smith
    Jul 15 '19 at 16:49

As prompted by LL3 above one option is:

sudo iptables -A POSTROUTING -s ! -d -j MASQUERADE

and another which implicitly refers to the nat table, which I used, is

sudo iptables --table nat -A POSTROUTING -s ! -d -j MASQUERADE
  • Please note that I prompted a command that prints all current rules of a specific chain, showing the syntax you would need to use to replicate those rules, so that you can see by yourself how a particular rule as shown by -L can be obtained on the command line. Also note that -S output is always relative to the table and chain you specify, meaning that to replicate the same rule you need to specify the table explicitly if it is not the -t filter one. For this reason your first example here will not work because the POSTROUTING chain does not exist in the -t filter table.
    – LL3
    Jul 15 '19 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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