Networking
Intermediate 14 min read Networking

Firewalls & Security Groups

Firewalls enforce who may talk to whom, either statelessly per packet or statefully per connection. Learn iptables and nftables fundamentals, default-deny policy, and how cloud security groups and network ACLs differ.

firewalliptablesnftablesstatefulsecurity groupnetwork ACLdefault deny
Introduction

What is this?

A firewall filters traffic based on rules. Stateless firewalls evaluate every packet in isolation, while stateful firewalls track connections and allow reply traffic automatically. On Linux, iptables and nftables implement packet filtering with chains of rules; in the cloud, security groups are stateful filters attached to instances and network ACLs are stateless filters attached to subnets.

Motivation

Why do you need it?

  • Defense in depth starts with restricting what can reach each service.
  • Most connectivity bugs — and many breaches — trace back to firewall rules.
  • iptables/nftables skills transfer directly to containers, Kubernetes and cloud networking.
  • Understanding stateful vs stateless explains why a security group works and a broken NACL does not.
Think about it

Mental model

Imagine

A building's security desk

Think of

A stateful firewall

When you enter, the desk records your badge. They let you in and, because they know you are inside, let you back out without a fresh check. A stateless firewall has no memory — it re-checks every door you pass, both ways.

Mechanics

How it works

  • Packets enter named chains — INPUT for traffic to the host, OUTPUT for traffic from it, FORWARD for routed traffic.
  • Each rule matches on attributes like source IP, destination port and protocol, then jumps to a target: ACCEPT, DROP or REJECT.
  • Rules are evaluated in order; the first match wins, so order and default policy matter.
  • Stateful tracking (conntrack) marks connections as NEW, ESTABLISHED or RELATED so reply packets are allowed automatically.
  • A default policy of DROP plus explicit ACCEPT rules is the secure baseline; REJECT actively refuses instead of silently dropping.

Packet flow through iptables filter chains.

Packet arrives
Conntrack lookup
Chain traversal
ACCEPT
DROP / REJECT
Reference

Key commands

iptables -L -n -v

List filter rules with counters, skipping DNS lookups.

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

Append a rule accepting HTTPS traffic to the INPUT chain.

iptables -I INPUT 1 -s 1.2.3.4 -j DROP

Insert a drop rule for a source IP at the top of the chain.

ufw status verbose

Show the Ubuntu Uncomplicated Firewall profile and its rules.

nft list ruleset

Dump the complete nftables ruleset (the modern successor to iptables).

Hands-on

Examples

A minimal default-deny host firewall

A minimal default-deny host firewall
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -s 10.0.0.0/8 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -L -n -v

The default INPUT policy is DROP, then four ACCEPT rules open SSH for the private range only, HTTP and HTTPS for everyone, plus loopback and established connections. Listing with -L -n -v shows each rule's packet and byte counters.

Real world

In production

  • Host firewalls (ufw, firewalld, iptables) restrict which ports each server exposes.
  • Security groups define per-service ingress in the cloud and are stateful by design.
  • Network ACLs add a stateless subnet boundary in front of security groups.
  • Service meshes and Kubernetes NetworkPolicies push firewall logic down to the pod level.
Guidelines

Best practices

  • Default to deny: allow only the ports and source ranges a service actually needs.
  • Restrict admin ports (SSH 22, RDP 3389) to your office or VPN CIDR, never 0.0.0.0/0.
  • Prefer stateful security groups for the cloud and conntrack for host firewalls.
  • Persist rules — iptables-save to a file and restore on boot, or use ufw/firewalld.
  • Audit rules regularly: remove stale allow rules and open ports.
Watch out

Common mistakes

  • Allowing SSH from 0.0.0.0/0 and leaving default credentials.
  • Blocking established/reply traffic and breaking every outbound request (missing conntrack rule).
  • Appending rules after a default DROP policy and assuming they apply to the wrong chain.
  • Testing a firewall change from a session that the change itself blocks — no keepalive fallback.
Root cause analysis

Common issues

Error
Connection timeout to a service
Root cause

A DROP rule or a stateful cloud security group blocking inbound SYN.

Fix

List rules with iptables -L -n -v and watch counters; verify the cloud security group allows the port and source.

Error
Connection refused immediately
Root cause

No listener (REJECT from firewall or service down).

Fix

Check ss -tulpn for the listener; if it's up, look for iptables REJECT targets in the log.

Error
Outbound works, inbound works, but clients can't reach the app
Root cause

FORWARD chain or subnet-level NACL drops routed traffic.

Fix

Check the FORWARD chain policy/rules and the network ACL on the subnet (stateless NACLs need both directions allowed).

When things break

Debugging tips

  • Check counters with iptables -L -n -v to see which rule is dropping your packets.
  • Use tcpdump on the interface to compare packets arriving vs packets allowed.
  • Temporarily set policy ACCEPT to isolate a firewall bug from an app bug.
  • Look for DROP vs REJECT: DROP causes timeouts, REJECT causes immediate connection refused.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/3
1

What is the key difference between a stateful and a stateless firewall?

2

By default, an AWS Security Group allows all inbound traffic.

3

In iptables, which chain handles traffic addressed to the local host?

Quick reference

Cheat sheet

iptables essentials
iptables -A INPUT -p tcp --dport 443 -j ACCEPTAllow HTTPS inbound
iptables -I INPUT 1 -s 10.0.0.5 -j DROPDrop a specific source
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTAllow reply traffic
iptables -P INPUT DROPSet default policy to deny
iptables-save > /etc/iptables.rulesPersist the ruleset
nft list rulesetView the nftables ruleset
Common open ports
22/tcpSSH — restrict to office/VPN CIDR
80, 443/tcpHTTP, HTTPS — the public web edge
53/udp + tcpDNS queries and zone transfers
5432/tcpPostgreSQL — internal only
6443/tcpKubernetes API server — control-plane only
Go deeper

Official documentation