Skip to content
BENIBIX Learn

UFW and Linux Firewalls

Level 4 · Timed jobs and the firewall

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

Course contentsLesson 10 of 10

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

12 mincovers 6 glossary entries

Worth reading first: systemd and Daemons

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.
Two ways to write a rule at the top. One place to keep the rules. One check at the bottom.

Worked 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.

The words for this lesson6

Check yourself

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

Finished this lesson?

Marking it complete also marks its 6 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.