Preparing your article…
Skip to content

Configure the UFW firewall on Linux

Learn to configure UFW on Linux without locking yourself out over SSH. This practical guide covers safe firewall rules and troubleshooting.

WRITTEN BY: ADRIEN PIRON • UPDATED ON 20 NOVEMBER 2026 • 5 MIN READ

Leaving port 22 open on a server exposed to the web is like leaving your front door unlocked while you go away for the weekend. Automated intrusion attempts knock on that door every few seconds, around the clock.

A well-configured firewall is not an impenetrable fortress, but it makes your server a much harder target for routine vulnerability scans. UFW simplifies firewall management. You do not need to be a networking expert to restrict access to the services you actually need. Here is how to secure your server.

Install UFW on Debian and Ubuntu

UFW is the default firewall on many Debian-based distributions. It is often already installed, but you may need to install it manually if the package was removed or if you use a minimal Debian system.

On Debian and Ubuntu, install the firewall with APT:

sudo apt update && sudo apt install -y ufw

After installation, check that the service is available. It is disabled by default at this point. On RHEL, Fedora and Rocky Linux, Firewalld is the preferred native firewall. You can install UFW from the EPEL repository, but running both tools can create configuration conflicts.

Secure SSH access before enabling UFW remotely

Before enabling a firewall over a remote connection, explicitly allow SSH. Otherwise, a restrictive firewall policy may immediately cut off your access to the server.

If you use the default SSH port, start by allowing port 22:

sudo ufw allow 22/tcp

If you use a custom port such as 2222, adjust the command. Adding a comment makes rules easier to maintain later:

sudo ufw allow 2222/tcp comment 'SSH admin'

To reduce brute-force attempts, limit connection attempts instead of simply allowing them:

sudo ufw limit 22/tcp

This rule allows six connections within a 30-second period and temporarily blocks IP addresses that appear suspicious.

For additional protection against brute-force attacks, you can also automatically block IP addresses with Fail2Ban.

Smart Ads

Before enabling the firewall, review the queued rules to make sure they are correct:

sudo ufw show added

Confirm that your SSH port appears in the list before continuing.

Set and enable basic UFW firewall rules

To secure your server, apply the principle of least privilege. Block all incoming traffic that has not been explicitly allowed, while leaving outgoing traffic enabled so the server can download package updates and communicate with external services.

Set the default rules in your terminal:

sudo ufw default deny incoming
sudo ufw default allow outgoing

Now enable the firewall:

sudo ufw enable

The system asks you to confirm because enabling UFW can interrupt existing connections if you did not allow them first. When it is enabled, check the full firewall status:

sudo ufw status verbose

A typical result looks like this:

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)

You should see an active status and your SSH rule. If you lose your connection, use an emergency console, such as a cloud console or physical access, then run sudo ufw disable to remove the blocks and turn off the firewall.

Manage and configure UFW firewall rules

Once UFW is active, you can open or close ports to suit your services. The firewall accepts service names listed in /etc/services or port numbers.

Open ports and allow services with UFW

To allow access to a web server, you can use the service names or their port numbers:

sudo ufw allow http
sudo ufw allow https

Allow a range of TCP and UDP ports with UFW

Some services, such as streaming or data transfers, need several ports. You can allow a complete range:

sudo ufw allow 2000:3000/tcp
sudo ufw allow 2000:3000/udp

Restrict connections by IP address

Security sometimes requires limiting access to a trusted IP address. For example, you can allow connections to a MySQL database only from a specific internal IP:

sudo ufw allow from 192.168.1.200 to any port 3306 proto tcp

Delete a UFW firewall rule

Before removing a rule, find its number:

sudo ufw status numbered

Identify the rule you want to remove and run the delete command with its number. For example, to remove the third rule:

sudo ufw delete 3

Rule numbers change whenever you delete a rule. After each deletion, run sudo ufw status numbered again before removing another one so you do not delete the wrong rule.

Diagnose and troubleshoot firewall problems

If a service is not working as expected, start by checking UFW’s logs. Set the logging level to medium to see more detail:

sudo ufw logging medium

To read the log in real time and monitor events, run:

sudo tail -f /var/log/ufw.log

Entries marked [UFW BLOCK] show which traffic the system is rejecting. If you make a serious configuration mistake, reset the firewall with sudo ufw reset. This removes all custom rules and disables UFW so you can start again with a clean configuration.

Here is a summary of essential firewall maintenance commands:

TaskCommand
Show the full statussudo ufw status verbose
List numbered rulessudo ufw status numbered
Show pending rulessudo ufw show added
Disable the firewallsudo ufw disable
Reset the firewallsudo ufw reset

Your server is now protected. A firewall is only one layer of security, so combine it with regular package updates and careful SSH key management.

Recalculation in
Too many readers are currently using an ad blocker

We allow some readers to view our articles for free with an ad blocker. However, their number is currently too high for us to keep this access available to everyone.

You can disable your ad blocker to continue reading immediately, subscribe to enjoy all our content without ads, or come back a little later when the pressure has eased.

I have disabled my ad blocker

Access will be restored automatically as soon as the situation allows.