The !
operator in iptables
allows you to invert a test for an IP address, protocol, or interface. This can be useful if you want to specify a rule that applies to all IP addresses, protocols, or interfaces except for a specific one.
For example, to specify a rule that applies to all IP addresses except for 192.0.2.1
, you can use the -s
option with the !
operator:
iptables -A INPUT -s ! 192.0.2.1 -j ACCEPT
This will accept all incoming connections except for those from the IP address 192.0.2.1
.
To specify a rule that applies to all protocols except for TCP, you can use the -p
option with the !
operator:
iptables -A INPUT -p ! tcp -j ACCEPT
This will accept all incoming connections except for those using the TCP protocol.
To specify a rule that applies to all interfaces except for eth0
, you can use the -i
option with the !
operator:
iptables -A INPUT -i ! eth0 -j ACCEPT
This will accept all incoming connections except for those coming in through the eth0
interface.
Note that the !
operator must be placed immediately before the test that it is inverting. For example, the following command will not work as intended:
iptables -A INPUT -s !192.0.2.1 -j ACCEPT
Instead, it will match all incoming connections from IP addresses that do not start with 192.0.2.1
.