Anime-Sanctuary

A firewall with ufw

On a fresh server, every program that listens on a port can be reached from anywhere. Here is how to install the ufw firewall on Debian, close everything at once, then open only what you choose, without sawing off the branch you're sitting on.

Course
Tutorial 12 of 35
Time
25 min to read
Tested with
Debian 13.7 (trixie), ufw 0.36.2
What you will be able to do
  • Install ufw and deny all incoming traffic by default.
  • Allow SSH before enabling the firewall.
  • Read the output of ufw status.
  • Open a port, close a port, delete a rule.
  • Get back in if you locked yourself out.

Before you start: Secure SSH: turn off passwords, The words: IP, DNS, domain, port, firewall.

Contents
  1. A guard in front of every door
  2. 1. Install ufw
  3. 2. Deny everything by default
  4. 3. Allow SSH first
  5. 4. Enable the firewall
  6. 5. Read ufw status
  7. 6. Open a port, then close it
  8. If it fails
  9. Frequently asked questions
  10. What's next
  11. Sources

How do you set up the ufw firewall on Debian without cutting off your access to the server? By following an order: deny everything, allow SSH, and only then turn it on. This article assumes you already log in to your server with an SSH key, as in Secure SSH: turn off passwords (test: ssh alice@203.0.113.10 should open the server without asking for a password). The word "firewall" is explained in The words: IP, DNS, domain, port, firewall.

A guard in front of every door

A port is a numbered door on your server: 22 for SSH, 80 for websites (see The words: IP, DNS, domain, port, firewall). A firewall is a guard that lets traffic through or blocks it based on that number. Without one, every program that listens on a port can be reached from the Internet, whether you want it or not. With one, a door opens only if you say so.

Our guard is called ufw, short for Uncomplicated Firewall. You give it instructions called rules, like "let this port in". The plan takes four steps:

  1. deny everything;
  2. allow SSH;
  3. turn the guard on;
  4. check.

The order matters: turned on without the SSH rule, the guard leaves you outside with the rest of the world.

The firewall lets SSH through and blocks the rest A request from the Internet on port 22 goes through the ufw firewall and reaches the server. A request on port 3306, with no rule for it, is stopped by the firewall. Internet ufw firewall Your server port 22 passes port 3306 blocked
Text version: a request on port 22 reaches the firewall, which has a rule for it: it passes and reaches the server. A request on port 3306 has no rule: the firewall stops it and it never reaches the server.

Your console is ready. Now it's time to hire the guard.

1Install ufw

Debian doesn't install ufw by default. Log in to the server as alice, then run apt install with sudo (Updates and packages with apt covers that command in detail).

On the server
sudo apt install ufw

Apt shows what it's about to install and asks you to confirm with Y. Then ask ufw what state it's in.

On the server
sudo ufw status
Expected output
Status: inactive

inactive means "installed, but not on duty yet". Nothing is blocked: this is the right moment to set the rules before you switch the guard on.

2Deny everything by default

The default rule says what the guard does when no other rule matches. We set two:

  • connections that come in to the server: "deny". Whatever isn't allowed is blocked;
  • connections that go out (the server downloading its updates, for example): "allow".
On the server
sudo ufw default deny incoming
sudo ufw default allow outgoing
Expected output
Default incoming policy changed to 'deny'
(be sure to update your rules accordingly)
Default outgoing policy changed to 'allow'
(be sure to update your rules accordingly)

These messages confirm the change, and the parenthetical note reminds you to add more rules. It's right to warn you: once on, the guard would refuse even SSH. Let's allow it right now.

3Allow SSH first

SSH listens on port 22 and speaks TCP (the way the two machines talk to each other), hence the 22/tcp notation. The allow command creates a rule that lets this traffic in.

On the server
sudo ufw allow 22/tcp
Expected output
Rules updated
Rules updated (v6)

There are two lines because ufw writes the rule twice: for the classic addresses (IPv4) and for IPv6, the newer kind of address from The words: IP, DNS, domain, port, firewall. The (v6) suffix marks this second version and shows up everywhere. Read it as "the same rule, for IPv6".

Your SSH doesn't listen on port 22

If you moved SSH to another port, for example 2222, allow that port instead: sudo ufw allow 2222/tcp. To find it, run sudo sshd -T | grep -i ^port: the line it prints gives the port, and 22 is the usual one.

The SSH rule is in place, so we can switch the guard on.

4Enable the firewall

The enable command puts the guard on duty, and it restarts along with the server. Since you're connected over SSH, it asks a question out of caution.

On the server
sudo ufw enable
Expected output
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Type y then Enter. "Firewall is active" confirms the guard is working. Now run the test that matters: open a new terminal on your computer, then a second connection, without closing the first.

On your computer, in a new terminal
ssh alice@203.0.113.10 hostname
Expected output
mon-serveur

