Understand iptable: tables chains rules

iptables tables chains rules fundamentals

    Iptables firewall is used to manage packet filtering and NAT rules.

    Iptables tool is used to manage the Linux firewall rules.

    This article explains how iptables is structured,and explains the fundamentals about iptables tables,chains and rules.

     On a high-level iptables might contain multiple tables.Tables might contain multiple chains.Chains can be built-in or user-defined.Chains might contain multiple rules.Rules are defined for packets.

    So the structure is:iptables  ->  tables  -> chains  ->  rules.

1.iptables tables and chains

Iptables has the following 4 built-in tables:

1)filter table

Filter table is default table for iptables.So if you don't define your own table,you'll be using filter table.Iptables's filter table has the following built-in chains:

  • INPUT chain - Incoming to firewall.For packets coming to the local server
  • OUTPUT chain - Outgoing from firewall.For packets generated locally and going out of the local server
  • FORWARD chain - Packet for another NIC on the local server.For packets routed through the local server.

2)nat table

Iptables has the following built-in chains:

  • PREROUTIGN chain - Alters packets before routing.i.e. Packet translation happens immediately after the packet comes to the system(and before routing).This helps to translate the destination ip address of the packets to something that matches the routing on the local server.This is used for DNAT(destination NAT).
  • POSTROUTING chain - Alters packets after routing.i.e. Packet translation happens when the packets are leaving the system.This helps to translate the source ip address of the packet to something that might match the routing on the destination server.This is used for SNAT (source NAT).
  • OUTPUT chain - NAT for locally generated packets on the firewall

3)mangel table

Iptables's Mangle table is for specialized packet alteration.This alters QOS bits in the TCP header.Mangle table has the following built-in chains.

  • PREROUTING chain
  • OUTPUT chain
  • FORWARD chain
  • INPUT chain
  • POSTROUTING chain

4)raw table

Iptables's Raw table is for configuration excemptions.Raw table has the following built-in chains.

  • PREROUTING chain
  • OUTPUT chain


2.iptables rules

Following are the key points to remember for the iptables rules:

  • Rules contain a criteria and a target
  • If the criteria is matched,it goes to the rules specified in the target (or) executes the special values mentioned in the target.
  • If the criteria is not matched,it moves on to the next rule


Target Values

Following are the possible special values that you can specify in the target:

  • ACCEPT - Firewall will accept the packet
  • DROP - Firewall will drop the packet
  • QUEUE - Firewall will pass the packet to the userspace
  • RETURN - Firewall will stop executing the next set of the rules in the current chain for this packet.The control will be returned to the calling chain.


If you do iptables --list (or) service iptables status,you'll see all the available firewall rules on your system.The following iptable example shows that there are no firewall rules defined on this system.As you see,it displays the default filter table,with the default input chain,forward chain,and output chain.

Understand iptable: tables chains rules_第1张图片


The rules in the iptables --list command output contains the following fields:

  • rules - Rule number within the particular chain
  • target - Special target variable that we discussed above
  • prot - Protocols,tcp,udp,icmp,etc
  • opt - Special options for that specific rule
  • source - Source ip-address of the packet
  • destination - Destination ip-address for the pacekt


你可能感兴趣的:(Understand iptable: tables chains rules)