๐งฑ 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:
| Table | Purpose | Usage Frequency |
|---|---|---|
| filter | Filter packets (allow/deny) | โญโญโญ Most commonly used |
| nat | Network Address Translation (NAT) | โญโญ Commonly used |
| mangle | Modify packet headers | โญ Less commonly used |
| raw | Connection tracking exemption | Rarely 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