Command Options for IPTables

Rules for filtering packets are created using the iptables command. The following aspects of the packet are most often used as criteria:

Refer to and for more information about specific options that address these aspects of a packet.

The options used with specific iptables rules must be grouped logically, based on the purpose and conditions of the overall rule, for the rule to be valid. The remainder of this section explains commonly-used options for the iptables command.

Structure of IPTables Command Options

Many iptables commands have the following structure:

iptables [-t <table-name>] <command> <chain-name> \
   <parameter-1> <option-1> \
   <parameter-n> <option-n>

<table-name> - Specifies which table the rule applies to. If omitted, the filter table is used.

<command> - Specifies the action to perform, such as appending or deleting a rule.

<chain-name> - Specifies the chain to edit, create, or delete.

<parameter>-<option> pairs - Parameters and associated options that specify how to process a packet that matches the rule.

The length and complexity of an iptables command can change significantly, based on its purpose.

For example, a command to remove a rule from a chain can be very short:

iptables -D <chain-name> <line-number>

In contrast, a command that adds a rule which filters packets from a particular subnet using a variety of specific parameters and options can be rather long. When constructing iptables commands, it is important to remember that some parameters and options require further parameters and options to construct a valid rule. This can produce a cascading effect, with the further parameters requiring yet more parameters. Until every parameter and option that requires another set of options is satisfied, the rule is not valid.

Type iptables -h to view a comprehensive list of iptables command structures.

Command Options

Command options instruct iptables to perform a specific action. Only one command option is allowed per iptables command. With the exception of the help command, all commands are written in upper-case characters.

The iptables commands are as follows:

IPTables Parameter Options

Certain iptables commands, including those used to add, append, delete, insert, or replace rules within a particular chain, require various parameters to construct a packet filtering rule.

If the -i parameter is used but no interface is specified, then every interface is affected by the rule.

IPTables Match Options

Different network protocols provide specialized matching options which can be configured to match a particular packet using that protocol. However, the protocol must first be specified in the iptables command. For example, -p <protocol-name> enables options for the specified protocol. Note that you can also use the protocol ID, instead of the protocol name. Refer to the following examples, each of which have the same effect:

iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
iptables -A INPUT -p 5813 --icmp-type any -j ACCEPT

Service definitions are provided in the /etc/services file. For readability, it is recommended that you use the service names rather than the port numbers.

Secure the /etc/services file to prevent unauthorized editing. If this file is editable, crackers can use it to enable ports on your machine you have otherwise closed. To secure this file, type the following commands as root:

chown root.root /etc/services
chmod 0644 /etc/services
chattr +i /etc/services

This prevents the file from being renamed, deleted or having links made to it.

TCP Protocol

These match options are available for the TCP protocol (-p tcp):

To configure this option, use a network service name (such as www or smtp); a port number; or a range of port numbers.

To specify a range of port numbers, separate the two numbers with a colon (:). For example: -p tcp --dport 3000:3200. The largest acceptable valid range is 0:65535.

Use an exclamation point character (!) after the --dport option to match all packets that do not use that network service or port.

To browse the names and aliases of network services and the port numbers they use, view the /etc/services file.

The --destination-port match option is synonymous with --dport.

For example, an iptables rule that contains the following specification only matches TCP packets that have the SYN flag set and the ACK and FIN flags not set:

--tcp-flags ACK,FIN,SYN SYN

Use the exclamation point character (!) after the --tcp-flags to reverse the effect of the match option.

UDP Protocol

These match options are available for the UDP protocol (-p udp):

For the --dport and --sport options, to specify a range of port numbers, separate the two numbers with a colon (:). For example: -p tcp --dport 3000:3200. The largest acceptable valid range is 0:65535.

ICMP Protocol

The following match options are available for the Internet Control Message Protocol (ICMP) (-p icmp):

Additional Match Option Modules

Additional match options are available through modules loaded by the iptables command.

To use a match option module, load the module by name using the -m <module-name>, where <module-name> is the name of the module.

Many modules are available by default. You can also create modules to provide additional functionality.

The following is a partial list of the most commonly used modules:

When used in conjunction with the LOG target, the limit module can prevent a flood of matching packets from filling up the system log with repetitive messages or using up system resources.

Refer to for more information about the LOG target.

The limit module enables the following options:

Periods can be specified in seconds, minutes, hours, or days.

If a number and time modifier are not used, the default value of 3/hour is assumed.

These connection states can be used in combination with one another by separating them with commas, such as -m state --state INVALID,NEW.

Refer to the iptables man page for more match options available through modules.

Target Options

When a packet has matched a particular rule, the rule can direct the packet to a number of different targets which determine the appropriate action. Each chain has a default target, which is used if none of the rules on that chain match a packet or if none of the rules which match the packet specify a target.

The following are the standard targets:

In addition, extensions are available which allow other targets to be specified. These extensions are called target modules or match option modules and most only apply to specific tables and situations. Refer to for more information about match option modules.

Many extended target modules exist, most of which only apply to specific tables or situations. Some of the most popular target modules included by default in Community Enterprise Linux are:

Additional options can be used after the LOG target to specify the way in which logging occurs:

Other target extensions, including several that are useful for IP masquerading using the nat table, or with packet alteration using the mangle table, can be found in the iptables man page.

Listing Options

The default list command, iptables -L [<chain-name>], provides a very basic overview of the default filter table's current chains. Additional options provide more information:

The following examples illustrate the use of several of these options. Note the difference in the byte display by including the -x option.

~]# iptables -L OUTPUT -v -n -x
Chain OUTPUT (policy ACCEPT 64005 packets, 6445791 bytes)
    pkts      bytes target     prot opt in     out     source               destination
    1593   133812 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
~]# iptables -L OUTPUT -v -n
Chain OUTPUT (policy ACCEPT 64783 packets, 6492K bytes)
    pkts bytes target     prot opt in     out     source               destination
    1819  153K ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
~]#