StackPractices
advanced By Mathias Paulenko

Advanced iptables Firewall Rules and Hardening

Harden a Linux server with iptables: DDoS limits, connection tracking, IPv6, packet logging, fail2ban, nftables migration, and port knocking.

Topics: security

Overview

A fresh VPS starts getting scanned within minutes — port 22 probes, HTTP crawlers, random UDP noise. On that machine, “write a few iptables rules” stops being a tutorial exercise and becomes the difference between noticing an attack in your logs and never seeing it at all.

This recipe collects the rulesets that go beyond the basics: connection tracking, rate-limited SSH, DDoS limits, IPv6 parity, packet logging, fail2ban, the nftables migration path, and port knocking. If you need the everyday fundamentals first — NAT, port forwarding, blocking a handful of IPs — Configure iptables Firewall Rules with Bash covers them; this recipe assumes that baseline and builds the hardening layer on top.

Every script below is also downloadable in the companion repository.

When to Use

  • Hardening a VPS or bare-metal server that faces the public internet.
  • You need rules that survive a reboot — and that fail closed, not open.
  • You want brute-force pressure off SSH without paying for a WAF or a hosted filter.
  • You’re migrating legacy iptables policies to nftables without rewriting everything in one sitting.

When NOT to Use

  • Your upstream already filters the traffic. Cloud security groups or a perimeter firewall do the same job with less maintenance — keep host rules as the second layer, not the only one.
  • You want the least amount of firewall possible. ufw (Ubuntu/Debian) or firewalld (RHEL/Fedora) wrap most of this in a couple of commands.
  • You’re starting fresh on a modern distro. Write nftables rules directly instead of learning syntax that’s already legacy.
  • You need application-layer filtering — HTTP paths, TLS inspection, per-route rate limits. That’s a reverse proxy or WAF job, not netfilter’s.

Solution

Baseline ruleset

The foundation: default-deny inbound, allow outbound, let established traffic back in, rate-limit SSH, and persist the result.

#!/usr/bin/env bash
set -euo pipefail

# Reset rules and counters
iptables -F
iptables -X
iptables -Z

# Default policies: deny inbound/forward, allow outbound
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# Loopback — local services depend on it
iptables -A INPUT -i lo -j ACCEPT

# Replies to connections we initiated
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# SSH, rate-limited: >4 new connections per 60s from one IP get dropped
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Web traffic
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# Persist (iptables-persistent writes here; create the dir if missing)
mkdir -p /etc/iptables
iptables-save > /etc/iptables/rules.v4

echo "Firewall rules applied"

Two warnings before this goes anywhere near a remote box. First, apply it while you still have an open session — if the policy drops your SSH packet, you want a shell alive to fix it. Second, iptables -P INPUT DROP takes effect the instant it runs; the SSH rule two lines later is what saves you, so never split them across separate commands.

IPv6 parity with ip6tables

IPv4 rules do nothing for IPv6 traffic — a server listening on :: is wide open even with a perfect IPv4 ruleset. Mirror the same structure with ip6tables, plus one critical addition: ICMPv6 isn’t optional — neighbor discovery depends on it.

#!/usr/bin/env bash
set -euo pipefail

ip6tables -F
ip6tables -X
ip6tables -Z

ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
ip6tables -P OUTPUT ACCEPT

ip6tables -A INPUT -i lo -j ACCEPT
ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# ICMPv6 is required — neighbor discovery, path MTU, MLD all break without it
ip6tables -A INPUT -p icmpv6 -j ACCEPT

ip6tables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set
ip6tables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
ip6tables -A INPUT -p tcp --dport 22 -j ACCEPT

ip6tables -A INPUT -p tcp --dport 80 -j ACCEPT
ip6tables -A INPUT -p tcp --dport 443 -j ACCEPT

mkdir -p /etc/iptables
ip6tables-save > /etc/iptables/rules.v6

echo "IPv6 firewall rules applied"

DDoS mitigation with connection limits

connlimit caps concurrent connections per source; SYNPROXY offloads SYN-handshake validation to the kernel so a flood never reaches your service. Neither replaces a scrubbing service under a real volumetric attack, but they absorb the background noise — slow crawlers, port scanners, lazy botnets — that otherwise eats sockets.

#!/usr/bin/env bash
set -euo pipefail

# Cap concurrent connections per single IP
iptables -A INPUT -p tcp -m connlimit --connlimit-above 50 --connlimit-mask 32 -j DROP

# New connections: accept up to 20/s with a 40-packet burst, drop the rest
iptables -A INPUT -p tcp -m conntrack --ctstate NEW -m limit --limit 20/s --limit-burst 40 -j ACCEPT
iptables -A INPUT -p tcp -m conntrack --ctstate NEW -j DROP

