Firewalls, Security Groups and NAT
Control which traffic may flow in and out of hosts and networks, both on Linux and in cloud networks.
Intermediate · about 4 hours · difficulty 2/5
Learn first: How the Internet Works: TCP/IP and the OSI Model
Network access control is one of the most effective security layers you own. Default-deny inbound, open only what is needed, and know where each rule lives.
On the host
- netfilter is the kernel packet filter;
nftablesis the modern interface,iptablesthe older one you will still see. - Friendly front ends: ufw (Ubuntu) and firewalld (RHEL).
- Stateful filtering: allow established/related return traffic automatically.
In the cloud
- Security groups are stateful, attached to instances or interfaces, allow-only.
- Network ACLs are stateless and apply per subnet.
- Put databases and internal services in private subnets with no public IPs; give them outbound access through a NAT gateway.
- Reference security groups from other groups instead of hard-coding IPs.
Also worth knowing
Web application firewalls (WAF) for HTTP-level filtering, DDoS protection services, egress filtering, and Kubernetes NetworkPolicies for pod-level rules.
Pitfalls
Opening SSH (22) or database ports to 0.0.0.0/0, forgetting that a stateless ACL needs rules for ephemeral return ports, and locking yourself out of a remote host when changing firewall rules (keep a console session open).
Good enough to move on: you can lock a VM down to only HTTP/HTTPS publicly and SSH from one IP, and design cloud rules where only the app tier can reach the database.
Key concepts
- Stateful vs stateless filtering
- nftables / iptables
- ufw and firewalld
- Cloud security groups
- Network ACLs
- NAT gateways and private subnets
- WAF and DDoS protection
Mini project
Harden a VM with ufw (default deny, allow 80/443, SSH only from your IP), then verify from another machine with nmap which ports are reachable.
Check yourself
Why does a stateless network ACL need an outbound rule for high-numbered ports to let web responses leave?
Free resources
- nftables wiki (netfilter.org)
- Amazon VPC security (AWS)