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.