Skip to content
KMyFirewall
Release 1.1.1 Download

The KMyFirewall Handbook

IPTables basics

An IPTables based firewall is made of three basic objects: rules, chains and tables. This chapter introduces each one.

Rules

The lowest level objects are rules, which perform the packet filtering or manipulation. A rule is made of several parts:

  • The table to which the rule should be added. If no table is defined, the rule is added to the filter table.
  • The chain to which the rule should be added, for example INPUT or FORWARD.
  • The filtering or manipulation instructions.
  • The target of the rule, which decides what should be done if the packet matches.

The most important targets are DROP, which drops the packet without further action, ACCEPT, which lets the packet pass the firewall, and LOG, which writes information about the matching packet to the system log.

Chains

Rules are organized in chains, which are ordered lists of rules. Some chains are built in and always available, such as INPUT and OUTPUT in the filter table.

Built in chains also have a default target, which decides what to do with a packet that did not match any rule in that chain.

For large configurations it is often useful to set up rules for packets that share some attributes. When you set up a firewall that also provides routing, you may want different rules for packets from the internal network and packets from the Internet. User defined chains make such configurations easier to manage.

User defined chains do not have a default target. They must be called by rules that use the chain name as their target. When a packet passes the whole user defined chain without matching, it is sent back to the chain that called it, right after the rule that sent it there.

Packets are compared with rules in the order they are defined. This means a packet is no longer tested after a rule matches, so pay attention to rule order. A wrong order can produce a bug that is very hard to find.

Tables

Because IPTables rules offer many ways to filter and manipulate packets, chains are organized in tables. Each table has its own set of built in chains.

  • The filter table is used for packet filtering, which is what most people think of when they talk about firewalls.
  • The nat table is used for network address translation, which changes source and destination attributes of packets. IP masquerading is the most common use.
  • The mangle table holds chains and rules that change other packet attributes or send packets to user space for processing. If you are not working on complicated requirements, you should not need it.
Reference

For a complete command reference, see the iptables manual page, then continue with IPTables usage.

Back to top