# Firewalls, Security Groups and NAT

> Control which traffic may flow in and out of hosts and networks, both on Linux and in cloud networks. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/devops/firewalls-nat
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/devops/internet-tcpip)

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; `nftables` is the modern interface, `iptables` the 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](https://wiki.nftables.org/) (netfilter.org)
- [Amazon VPC security](https://docs.aws.amazon.com/vpc/latest/userguide/security.html) (AWS)

Previous: [Load Balancing and Proxies](https://www.internhack.in/roadmaps/tracks/devops/load-balancing-proxies) · Next: [SSH, Bastions and VPNs](https://www.internhack.in/roadmaps/tracks/devops/ssh-vpn)

## More in Networking You Will Actually Debug

- [How the Internet Works: TCP/IP and the OSI Model](https://www.internhack.in/roadmaps/tracks/devops/internet-tcpip)
- [DNS in Practice](https://www.internhack.in/roadmaps/tracks/devops/dns)
- [HTTP and HTTPS](https://www.internhack.in/roadmaps/tracks/devops/http-https)
- [TLS and Certificates](https://www.internhack.in/roadmaps/tracks/devops/tls-certificates)
- [Load Balancing and Proxies](https://www.internhack.in/roadmaps/tracks/devops/load-balancing-proxies)
- [Firewalls, Security Groups and NAT](https://www.internhack.in/roadmaps/tracks/devops/firewalls-nat)
- [SSH, Bastions and VPNs](https://www.internhack.in/roadmaps/tracks/devops/ssh-vpn)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DevOps Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/devops)
