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.
- 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 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.
$ 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.
$ 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.
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.
- 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.
- 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.
- 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.
- "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."