In one minute
- A firewall decides which network traffic may come in to, or go out of, a machine.
- Firewalls keep attackers out. They are also a top cause of "it times out" problems. You need to find the one rule that is blocking traffic.
- A firewall is a list of rules, such as "allow port 22" or "block everything else". Each packet is checked against the rules from top to bottom, and the first rule that matches wins. If no rule matches, a default rule decides. On Linux, every firewall tool (ufw, firewalld, iptables) programs the same engine inside the kernel, called netfilter.
- 👉 Opening a port for a new app, locking down a new server, and fixing timeouts.
Think of it like: a security guard at a gate with a list. The guard reads the list from the top and follows the first line that fits you. If no line fits, they follow the sign at the bottom: "Everyone else: no entry".
Layers of firewalls
A packet travelling from the internet to your app may pass several firewalls:
Internet → AWS NACL (subnet) → Security group (instance) → Host firewall (ufw / firewalld) → App| Layer | Stateful? | Where it is set up |
|---|---|---|
| Network ACL | No: you must allow reply traffic yourself | AWS VPC subnet |
| Security group | Yes: replies are allowed automatically | AWS instance or network card |
| Host firewall | Yes | On the Linux server |
| Kubernetes NetworkPolicy | Yes | Inside the cluster |
Stateful means the firewall remembers connections. If a request was allowed out, its reply is allowed back in automatically.
Why it matters: When you debug, check every layer. Any one of them can be the block.
Think of it like: a building with a gate (NACL), a front door (security group) and an office door (host firewall). You need all three open to reach the desk (app).
ufw on Ubuntu
ufw ("uncomplicated firewall") is Ubuntu's easy firewall tool.
👉 Any Ubuntu server.
sudo ufw statussudo ufw allow 22/tcpsudo ufw allow 80/tcpsudo ufw enable| Command | What it does |
|---|---|
ufw status verbose | Show the rules and default settings |
ufw default deny incoming | Block everything coming in, unless allowed |
ufw default allow outgoing | Allow everything going out |
ufw allow 443/tcp | Open a port |
ufw allow from 10.0.0.0/16 to any port 5432 | Allow only one network to one port |
ufw deny 23 | Block a port |
ufw status numbered then ufw delete 3 | Remove rule number 3 |
ufw limit ssh | Slow down repeated SSH attempts (stops password guessing) |
Allow SSH before you enable ufw on a remote server. Otherwise you cut off your own connection and cannot get back in.
firewalld on RHEL, Rocky and Amazon Linux
firewalld is the firewall tool on the Red Hat family. It groups rules into zones (public, internal, trusted). Each network card belongs to one zone.
👉 Any RHEL-family server.
sudo firewall-cmd --statesudo firewall-cmd --get-active-zonessudo firewall-cmd --zone=public --list-allsudo firewall-cmd --zone=public --add-service=http --permanentsudo firewall-cmd --zone=public --add-port=8080/tcp --permanentsudo firewall-cmd --reload- Without
--permanent: the change works now, but is lost on reload or reboot. - With
--permanent: the change is saved, but only takes effect after--reload. - A common habit: test without
--permanent, then repeat with it once it works.
Remember it:
--permanent= saved for later.--reload= use it now. You usually need both.
iptables: what is underneath
ufw and older firewalld write iptables rules for you. Seeing iptables directly helps you understand any Linux firewall.
Rules are grouped into chains:
| Chain | Traffic |
|---|---|
INPUT | Packets coming to this machine |
OUTPUT | Packets leaving from this machine |
FORWARD | Packets passing through (routers, Docker, Kubernetes) |
Rules are checked top to bottom. The first match wins. If nothing matches, the chain's policy (ACCEPT or DROP) is used.
sudo iptables -L -n -vChain INPUT (policy DROP) pkts bytes target prot opt in out source destination 9123 812K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 51K 9M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 210 12K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 88 5K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80How to read it, line by line:
- Traffic from the machine to itself (
lo) is allowed. - Replies to connections that already exist are allowed.
- New connections on port 22 (SSH) are allowed.
- New connections on port 80 (HTTP) are allowed.
- Everything else is dropped, because the policy is DROP.
The pkts column counts how many packets matched each rule. It shows which rules are really being used, which is very useful when debugging.
Writing rules by hand:
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # appendsudo iptables -I INPUT 1 -s 203.0.113.9 -j DROP # insert at the topsudo iptables -D INPUT -p tcp --dport 443 -j ACCEPT # deletesudo iptables-save > /etc/iptables/rules.v4 # persist| Action | What happens | What the client sees |
|---|---|---|
ACCEPT | Let it through | A normal connection |
DROP | Throw it away silently | A timeout |
REJECT | Throw it away and say "no" | "Connection refused" straight away |
Remember it: DROP = ignored = timeout. REJECT = told no = refused.
nftables
Newer distributions use nftables (nft) underneath. The iptables command often just translates into it. The ideas are the same: tables, chains, rules, first match wins. sudo nft list ruleset shows everything.
Docker and Kubernetes write rules too
- Docker adds its own iptables rules for published ports. A port published with
docker run -p 8080:80can skip ufw entirely, because Docker's rules are checked first. If it should not be public, publish it as127.0.0.1:8080:80, or use theDOCKER-USERchain. - Kubernetes (kube-proxy) writes thousands of iptables rules (or IPVS) for its Services. Never wipe iptables on a Kubernetes node.
Real-world scenario: port 8080 times out from other servers
ss -tulpnsudo ufw statussudo iptables -L -n -vssshows the app listening on0.0.0.0:8080. Good.curl localhost:8080works on the server itself. The app is fine.- From another server, it times out. Something is dropping packets.
ufw statusdoes not list 8080, and the default is "deny incoming". Found it.sudo ufw allow from 10.0.0.0/16 to any port 8080 proto tcpfixes it. It only opens the port to the VPC, not the whole internet.- If it still times out, check the AWS security group and NACL next.
Common mistakes
- Turning on a firewall over SSH without allowing port 22 first.
- Forgetting
--permanentwith firewalld, so the fix disappears after a reboot. - Opening ports to
0.0.0.0/0(everyone) when only the VPC or the load balancer needs access. - Running
iptables -F(flush all rules) on a Docker or Kubernetes host. This breaks all container networking. - Checking only the server's firewall when the security group is the real block.
Practice
- Set ufw to deny incoming and allow outgoing, allow SSH and HTTP, then enable it.
- Allow PostgreSQL (5432) only from
10.0.0.0/16. - Read the iptables output above and say what happens to a new connection on port 3306.
- Explain why a DROP rule causes a timeout, but a REJECT rule causes "connection refused".
Cheat sheet
| Task | ufw | firewalld |
|---|---|---|
| Status | ufw status verbose | firewall-cmd --list-all |
| Open a port | ufw allow 8080/tcp | firewall-cmd --add-port=8080/tcp --permanent |
| Allow a network | ufw allow from 10.0.0.0/16 | firewall-cmd --add-source=10.0.0.0/16 --permanent |
| Apply | Automatic | firewall-cmd --reload |
| Low level | iptables -L -n -v | nft list ruleset |
Key takeaways
- A firewall is a list of allow and block rules for network traffic.
- Rules are read top to bottom, and the first match wins.
- Traffic can pass several firewalls: NACL, security group, server firewall. Check them all.
- Ubuntu uses
ufw. Red Hat usesfirewalld. Both sit on top of iptables or nftables. - DROP causes a timeout. REJECT causes "refused". Always allow SSH before turning a firewall on.