# Spoofed or malformed packets never belong to a valid connection
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP

# SYNPROXY (kernel 3.12+): validate handshakes without holding sockets
iptables -t raw -A PREROUTING -p tcp -m tcp --syn -j CT --notrack
iptables -A INPUT -p tcp -m tcp --syn -m conntrack --ctstate UNTRACKED,INVALID -j SYNPROXY --sack-perm --timestamp --wscale 7 --mss 1460
iptables -A INPUT -p tcp -m tcp --syn -m conntrack --ctstate UNTRACKED,INVALID -j DROP

# ICMP: enough for diagnostics, not enough for a ping flood
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 3 -j ACCEPT
iptables -A INPUT -p icmp -j DROP

echo "DDoS mitigation rules applied"

Logging dropped packets

A silent DROP hides both attacks and your own misconfigurations. Log drops at a capped rate — without the limit match, one port scan fills your disk — and route them to their own file with rsyslog so journalctl stays readable.

#!/usr/bin/env bash
set -euo pipefail

# Log drops, rate-limited: 5/min with a 10-packet burst
iptables -A INPUT -m limit --limit 5/min --limit-burst 10 -j LOG \
    --log-prefix "iptables-DROP: " \
    --log-level 4 \
    --log-ip-options \
    --log-tcp-options

# Everything still falls through to the default DROP policy

# Read the drops
# journalctl -k | grep "iptables-DROP"
# or: dmesg | grep "iptables-DROP"

# Dedicated log file via rsyslog
cat > /etc/rsyslog.d/10-iptables.conf << 'EOF'
:msg, contains, "iptables-DROP" /var/log/iptables.log
& stop
EOF
systemctl restart rsyslog

fail2ban integration

Rate limiting slows brute force down; fail2ban removes repeat offenders entirely. Its iptables-multiport action creates a dedicated chain (f2b-sshd) and inserts DROP rules for IPs that trip a jail, so your hand-written rules stay untouched.

#!/usr/bin/env bash
set -euo pipefail

# Install: apt install fail2ban  OR  yum install fail2ban

cat > /etc/fail2ban/jail.local << 'EOF'
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
banaction = iptables-multiport

[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
backend = systemd

[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log

[recidive]
enabled = true
logpath = /var/log/fail2ban.log
bantime = 86400
findtime = 86400
maxretry = 5
EOF

systemctl enable fail2ban
systemctl start fail2ban

# Inspect
fail2ban-client status sshd

# Manual ban/unban
fail2ban-client set sshd banip 198.51.100.1
fail2ban-client set sshd unbanip 198.51.100.1

# See the generated rules: iptables -L f2b-sshd -n --line-numbers

The recidive jail is worth keeping even if you disable the others — it bans IPs that get banned repeatedly, which is a decent proxy for “this one isn’t going away”.

Migrating to nftables

iptables is legacy — most modern distros already run it on top of an nftables backend. When you’re ready to move, translate the saved ruleset instead of rewriting it, then clean up the result by hand.

#!/usr/bin/env bash
set -euo pipefail

# Translate existing rules
iptables-restore-translate -f /etc/iptables/rules.v4 > /etc/nftables.conf

# Or write nftables directly — the equivalent of the baseline ruleset
cat > /etc/nftables.conf << 'EOF'
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;

        iif "lo" accept
        ct state established,related accept
        ct state invalid drop

        # ICMP on both families — echo-request only, rate-limited
        icmp type echo-request limit rate 1/second accept
        icmpv6 type echo-request limit rate 1/second accept

        tcp dport 22 ct state new limit rate 4/minute accept
        tcp dport { 80, 443 } accept

        limit rate 5/minute log prefix "nft-DROP: " level warn
        drop
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
    }

    chain output {
        type filter hook output priority 0; policy accept;
    }
}
EOF

nft -f /etc/nftables.conf
systemctl enable nftables
systemctl start nftables

nft list ruleset

Notice what one inet table buys you: IPv4 and IPv6 rules in a single ruleset — no parallel ip6tables file to keep in sync.

Port knocking for hidden services

Port knocking keeps SSH closed to everyone until a client hits a secret sequence of ports. It’s security through obscurity, so treat it as a complement — the daemon only opens the port, SSH still has to be hardened with keys and rate limits. See SSH Key Management in Bash for that half.

#!/usr/bin/env bash
set -euo pipefail

# Requires knockd: apt install knockd

cat > /etc/knockd.conf << 'EOF'
[openSSH]
    sequence    = 7000,8000,9000
    seq_timeout = 15
    tcpflags    = syn
    start_command = iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
    cmd_timeout   = 30
    stop_command  = iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT

[closeSSH]
    sequence    = 9000,8000,7000
    seq_timeout = 15
    tcpflags    = syn
    start_command = iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
