Most commands you run with iptables are going to look something like this:
iptables [-t table] [mode] [chain] [rulenum] [rule-specification] [options]
Let's break this down part by part:
table
tells iptables which part of the firewall you want this command to work with. There are multiple tables for various purposes:
mode
tells iptables what kind of action you want this command to do. Here are the most useful ones:
chain
tells iptables which chain you want this command to work with. There are five chains, but not all of them exist in all tables:
rulenum
gives a numerical position in the chain. Rules in each chain are numbered starting from 1. You can use the rulenum
with the -I
mode to tell iptables where in the chain to insert the rule and with the -R
mode to tell it which rule to replace. You can also use it with -D
to tell iptables which rule to delete.rule-specification
gives iptables the conditions and the action that make up the rule you're giving it. Obviously you need this when inserting, appending, or replacing a rule, to tell iptables what new information it should put in the table, but you can also use it to specify a rule to delete. If used with the -D
option, iptables will search the chain for a rule that exactly matches the specification you gave and delete it if it finds one.options
can be any of a few miscellaneous options:
-L
, list the counts of how many packets/bytes have been handled by each rule or chain since IPTables started.-L
command.command
command
to load any extra modules needed for the rule. It's fairly rare to actually need this option.An iptables rule specification consists of some number of parameters given as options to the iptables command. Remember that each rule in a firewall consists of some number of conditions and an action to be taken if all the conditions match. So when specifying a rule, one of the parameters is supposed to give the action and all the rest give conditions. It's usually good practice to put the action parameter last, since it makes it clear when reading the rule specification that the conditions are checked first, although iptables isn't picky about how the parameters are ordered. (One exception: a module must be loaded before any of its commands are used. I'll discuss this later.)
So what are these parameters?
Most rules should have one action parameter. IPTables will accept rules that have no action, but obviously they won't do anything. However, packet and byte counters are still maintained for rules with no action, so you can use actionless rules to see how many packets are matching a particular set of conditions.
-j
or --jump
is that if the target is a user-defined chain and none of its rules match, IPTables will skip the rest of the current chain.All the condition parameters are optional. By default, if no conditions are specified, the rule will match all packets, so use conditions to restrict the set of packets you want the rule to match.
Most conditions can be inverted by using an exclamation mark after the condition option but before the value; for example, -p ! tcp
will match any packet that is not a TCP packet.
tcp
, udp
, or icmp
, but any protocol listed in /etc/protocols
(either the lowercase protocol name, column 1, or the numeric code, column 2) is acceptable./mask
is optional, but if given, it specifies which bits should be considered in the match; for example, 255.255.255.0
is 11111111111111111111111100000000
in binary, so only the leftmost 24 bits of the address will be considered. Keep in mind that source IP addresses can be faked.--source
. Keep in mind that destination IP addresses can be faked, although it's harder to do so than with source IP addresses.eth0
, and a wireless networking card, which corresponds to an interface named wlan0
. A server might have two ethernet cards with two connected cables, eth0
for the internet at large and eth1
for a private network. All Linux computers also have an interface called lo
, the loopback interface, which is usable only for sending packets from that computer to itself. Note that if you give the --in-interface
option for a chain which only handles outgoing packets (namely OUTPUT or POSTROUTING), iptables will complain!--in-interface
, if you give this option for a chain which only handles incoming packets (INPUT or PREROUTING), iptables will complain!-f
condition exists to allow you to catch the rest of the packet, although in practice it's not usually necessary to do this. Note that unlike most other parameters, if you want to negate this condition you put the exclamation mark before the -f
.Just like the builtin conditions, these are optional and can be used to restrict the set of packets that the rule matches. The difference is that these conditions aren't built in to the main iptables program; rather, they're programmed in modules which you will need to load before you can use the commands. There are two ways to load a module:
-p [protocol]
condition, if there is a module with the same name as the protocol, it will be automatically loaded-m [module]
parameterNote that since the parts of the rule specification are evaluated in order from left to right, if you're going to use a module, you need to give the option to load it before you use any of its options. For example, -p tcp --dport 1779
will work, but --dport 1779 -p tcp
will give an error.
There are a lot of modules available, and they're all described in the iptables man page, but here's a selection of the most useful ones:
--limit 3/hour
(which is the default).--limit
parameter, if you specify a number higher than the number in --limit
.--source-port
and similar options). Example: --source-ports 1907:1909,2156,7703
.--source-ports
, the list of ports is a comma-separated list of single port numbers and/or colon-separated ranges.--source-ports
, the list of ports is a comma-separated list of single port numbers and/or colon-separated ranges.[state]
can actually be a comma-separated list of states, in which case any packet that has any one of those states will match the condition. There are four possible states:
--state
condition is important so that when a program on your computer sends an outgoing packet to start a connection, the responses from the other computer aren't blocked by the firewall. For that reason, you'll see a rule like this near the beginning of every firewall configuration:
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
--source-port 5509
or --source-port 7795:7799
.--source-port
. This is by far the most useful option from the tcp
module.[mask]
specifies which flags to check, and the second one [comp]
specifies which should be set.--tcp-flags SYN,ACK,FIN,RST SYN
, which will check the SYN, ACK, FIN, and RST flags and match if, out of those, only the SYN flag is set. This is useful for picking out the packets that start new connections.--source-port 5509
or --source-port 7795:7799
.--source-port
. This is by far the most useful option from the udp
module.