Skip to main content

Linux Firewall

A Linux firewall controls which network packets may enter, leave, or cross a server, using rules enforced by the kernel's netfilter framework. Tools such as ufw, firewalld, iptables, and nftables configure those rules.

Understanding Linux Firewalls

What Is a Linux Firewall?

A firewall is a gatekeeper for network traffic. Each packet is checked against an ordered rule list and receives an action: ACCEPT, DROP, or REJECT. The safest model is default-deny: block everything, then explicitly allow only the ports and sources you need.

How Netfilter, iptables, nftables, and ufw Relate

netfilter is the kernel framework that intercepts packets. iptables and nftables are command-line tools that load rules into it, and ufw and firewalld are friendlier frontends over those tools. They configure the kernel; they do not filter packets themselves.

flowchart TD A[ufw or firewalld] --> B[iptables or nftables] B --> C[netfilter in kernel] C --> D{Rule match?} D -->|Allow| E([Packet accepted]) D -->|Deny| F([Packet dropped])

Defense in Depth: Three Firewall Layers

Production setups layer controls rather than relying on one.

TEXT
1. Cloud security group or network ACL blocks before traffic reaches the VM
2. Host firewall (ufw, nftables) second layer on the server itself
3. Application controls (nginx, AppArmor) limits at the service level

How to Configure a Basic Firewall With ufw

Bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow SSH first, from your own network
sudo ufw allow from 10.0.0.0/8 to any port 22 proto tcp
sudo ufw allow 443/tcp
sudo ufw allow from 10.0.1.50 to any port 5432 proto tcp
sudo ufw enable
sudo ufw status numbered
sudo ufw delete 3

Troubleshooting Firewalls

Symptom Command What to Check
Service unreachable sudo ufw status Port not allowed
Locked out of SSH Console or recovery mode Allow port 22 from your IP
Docker port still open sudo iptables -L DOCKER-USER Docker bypasses ufw
Connection refused vs timeout ss -tlnp and nc -zv host port Not listening vs filtered
Common Mistake

Assuming ufw protects Docker-published ports. Docker writes its own iptables rules that can bypass ufw. Bind containers to 127.0.0.1 (for example -p 127.0.0.1:8080:80) or restrict traffic in the DOCKER-USER chain.

Security

Allow SSH before enabling default-deny, and test from a second terminal before closing the first. Without console access, a mistake means a lockout.

Remember

A cloud security group and a host firewall are independent. A port must be open in both to be reachable.

Frequently Asked Questions

What is the relationship between iptables, nftables, and netfilter?

netfilter is the kernel framework that processes packets. iptables and nftables are userspace tools that load rules into it. nftables is the newer replacement, and the iptables command is often a compatibility layer over it.

Why does "connection refused" differ from a timeout?

Refused usually means the host is reachable but nothing is listening on that port. A timeout usually means a firewall or network rule is silently dropping the packets before they arrive.

How do I avoid locking myself out of a remote server?

Allow your SSH source first, apply changes from a second open session, and use a timed rollback so rules revert if you lose access.

Found a mistake, an outdated step or a broken link?