Skip to content

๐Ÿงฑ iptables Introduction โ€‹

What is iptables? โ€‹

iptables is like the security guard at your front door โ€” everyone entering or leaving must pass through inspection. The guard has a list (rule table): who can enter, who cannot, which door to use, which floor to go to โ€” everything is recorded.

iptables is a firewall tool built into the Linux kernel, operating at the network layer (Layer 3/4), capable of filtering, forwarding, and modifying packets entering and leaving a server.

Core Concept: Tables โ€‹

iptables has several "tables," each responsible for different types of operations:

TablePurposeUsage Frequency
filterFilter packets (allow/deny)โญโญโญ Most commonly used
natNetwork Address Translation (NAT)โญโญ Commonly used
mangleModify packet headersโญ Less commonly used
rawConnection tracking exemptionRarely used

Core Concept: Chains โ€‹

Each table contains several "chains," like different posts for the security guard:

  • ๐Ÿ“ฅINPUT โ€” Entry post: checks packets coming into the local machine
  • ๐Ÿ“คOUTPUT โ€” Exit post: checks packets sent from the local machine
  • ๐Ÿ”„FORWARD โ€” Forwarding post: checks packets passing through the machine but not destined for it (router scenarios)
  • ๐Ÿ”€PREROUTING โ€” Pre-processing: handled before routing decisions (used for DNAT)
  • ๐Ÿ”€POSTROUTING โ€” Post-processing: handled after routing decisions (used for SNAT)

Packet Flow โ€‹

Understanding how packets flow through iptables is like understanding how a delivery passes through various checkpoints:

text
Packet enters the server
    โ”‚
    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  raw         โ”‚  PREROUTING (before connection tracking)
โ”‚  table       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  mangle      โ”‚  PREROUTING (modify packet headers)
โ”‚  table       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  nat         โ”‚  PREROUTING (DNAT: modify destination address)
โ”‚  table       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ–ผ
   Routing decision: for local machine? Or forward?
   โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”
   โ–ผ         โ–ผ
  Local    Forward โ†’ FORWARD โ†’ POSTROUTING โ†’ Out
   โ”‚
   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  filter      โ”‚  INPUT (filter: allow or deny?)
โ”‚  table       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ–ผ
   Application receives data