Lesson 01 Packet Filtering: from iptables to nftables
Theme:
Lesson 01 · Slide 1

Lesson 01 — Packet Filtering

From iptables to nftables, beginner edition.

Linux Networking

In this lesson you will learn how the Linux kernel inspects and filters network packets using the netfilter framework, how to write and read iptables rules, what conntrack is and why it matters, and how the newer nftables tool improves on the classic toolchain.

Lesson 01 · Slide 2

What is a firewall on Linux?

  • A firewall = packet inspector + decider — it looks at each packet and decides: allow, drop, or rewrite?
  • On Linux, the inspector lives in the kernel: netfilter — a set of hooks deep inside the networking stack.
  • iptables/nftables are just front-ends — they load rules into netfilter. The kernel does the actual filtering.
  • No firewall daemon needs to run continuously — the rules are stored in kernel memory.

Plain English

Think of the Linux kernel as a post office. Every packet is a letter passing through. Netfilter is the sorting machine that can inspect, redirect, or shred any letter. iptables and nftables are the tools you use to program that sorting machine — they just write the instructions, they don't run them.

Lesson 01 · Slide 3

The 5 netfilter hooks

PRE- ROUTING INPUT FORWARD OUTPUT POST- ROUTING raw / mangle / nat mangle / filter mangle / filter raw / mangle / nat / filter mangle / nat A packet's journey through netfilter
Lesson 01 · Slide 4

Tables vs chains

iptables organises rules into tables (purpose) and chains (hook attachment points).

Table What it does Typical chains Example rule
filter Accept, drop, or reject packets — the default firewall table INPUT, FORWARD, OUTPUT -A INPUT -p tcp --dport 22 -j ACCEPT
nat Rewrite source or destination IP/port (SNAT, DNAT, MASQUERADE) PREROUTING, OUTPUT, POSTROUTING -t nat -A POSTROUTING -o eth0 -j MASQUERADE
mangle Modify packet headers (TTL, TOS, mark for routing) without rewriting addresses All 5 chains -t mangle -A PREROUTING -j MARK --set-mark 1
raw Bypass conntrack for specific traffic (performance) PREROUTING, OUTPUT -t raw -A PREROUTING -p udp --dport 53 -j NOTRACK
Notes: The filter table is the default — when you omit -t, you are working in filter. Rules are evaluated top-to-bottom; first match wins.
Lesson 01 · Slide 5

Stateful inspection (conntrack)

Client Server state=NEW state=NEW state=ESTABLISHED state=ESTABLISHED SYN SYN-ACK ACK DATA
Lesson 01 · Slide 6

iptables crash course

Three commands every sysadmin needs to know:

List all rules with packet/byte counters:

iptables -L -v -n

The -v flag shows interface and packet/byte counters. -n prevents slow DNS lookups — always use it.

Allow inbound SSH:

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Appends (-A) a rule to the INPUT chain: TCP on port 22 → ACCEPT. The -A adds to the end; use -I to insert at a specific position.

Set default policy to drop all inbound:

iptables -P INPUT DROP

The policy is the fallback when no rule matches. Set this after adding ACCEPT rules for needed services, or you will lock yourself out.

Lesson 01 · Slide 7

Reading a rule like a sentence

Anatomy of an iptables rule

iptables  -A INPUT  -p tcp  --dport 22  -m conntrack --ctstate NEW,ESTABLISHED  -j ACCEPT
  • iptables — the tool that writes rules into the kernel's netfilter tables
  • -A INPUT — Append to the INPUT chain (packets destined for this machine)
  • -p tcp — match only TCP packets
  • --dport 22 — match destination port 22 (SSH)
  • -m conntrack --ctstate NEW,ESTABLISHED — load the conntrack module and match packets in state NEW or ESTABLISHED
  • -j ACCEPT — jump to target ACCEPT (let the packet through)

Read it aloud: "For all TCP packets arriving at port 22 that are new or part of an established connection, accept them."

Lesson 01 · Slide 8

NAT in action

SNAT (outgoing): POSTROUTING rewrites SOURCE IP POSTROUTING src=10.0.0.5 src=203.0.113.10 → dst=8.8.8.8 DNAT (incoming): PREROUTING rewrites DEST IP PREROUTING src=8.8.8.8 → dst=203.0.113.10 dst=10.0.0.5
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

MASQUERADE is dynamic SNAT — it automatically uses the current IP of the outgoing interface. Ideal for dynamic IP addresses (home routers, VMs sharing a host NIC).

Lesson 01 · Slide 9

DROP vs REJECT

DROP — silent void REJECT — ICMP unreachable firewall (packet lost) firewall

DROP

Packet is silently discarded. The sender gets no response and must wait for a timeout. Harder to fingerprint your firewall, but slower failure for legitimate clients.

iptables -A INPUT -p tcp --dport 23 -j DROP

REJECT

Packet is discarded AND an ICMP "port unreachable" (or TCP RST) is sent back. Sender knows immediately the port is closed — faster failure, but reveals firewall presence.

iptables -A INPUT -p tcp --dport 23 -j REJECT \
  --reject-with tcp-reset
Lesson 01 · Slide 10

nftables — what changed and why

nftables replaced iptables as the recommended Linux firewall tool starting with kernel 3.13 (2014), and became the default in Debian 10 / RHEL 8.

Quick start

# List all tables and rules
nft list ruleset

# Load a complete ruleset from a file (atomic)
nft -f /etc/nftables.conf
Lesson 01 · Slide 11

iptables vs nftables — side by side

Task iptables nftables
Accept SSH (port 22) iptables -A INPUT -p tcp --dport 22 -j ACCEPT tcp dport 22 accept
Drop all ICMP iptables -A INPUT -p icmp -j DROP ip protocol icmp drop
NAT MASQUERADE iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE ip saddr 10.0.0.0/8 oifname "eth0" masquerade
Match a set of IPs Requires multiple -A rules or ipset module ip saddr { 192.168.1.0/24, 10.0.0.0/8 } accept
Reload entire ruleset atomically Not natively supported; use iptables-restore nft -f /etc/nftables.conf
IPv6 support Separate ip6tables command Same ruleset; use inet table for dual-stack

Both tools write rules into the same kernel netfilter subsystem — they can coexist but may conflict; pick one per production system.

Lesson 01 · Slide 12

Migration & gotchas

Lesson 01 · Slide 13

Recap

Lab time

In the lab you will: add, list, and delete iptables rules on a live VM; observe conntrack state transitions with conntrack -L; write an equivalent nftables ruleset; and test DROP vs REJECT behaviour with netcat.

Coming up: Lesson 02 — How Linux Decides Where Packets Go (routing tables, ip route, policy routing, and Linux as a router).
1 / ?