# UFW and Linux Firewalls

> How UFW closes every port except the ones you allow, in short rules like allow 22. Where nftables and iptables fit underneath.

**Course:** [Linux Command Line](https://learn.benibix.com/learn/infrastructure-linux)  
**Time:** 12 min

**After this lesson you can:**

- Allow SSH before switching UFW on
- Open only the ports a server needs
- Say how UFW, nftables and iptables relate

## How to close every port you do not use

A firewall decides which network traffic may reach a machine. On Ubuntu, the easy way to set one up is UFW. You type short rules, such as sudo ufw allow 22/tcp, then sudo ufw enable. From then on, every port you did not allow is closed to the outside world.

UFW starts from a simple stance. Traffic going out is allowed. Traffic coming in is refused, unless a rule lets it through. So the work is to list the few ports the server really needs, often 22 for logins and 80 and 443 for a website.

The order of the steps matters more than anything else here. If you log in over the network and switch UFW on before allowing port 22, the firewall shuts your own door behind you. Always allow SSH first, then enable. UFW even warns you about this.

UFW is a friendly front for deeper tools. The rules really live in nftables, the part of Linux that holds them today. The kernel is the core of the system. It checks every packet against those rules. Before nftables there was iptables. Its commands still work, and they are changed into nftables rules below.

So three names describe one system. UFW is the short way to write rules. iptables is the old long way, still found in many guides. nftables is where the rules end up. Use one tool to manage a machine, so two sets of rules do not fight.

sudo ufw status verbose lists every rule and the default stance. Check it after each change. A rule that opens a port is only half the story. A program must also be waiting on that port, or nothing answers.

A common mistake is to think a firewall makes a weak service safe. It only decides who may knock. A login service with a weak password is still weak to anyone you let through. The firewall shrinks the number of doors, and each door still needs its own lock.

```console
$ sudo ufw allow 22/tcp
Rules updated
$ sudo ufw allow 443/tcp
Rules updated
$ sudo ufw enable
Firewall is active and enabled on system startup
```
Allow logins first, then the website. Only then switch the firewall on.

![Four stacked layers. At the top, UFW takes a short rule, allow 22. Beside it, the older iptables command takes a long rule. Both feed down into nftables, which holds the rules. At the bottom, the core of Linux checks every packet against them.](https://learn.benibix.com/diagrams/inf3-linux-firewall.svg)

### Example: Emma locks herself out

Emma logs in to her server over SSH. She runs sudo ufw enable and plans to add rules after. Her window freezes, and she cannot log back in.

She opens her host's web console, which works like a screen plugged into the server. There she types sudo ufw allow 22/tcp. A second later, her normal login works again.

**Words in this lesson:** [Firewall](https://learn.benibix.com/term/infrastructure/firewall), [Network Port](https://learn.benibix.com/term/infrastructure/port), [Uncomplicated Firewall (UFW)](https://learn.benibix.com/term/infrastructure/ufw), [nftables](https://learn.benibix.com/term/infrastructure/nftables), [iptables](https://learn.benibix.com/term/infrastructure/iptables), [Kernel](https://learn.benibix.com/term/development/kernel)

**Question:** You are logged in over SSH. What must you do before sudo ufw enable?

**Answer:** Allow port 22, so your own login is not shut out. UFW blocks all traffic coming in that no rule allows. With no rule for port 22, it cuts off the very login you are using.

---

Source: https://learn.benibix.com/learn/infrastructure-linux/ufw-and-linux-firewalls
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