The server's name appears: SSH gets through. If the command stays silent, the guard is blocking SSH, and "If it fails" tells you what to do. Now let's read what the guard has on file.

5Read ufw status

The ufw status command lists the rules in force. It's the guard's dashboard, to check after every change. Only the two 22/tcp lines matter here; the words around them are described just below.

On the server
sudo ufw status
Expected output
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)

Each line reads from left to right:

  • To: the port it targets;
  • Action: what is done with it, ALLOW for "allow";
  • From: where the traffic comes from, Anywhere for "from anywhere".

So our two lines say: "SSH is open to everyone, over IPv4 and IPv6". Everything else is denied, by the default rule.

Show the default rules too

Add verbose to see the default rules as well as the regular ones.

On the server
sudo ufw status verbose
Expected output
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)

The Default line repeats your choice: deny incoming, allow outgoing. routed means traffic that passes through the server on its way to another device: ignore it for now.

The guard is in place and SSH gets through. What's left is the everyday routine: open a door when you install a service, then close it again.

6Open a port, then close it

Opening a port means adding an allow rule; closing it means deleting that rule. Let's take port 80, the one for websites. To see the difference, we first test it from your computer: curl knocks on the door and waits five seconds at most.

On your computer
curl -sS --connect-timeout 5 http://203.0.113.10/
Expected output
curl: (28) Connection timed out after 5002 milliseconds

Silence, then "timed out": the guard ignores whoever knocks. Let's open port 80. Here is the catch: the error will change, and that change is the good news.

On the server
sudo ufw allow 80/tcp
Expected output
Rule added
Rule added (v6)

Let's run the same test again from your computer.

On your computer
curl -sS --connect-timeout 5 http://203.0.113.10/
Expected output
curl: (7) Failed to connect to 203.0.113.10 port 80 after 0 ms: Could not connect to server

This new error is a success. The answer became instant: the door is open, but no website is listening behind it. The exact text varies a little with the curl version. To close the door again, repeat the rule with delete in front.

On the server
sudo ufw delete allow 80/tcp
Expected output
Rule deleted
Rule deleted (v6)

Both versions of the rule (IPv4 and IPv6) went away together. Check with sudo ufw status: only SSH should remain.

Two variants for later (don't type them)
  • Deny explicitly: sudo ufw deny 25/tcp adds a blocking rule;
  • Allow only one address: sudo ufw allow from 203.0.113.50 to any port 22 proto tcp lets only that address in on SSH.
Delete a rule by its number

You can delete by number, with sudo ufw status numbered then sudo ufw delete 2. There are two traps: the number targets only one rule, so the IPv6 version stays in place; and the numbers shift after each deletion, so reread the list in between. With delete allow 80/tcp, neither problem exists.

You can open, close and read. Now let's see what can go wrong, starting with the worst case.

If it fails

The dreaded case is turning the firewall on without an SSH rule: the guard refuses your own connection. The ssh command then stays silent for a while, and gives up.

On your computer
ssh alice@203.0.113.10
Expected output
ssh: connect to host 203.0.113.10 port 22: Connection timed out

The door is shut, but your hosting provider's remote console works without SSH. To get back in:

  1. Open the remote console in your provider's customer area.
  2. Log in there with alice and her password.
  3. Add the missing rule with the command below.
  4. Try the SSH connection again from your computer.
On the server (in the console)
sudo ufw allow 22/tcp
Expected output
Rule added
Rule added (v6)

The rule applies right away. If you'd rather start from scratch, the next command switches the guard off, and you redo the steps cleanly.

On the server (in the console)
sudo ufw disable
Expected output
Firewall stopped and disabled on system startup

Two other messages come up often:

  • Skipping adding existing rule: you ran a command that had already worked. No harm done: read ufw status.
  • Could not delete non-existent rule: the text of your delete matches no rule. Copy it from ufw status, for example 80/tcp.

Frequently asked questions

How do I enable ufw without cutting off my SSH access?

Follow the order: deny all incoming traffic, allow SSH with sudo ufw allow 22/tcp, and only then enable the firewall. Test a second connection before you close the first.

How do I open or close a port with ufw?

To open a port, run for example sudo ufw allow 80/tcp. To close it again, run sudo ufw delete allow 80/tcp, then reread the result with sudo ufw status.

How do I see the ufw firewall rules?

The sudo ufw status command lists the rules in force, with the port, the action and where the traffic comes from. Add verbose to see the default rules too.

What's next

Your server now lets in only what you allowed, and you know how to reopen the door if you slip. But the SSH door is still open to everybody, and bots knock on it nonstop. The next article, fail2ban, adds a bouncer that bans the ones who keep pushing. It comes now because it relies on this firewall to shut the door on anyone who won't give up.