EOF

systemctl enable knockd
systemctl start knockd

# Client side: knock first, then connect
# knock -v server-ip 7000 8000 9000 && ssh user@server-ip
# knock -v server-ip 9000 8000 7000   # close after you leave

Explanation

iptables evaluates rules through tables (filter for policy, nat for address translation, raw for exemptions, mangle for packet rewriting), each containing chains (INPUT, OUTPUT, FORWARD, plus PREROUTING/POSTROUTING outside the local machine). Every packet walks a chain top to bottom; the first matching rule wins, and if nothing matches, the chain’s default policy decides. That’s why ordering matters so much: the ESTABLISHED,RELATED accept must come before any broad accept, and the SSH rule must exist before the INPUT DROP policy can be reached safely.

flowchart diagram: Incoming packet

Most of the heavy lifting in these scripts comes down to two kernel modules. conntrack attaches each packet to a connection state, which is what lets a DROP inbound policy coexist with outbound traffic — replies ride back on ESTABLISHED. recent keeps a per-IP timestamp list under /proc/net/xt_recent/, which is how the SSH rate limit counts attempts without any daemon running. When you move to nftables, both ideas survive as ct state and native limit rate expressions — the syntax changes, the model doesn’t.

Variants

ApproachToolComplexityUse When
Manual rulesetiptables / ip6tablesMediumExisting servers, scripts you already maintain
Modern rulesetnftMediumNew distros; one ruleset covers IPv4 and IPv6
Simple wrapperufwLowUbuntu/Debian, quick hardening
Zone-based wrapperfirewalldLowRHEL/CentOS/Fedora, dynamic zones
Upstream filteringSecurity groups / NACLsLowCloud VMs — first line of defense

For container-to-container filtering the calculus changes again — see Docker Network Isolation when the workloads live in containers rather than on the host.

What Works

  1. Default deny, allow explicitly. A DROP policy turns every forgotten rule into a support ticket, not a breach — the failure mode you want.
  2. Persist only after testing. iptables-save writes whatever is live, including the mistake that locks you out. Apply, verify connectivity from a second session, then save.
  3. Rate-limit SSH and log drops. Brute force is constant background noise; -m recent plus a capped LOG rule turns it into a metric instead of a threat.
  4. Keep IPv6 in lockstep. Every rule you write for iptables needs an ip6tables twin, or a single inet table in nftables. An open IPv6 path makes the IPv4 ruleset decoration.
  5. Layer the defenses. Host iptables behind cloud security groups, fail2ban on top of rate limits — each layer covers the blind spots of the one below.

Common Mistakes

  1. Forgetting established connections. Without the conntrack rule, every outbound request — DNS, apt, NTP — dies waiting for a reply the firewall drops.
  2. Setting INPUT DROP before the SSH rule. The policy applies instantly; the moment it runs, your session is gone. Console access or a reboot is the way back.
  3. Logging without a rate limit. One nmap sweep against an unthrottled LOG target fills the disk — always pair it with -m limit.
  4. Flushing rules to “fix” connectivity. -F leaves the DROP policy active, which is worse: everything still drops, and your accepts are gone too. Reset policies to ACCEPT explicitly if you need a clean slate.
  5. Assuming --log-uid works on INPUT. It only has meaning for locally generated traffic on OUTPUT — on inbound packets the field logs nothing useful.

See Also

Frequently Asked Questions

What is the difference between iptables and nftables?

iptables is the legacy frontend; nftables is its modern replacement with a unified syntax and one inet table that handles IPv4 and IPv6 together. On most current distros, iptables commands already run against an nftables backend through a compatibility layer — which is why learning nft directly is the better long-term bet.

How do I persist iptables rules across reboots?

Run iptables-save > /etc/iptables/rules.v4 (and ip6tables-save for rules.v6), then restore at boot with the iptables-persistent package or a small systemd oneshot calling iptables-restore. Always save after verifying — persistence freezes whatever is currently live, including mistakes.

How do I test firewall rules without locking myself out?

Keep one verified SSH session open while you apply rules from a second terminal, or schedule iptables-restore of a known-good ruleset via at or cron in a few minutes as a dead man's switch. If the new ruleset kills your session, the scheduled restore brings it back.

Should I DROP or REJECT external traffic?

DROP for internet-facing rules — no response means slower scans and less information leaked. REJECT makes sense on internal networks, where clients benefit from an immediate "connection refused" instead of waiting out a timeout.

Can fail2ban replace a hand-written iptables ruleset?

No — they do different jobs. fail2ban bans IPs that misbehave at the application layer by inserting temporary rules into its own chains; your baseline ruleset defines the standing policy. Use fail2ban on top of a solid default-deny ruleset, not instead of one.