Build a least-privilege Linux firewall with nftables

8 steps · about 2 h 28 min · Intermediate

  1. Learn the nftables rule model (guide, nftables wiki, 18 min)

    Tables, chains, hooks and rules replace assumptions carried over from older firewall tools.

  2. Choose hooks and default policies (guide, nftables wiki, 20 min)

    Base-chain placement and policy determine what traffic is evaluated and what happens when no rule matches.

  3. Match interfaces protocols and state (guide, nftables wiki, 20 min)

    Precise metadata matches keep rules narrow and readable instead of opening broad address ranges.

  4. Preserve established connections (guide, nftables wiki, 18 min)

    Connection tracking lets reply traffic continue while new inbound connections remain controlled.

  5. Group related addresses and ports (guide, nftables wiki, 18 min)

    Named sets reduce duplicated rules and make later service changes safer to review.

  6. Log useful drops without flooding (guide, nftables wiki, 16 min)

    Rate-limited logging provides evidence while avoiding a noisy rule that overwhelms the journal.

  7. Validate and apply a ruleset (guide, nftables wiki, 20 min)

    Rule-management commands show how to inspect, add and remove policy changes deliberately.

  8. Trace packets and prove the policy (guide, nftables wiki, 18 min)

    Tracing shows the exact chain and verdict for a test packet so verification is based on evidence.