The Short Answer
A firewall decides which network traffic may pass and which is dropped, by comparing every packet with a list of rules. A rule names things such as the source address, the destination port, the protocol, and the state of the connection, and says what to do: accept, drop, or reject. On a server, the firewall's main job is to make sure that only the services you mean to offer can be reached from the internet.
What a Rule Looks At
| Field | Example |
|---|---|
| Source address | 203.0.113.0/24 |
| Destination port | 22, 443 |
| Protocol | TCP, UDP, ICMP |
| Direction | Inbound or outbound |
| Connection state | New or established |
In most tools, rules are checked in order, and the first one that matches decides. Traffic that matches no rule gets the default policy. Which port belongs to which service is listed in common port numbers.
Stateful and Stateless
A stateless filter judges each packet on its own, so you must write rules for both directions to let replies come back. A stateful firewall tracks connections: once it has allowed a request, it lets the replies through, and it can tell a new connection from one that is already established. The Linux kernel tracks connections (connection tracking, or conntrack), so its firewall rules can be stateful, and a typical rule set starts by accepting established traffic.
On the Server or in the Network
- Host firewall: runs on the server itself and protects only that server. You set its rules as root.
- Network firewall: a separate device or service in front of one or more servers, such as a router's filter or a firewall in a provider's control panel. Its rules act before traffic reaches the server.
The two work together. Where both exist, a port must be open in both places before a service can be reached. Check with your provider which filters, if any, sit in front of your server.
Deny by Default
The safe pattern for inbound traffic is to block everything and open only what is needed: SSH for administration, ports 80 and 443 for a website, and nothing else. Databases, caches, and admin interfaces should listen on localhost or a private network, not on a public port. Outbound traffic is usually allowed by default on a server.
Firewalls on Linux
The filtering itself happens in the kernel, in the netfilter framework. Several tools write the rules:
| Tool | Where it is common |
|---|---|
| nftables | The current kernel interface |
| iptables | The older interface |
| UFW | Ubuntu; optional on Debian |
| firewalld | AlmaLinux, Rocky Linux |
UFW and firewalld are front ends: they turn short commands into nftables or iptables rules. Use one of them, not both at once. The guides are nftables basics, the iptables tutorial, how to set up a UFW firewall, and how to use firewalld.
Switch It On in the Safe Order
The classic mistake is to turn on a firewall over SSH before allowing SSH: the connection drops and you are locked out. Keep your current session open and allow SSH first. With UFW (on Debian, install it first with sudo apt install ufw):
sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw status verbose
If SSH runs on another port, allow that port instead. Then open a second SSH session to test before you close the first. If you do lock yourself out, a console in your provider's control panel, where one is offered, does not go through the server's network and still lets you log in. Opening further ports is covered in ufw allow port.
One trap on Docker hosts: traffic to ports published with -p is routed by Docker's own rules before UFW's rules apply, so UFW does not filter it; see Docker port mapping.
What a Firewall Does Not Do
- Weak passwords: if SSH must be open, the firewall lets every login attempt through. Use SSH keys and Fail2ban.
- Application bugs: traffic to an open port reaches the application, flaws and all. Keep software updated.
- Threats already inside: malware on the server can open outbound connections that an allow-all outbound policy lets through.
A web application firewall (WAF) is a different layer: it reads HTTP requests and blocks attack patterns such as SQL injection, which a packet filter cannot see.