1

I'm building a LAN to WLAN router and I need setting up NAT using iptables.

I don't get the description of the -i and -o command line switches (emphasis mine) in the MAN page:

[!] -i, --in-interface name Name of an interface via which a packet was received (only for packets entering the INPUT, FORWARD and PREROUTING chains). When the "!" argument is used before the interface name, the sense is inverted. If the interface name ends in a "+", then any interface which begins with this name will match. If this option is omitted, any interface name will match.

[!] -o, --out-interface name Name of an interface via which a packet is going to be sent (for packets entering the FORWARD, OUTPUT and POSTROUTING chains). When the "!" argument is used before the interface name, the sense is inverted. If the interface name ends in a "+", then any interface which begins with this name will match. If this option is omitted, any interface name will match.

The position of the optional ! seems to be before the hyphen. But from the description I understand it should be before the interface name and not before the switch itself.

Is it

iptables !-i eth0

or

iptables -i !eth0

I was not able to find examples using this and I'd like to get it right.

I have tried both and none of them seems to work:

Using !-i seems to replace that with the last command starting with i that I used (?):

pi@raspberrypi:~ $ sudo iptables -t nat -A POSTROUTING !-i eth0 -o wlan+ -j MASQUERADE
sudo iptables -t nat -A POSTROUTING ifconfig eth0 -o wlan+ -j MASQUERADE
Bad argument `ifconfig'
Try `iptables -h' or 'iptables --help' for more information.
pi@raspberrypi:~ $ !-i
ifconfig

Using !eth0 gives "event not found":

pi@raspberrypi:~ $ sudo iptables -t nat -A POSTROUTING -i !eth0 -o wlan+ -j MASQUERADE
-bash: !eth0: event not found

closed as off-topic by Milliways, Steve Robillard Aug 30 '16 at 11:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Milliways, Steve Robillard
If this question can be reworded to fit the rules in the help center, please edit the question.

2

! is a special bash character for history expansion and has to be escaped.

The exclamation mark needs to be escaped in single quotes ' and the position is before the interface name, not before the argument.

So your command becomes

sudo iptables -t nat -A POSTROUTING -i '!'eth0 -o wlan+ -j MASQUERADE

Note that POSTROUTING and -i don't work together, so it has to be fixed to

sudo iptables -t nat -A POSTROUTING -o wlan+ -j MASQUERADE

anyway and you got rid of the whole problem.

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