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.
Defense in Depth: Three Firewall Layers
Production setups layer controls rather than relying on one.
1. Cloud security group or network ACL blocks before traffic reaches the VM2. Host firewall (ufw, nftables) second layer on the server itself3. Application controls (nginx, AppArmor) limits at the service levelHow to Configure a Basic Firewall With ufw
sudo ufw default deny incomingsudo ufw default allow outgoing # Allow SSH first, from your own networksudo ufw allow from 10.0.0.0/8 to any port 22 proto tcpsudo ufw allow 443/tcpsudo ufw allow from 10.0.1.50 to any port 5432 proto tcp sudo ufw enablesudo ufw status numberedsudo ufw delete 3Troubleshooting 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 MistakeAssuming 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 theDOCKER-USERchain.
SecurityAllow SSH before enabling default-deny, and test from a second terminal before closing the first. Without console access, a mistake means a lockout.
RememberA 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.