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
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:
- deny everything;
- allow SSH;
- turn the guard on;
- check.
The order matters: turned on without the SSH rule, the guard leaves you outside with the rest of the world.
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).
sudo apt install ufwApt shows what it's about to install and asks you to confirm with Y. Then ask ufw what state it's in.
sudo ufw statusStatus: inactiveinactive 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".
sudo ufw default deny incoming
sudo ufw default allow outgoingDefault 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.
sudo ufw allow 22/tcpRules 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.
sudo ufw enableCommand may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startupType 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.
ssh alice@203.0.113.10 hostnamemon-serveurThe 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.
sudo ufw statusStatus: 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,ALLOWfor "allow";From: where the traffic comes from,Anywherefor "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.
sudo ufw status verboseStatus: 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.
curl -sS --connect-timeout 5 http://203.0.113.10/curl: (28) Connection timed out after 5002 millisecondsSilence, 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.
sudo ufw allow 80/tcpRule added
Rule added (v6)Let's run the same test again from your computer.
curl -sS --connect-timeout 5 http://203.0.113.10/curl: (7) Failed to connect to 203.0.113.10 port 80 after 0 ms: Could not connect to serverThis 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.
sudo ufw delete allow 80/tcpRule 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/tcpadds a blocking rule; - Allow only one address:
sudo ufw allow from 203.0.113.50 to any port 22 proto tcplets 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.
ssh alice@203.0.113.10ssh: connect to host 203.0.113.10 port 22: Connection timed outThe door is shut, but your hosting provider's remote console works without SSH. To get back in:
- Open the remote console in your provider's customer area.
- Log in there with
aliceand her password. - Add the missing rule with the command below.
- Try the SSH connection again from your computer.
sudo ufw allow 22/tcpRule 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.
sudo ufw disableFirewall stopped and disabled on system startupTwo other messages come up often:
Skipping adding existing rule: you ran a command that had already worked. No harm done: readufw status.Could not delete non-existent rule: the text of yourdeletematches no rule. Copy it fromufw status, for example80/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.