UFW (Uncomplicated Firewall) is a command-line frontend for
nftables/iptables. It is installed by default on Ubuntu (disabled at
boot), available through apt on Debian, and designed to make configuring
a host firewall accessible without learning the iptables syntax. It is the
right entry point for a standalone Debian/Ubuntu server. In this guide, you set
a strict default policy, open the useful flows, validate the firewall
state, and equip yourself with a rollback plan so you never lock yourself
out of your own server.
What you will learn
- Install UFW on Debian/Ubuntu and understand when it is not available elsewhere
- Allow SSH before any activation to avoid the lockout
- Set a default policy (
deny incoming,allow outgoing) and enable it - Add
allow,deny,limitrules by service or by port - Check the state and the number of each rule with
ufw status verbose/numbered - Roll back cleanly after a bad rule
In what context?
UFW is made for the host firewall of a Linux server or workstation. You use it when:
- you are on Ubuntu or Debian and want a simple firewall, quick to audit
- you want a readable syntax (
sudo ufw allow 22/tcp) rather than iptables chains - you are setting up a first filtering layer before considering direct
nftablesorfirewalld
If you are on RHEL, Fedora, CentOS Stream or Rocky, the default is firewalld, UFW is not packaged by default there and mixing it with firewalld creates silent conflicts. On these systems, stick with firewalld.
Prerequisites
- A Debian 13 or Ubuntu 24.04 LTS server
- A user with
sudorights - Key-based SSH access already configured; if you use SSH with a password, first read SSH keys
- A fallback access (physical console, cloud provider console, serial…) in case of lockout
Install UFW
The package is called ufw everywhere, but its status differs by
distribution: absent on a minimal Debian, usually already present on
Ubuntu, and outside the default repositories on the RHEL family.
sudo apt updatesudo apt install -y ufwUFW is normally already present:
dpkg -l ufwIf it is missing:
sudo apt updatesudo apt install -y ufwUFW is not the recommended solution here. Prefer firewalld, which is packaged by default and integrated into the Red Hat ecosystem. If you still want UFW, it is available through EPEL (version 0.35 on AlmaLinux 10), but mixing it with firewalld is to be avoided.
Check that the command responds:
sudo ufw statusExpected output after installation, before activation:
Status: inactiveAllow SSH before any activation
This is the most important step of the guide. Before enabling UFW, create the rule that keeps your SSH session alive, otherwise the default policy will refuse your next connection.
-
Allow SSH explicitly
Terminal window sudo ufw allow 22/tcpIf your SSH listens on a non-standard port:
Terminal window sudo ufw allow 2222/tcp comment 'SSH admin' -
Rate-limit SSH to slow down brute-force attacks
Terminal window sudo ufw limit 22/tcplimitrefuses any new connection from a source address that has attempted 6 or more in 30 seconds: the rule it sets is--hitcount 6 --seconds 30in/etc/ufw/user.rules. It is not a ban, the IP gets access back as soon as its rate drops. Useful against brute-force bots; not a substitute for SSH keys and full SSH hardening if you expose SSH on the Internet (see Harden SSH (in French)). -
Review the rule
Terminal window sudo ufw show addedIt must show
ufw limit 22/tcporufw allow 22/tcpdepending on your choice.
As long as UFW is not enabled, the ufw allow/limit/deny commands are not
active yet: they are added to a queue visible with sudo ufw show added.
They will all be applied at once at activation, in the next step.
Enable UFW with a strict policy
By default, UFW applies two policies that form the right starting point for a server: nothing comes in unless allowed, everything can go out.
deny incoming: any inbound traffic not explicitly allowed is refusedallow outgoing: all outbound traffic is allowed
This is the right baseline. Enable it:
sudo ufw enableYou will see a warning, then the message Firewall is active and enabled on system startup. Check immediately:
sudo ufw status verboseExpected output:
Status: activeLogging: on (low)Default: deny (incoming), allow (outgoing), disabled (routed)New profiles: skip
To Action From-- ------ ----22/tcp LIMIT IN Anywhere22/tcp (v6) LIMIT IN Anywhere (v6)The verbose view states the direction of each rule (LIMIT IN), where a
plain ufw status shows LIMIT. The disabled (routed) value is that of a
server that does not route: it becomes deny (routed) as soon as IP
forwarding is enabled, for example on a Docker host.
Your current SSH connection must still work. If it drops, use your
fallback access and run sudo ufw disable.
Open the useful services
UFW knows service names through /etc/services. The two syntaxes below
are equivalent:
# by service namesudo ufw allow http
# by port numbersudo ufw allow 80/tcpA few common cases, each with a comment that will explain the rule during
the next audit:
# Web serversudo ufw allow httpsudo ufw allow https
# Mail server (encrypted SMTP + IMAP)sudo ufw allow 587/tcp comment 'SMTP submission'sudo ufw allow 993/tcp comment 'IMAPS'
# Restrict a service to an internal networksudo ufw allow from 192.168.10.0/24 to any port 3306 proto tcp comment 'MySQL LAN only'
# Block an IP hammering a public service (e.g. exposed HTTP)# Useless on a port already blocked by deny-by-defaultsudo ufw deny from 203.0.113.42 to any port 80 proto tcpAlways check after each batch of rules:
sudo ufw status numberedThe number lets you delete a precise rule:
sudo ufw delete 3Check and read the rules
Three check commands to keep at hand, each answering a different question about the state of the firewall:
| Need | Command |
|---|---|
| State + default policy + rules | sudo ufw status verbose |
| Rules with number (for deletion) | sudo ufw status numbered |
Pending rules (before ufw enable) | sudo ufw show added |
To inspect blocked traffic, enable logging. Refused packets are kernel
messages prefixed with [UFW BLOCK]: where they land depends on whether
rsyslog is present, installed by default on Ubuntu Server but no longer on
Debian since version 12.
sudo ufw logging mediumsudo tail -f /var/log/ufw.logsudo ufw logging mediumsudo journalctl -k -f --grep 'UFW BLOCK'The [UFW BLOCK] entries show the refused packets; use them to diagnose a
service that does not answer. On Debian 13 without rsyslog, the
/var/log/ufw.log file does not exist: looking for it wrongly suggests
that nothing is blocked.
Best practices
These habits avoid most UFW incidents; the first one alone prevents the lockout, the others keep the ruleset readable and persistent.
- Always allow SSH before
ufw enable, the golden rule. - Prefer
limitoverallowfor SSH exposed on the Internet, combined with key-only SSH. - Comment each rule with
comment '…': reviewing it in 6 months will be much easier. - Scope the rules to the right network (
from 192.168.0.0/16 to any …) rather than openingAnywhereby default. - Do not mix UFW with hand-written iptables/nftables: UFW writes its own chains, a manual change creates silent conflicts.
- Test persistence:
sudo systemctl rebootthen checkufw status verboseagain. UFW is normally enabled at boot, but a cloud-init configuration can reset the state toinactive.
Security and rollback
Before running ufw enable, have these two safety nets in place:
- A second open SSH session on the server (activation does not cut established connections, but a failed rule could cut the next one).
- Console access configured and tested (IPMI, cloud KVM, serial port).
Full rollback in case of trouble:
# Disable UFW (back to the pre-activation state)sudo ufw disable
# Reset all rulessudo ufw resetreset erases all the rules and disables UFW. It is drastic, use it
if you are lost. After a reset, you must redo the SSH authorisation
procedure before a new enable.
Quick troubleshooting
The table below starts from the symptom you observe, gives the most likely cause, then the action that fixes it.
| Symptom | Probable cause | Action |
|---|---|---|
SSH cut after ufw enable | SSH port not allowed or rule too restrictive | Console access → sudo ufw disable, fix, try again |
| Service reachable locally, not from outside | Port not allowed or rule with a from too narrow | sudo ufw status numbered; adjust or add allow <port>/tcp |
Empty ufw status after reboot | UFW disabled at boot (Ubuntu cloud-init) | sudo systemctl enable --now ufw + check sudo ufw enable |
Rules missing from the INPUT chain of iptables -L | Normal: UFW stores its rules in its own chains, called from INPUT | sudo iptables -L ufw-user-input -n or stick to sudo ufw status |
[UFW BLOCK] in the logs for a legitimate service | Service arrives on an interface that is not allowed | Add allow in on <iface> or widen the rule |
Put it into practice
Open a service in the ufw firewall on a VM, check the active rule, and confirm that it persists after a reboot.
Key points
- UFW is a simplified frontend for nftables/iptables, packaged by default on Ubuntu and installable on Debian.
- Allow SSH before enabling: it is the only mistake that can cut you off from your server.
- Use
limitfor SSH exposed on the Internet,allowfor an internal network. - Recommended default policy:
deny incoming,allow outgoing. sudo ufw status verboseandsudo ufw status numberedare the two commands to memorise.- Each rule can carry a
comment: use it systematically. - On RHEL/Fedora/Rocky, prefer firewalld; do not mix the two.
ufw resetas a last resort to start again from a blank page.
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Configure a firewall with firewalld
A new full quiz with random questions
Work again only on the questions you missed
FAQ: frequently asked questions
The questions below cover those that come up most often about UFW: its
role, enabling without lockout, the difference between allow and
limit, and the choice against firewalld.
nftables/iptables. It makes configuring a host firewall on Debian/Ubuntu accessible without learning the iptables syntax, with readable commands such as sudo ufw allow 22/tcp. Installed by default on Ubuntu (disabled at boot).ufw enable: sudo ufw allow 22/tcp (or sudo ufw limit 22/tcp). The default deny incoming policy refuses any port that is not allowed, SSH included: it is the number one cause of lockout. Also keep a fallback console access (IPMI, cloud KVM) and a second SSH session open during the change.allow: opens a port (sudo ufw allow 80/tcp)deny: explicitly refuses itlimit: opens the port but refuses new connections from a source that attempts 6 or more in 30 s (anti brute force)
limit for SSH exposed on the Internet, allow for an internal network or through a VPN. limit is not a substitute for SSH keys and hardening.sudo ufw status numbered, then delete by number: sudo ufw delete 3. For the full state + the default policy: sudo ufw status verbose. Before ufw enable, queued rules show up with sudo ufw show added. As a last resort, sudo ufw reset starts again from a blank page.