DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · SECURITY · 41 OF 187

Linux Firewalls: iptables, nftables, Security Groups

How packet filtering works and how to reason about it: netfilter hooks, tables, chains and rules, drop versus reject measured with nftables (drop: curl waited 3.0 s while the rule counted 3 SYN packets; reject: failed in 0.12 ms), stateful rules with conntrack, default-deny, iptables and ufw, Docker's own rules, and cloud security groups versus network ACLs.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • netfilter hooks
  • chains, first match
  • drop = 3.0 s timeout
  • reject = 0.12 ms
  • conntrack
  • SGs stateful, NACLs not

Notes

netfilter

netfilter is a set of hooks in the kernel's network path. Prerouting sees every packet as it arrives, and is where destination NAT happens. Input sees packets for this machine. Forward sees packets being routed through, which matters on routers, Docker hosts and Kubernetes nodes. Output sees packets this machine creates, and postrouting sees everything leaving, where source NAT happens. Rules attach to these hooks.

Hooks
  • prerouting just arrived (DNAT happens here)
  • input for this host
  • forward passing through (routers, Docker, k8s)
  • output created here
  • postrouting leaving (SNAT, masquerade)

Tables and chains

Rules live in chains, and chains live in tables. We created a table called lab, a chain attached to the input hook with a default policy of accept, and one rule: TCP to port eight thousand and eighty from our own address, count it, and drop it. Rules are checked from top to bottom, and the first rule that accepts or drops decides. If none match, the chain's policy applies. Order is everything.

nft (our lab)
nft add table inet lab
nft add chain inet lab input
  { type filter hook input
  priority 0; policy accept; }
nft add rule inet lab input
  tcp dport 8080 ip saddr
  127.0.0.1 counter drop

rules run top to bottom;
  first final match wins

Drop

First, drop. curl, with a three second limit, waited the full three seconds and gave up with a timeout. The rule's counter showed three packets: the client's SYN, retransmitted twice because no answer ever came. That is exactly what a dropped packet looks like from outside: a timeout, as if the host did not exist. When you see connection timeouts to a port, suspect a firewall that drops.

With the drop rule (ours)
$ curl -m 3 :8080/api/health
code 000  3.002 s  exit 28
  (timeout)

counter packets 3 bytes 180
  = the SYN, sent again
  and again

looks like a dead host

Reject

Then reject, with a TCP reset. curl failed in about a tenth of a millisecond with connection refused, and the counter saw only one packet. Drop hides your machine from scanners and slows them down, but it also makes your own services hang until timeouts. Reject fails fast and clearly. A common pattern: drop at the internet edge, reject inside your own network. After removing the rule, ShopRung answered two hundred again.

reject with tcp reset (ours)
$ curl :8080/api/health
code 000  0.000118 s  exit 7
  (connection refused)
counter packets 1

drop: hides, slows scanners,
  confuses your own team
reject: fast failure inside
  your network

Stateful rules

Real firewalls are stateful, using connection tracking from episode three. The first rule accepts packets that belong to connections already established, so replies to your own requests always get in, without opening every high port. Then a default deny policy, and only the openings you need: loopback, SSH from the internal network, web ports. Log what is rejected so you can see what you blocked.

A sane input chain
policy drop
ct state established,related
  accept
iif lo accept
tcp dport 22 ip saddr
  10.0.0.0/16 accept
tcp dport { 80, 443 } accept
ct state invalid drop
counter log reject

The tools

Several tools drive the same kernel. nftables is the modern interface, one syntax for IP version four and six. iptables is the classic one; on current distributions the iptables command is often a compatibility layer on top of nftables. ufw on Ubuntu and firewalld on Red Hat are friendlier wrappers. Docker and Kubernetes' kube proxy add many rules of their own, and Docker's published ports can bypass host rules you thought applied.

Same kernel, many front ends
  • nftables: modern, one tool for v4 and v6
  • iptables: classic syntax; often iptables-nft today
  • ufw (Ubuntu), firewalld (RHEL): friendly wrappers
  • Docker and kube-proxy write their own rules: check them before blaming yours

Cloud firewalls

In the cloud, security groups attach to each instance's network interface. They are stateful, only contain allow rules, and can reference other security groups, so you can say the app tier accepts from the load balancer's group, without IP addresses. Network ACLs attach to subnets, are stateless, and have numbered allow and deny rules, so you must open return ports too. Traffic must pass both. Part eight builds them with Terraform.

AWS
  • security group: on each network interface, stateful, allow rules only can reference other SGs
  • network ACL: on the subnet, stateless, numbered allow and deny, needs return ports (1024-65535)
  • both must allow the traffic

Common mistakes

Mistakes. Opening SSH or a database to the whole internet. Locking yourself out by applying a default deny before allowing established and SSH traffic, with no console access. A broad accept rule placed above the specific drop you wanted. Forgetting IP version six entirely. A stateless ACL without return ports. And no control over outbound traffic at all.

Avoid these
  • 0.0.0.0/0 on SSH or a DB
  • locking yourself out (no established rule, no console access)
  • rule order: a broad accept above a specific drop
  • forgetting IPv6
  • stateless ACL without return ports
  • no egress control

Say it like this

In the interview: a connection timeout suggests something dropped the packets; an instant connection refused means a reject or simply nothing listening. I confirm the service listens with ss, then read the rules with nft list ruleset or iptables with counters to see what matched, then the security groups and network ACLs. My baseline is stateful rules, default deny, and least privilege in both directions.

In the interview
  • "A timeout suggests a drop; an instant refused is a reject or no listener. I check ss on the server, nft list ruleset or iptables -L with counters, then security groups and NACLs. My
  • baseline: stateful, default
  • deny, least privilege."

Interview questions

Q1What is the difference between DROP and REJECT?

DROP silently discards the packet, so the client times out; REJECT sends back an error (TCP reset or ICMP), so the client fails immediately.

Q2What is a stateful firewall?

One that tracks connections (conntrack) so replies to allowed connections are accepted automatically with an "established, related" rule.

Q3What is the difference between iptables and nftables?

Both configure netfilter; nftables is the newer framework with one syntax for IPv4/IPv6 and atomic rule sets. Modern iptables is often iptables-nft on top of it.

Q4Security group vs network ACL in AWS?

Security groups are stateful, allow-only and attached to ENIs; NACLs are stateless, ordered allow/deny rules on subnets and need ephemeral return ports.

Q5Why might Docker bypass your host firewall rules?

Docker inserts its own DNAT and FORWARD rules for published ports, which are evaluated before rules in the host's INPUT chain.

Q6How do you avoid locking yourself out when changing a firewall?

Allow established connections and your SSH source first, keep console access, and apply changes with an automatic rollback timer.