What is the UFW firewall and how to configure it in Linux?

Aggiornato il 2026-10-23 14:01:01 47 visite

In this tutorial, we will explain in a simple and direct way how to configure the Firewall 'ufw' for your Linux server. This way, you can easily configure the incoming connections you wish to enable, drastically improving the security of your server.

Before starting the tutorial, don't you have a Linux Cloud server? Don't worry! With just one click, you can easily deploy it with SW Hosting.

What is UFW?

The acronym 'UFW' stands for 'Uncomplicated Firewall' and refers to an application that aims to set rules in 'iptables', the native firewall tables in Linux. Since iptables has a relatively complex syntax, using UFW for its configuration is a useful alternative without compromising security.

Install UFW

Installing the 'ufw' package is very simple and, in fact, it is installed by default in many distributions. In this case, we will provide the instructions for a Debian-based distribution, such as Ubuntu. In other distributions, the commands to follow may be different.

  • sudo apt update // To update the package list.
  • sudo apt install ufw // To install the 'ufw' package.

info Remember! You will need superuser privileges to perform this operation.

Configure and enable UFW

Once the firewall is installed and configured, we will explain the basic syntax to start establishing rules.

1. Definition of default behavior

First, we must determine if we want UFW to allow or deny incoming and outgoing traffic by default.

We can achieve this in the following way:

  • ufw default deny incoming // Denies incoming connections that do not match any rule.
  • ufw default allow incoming // Allows incoming connections that do not match any rule.

For outgoing connections:

  • ufw default deny outgoing // Denies outgoing connections that do not match any rule.
  • ufw default allow outgoing // Allows outgoing connections that do not match any rule.

Our recommendation is to deny incoming connections and allow outgoing connections for a basic configuration. Afterwards, you should create rules to allow access for those connections, protocols, or devices that you consider appropriate.

2. View the current firewall configuration

Now that you have created your first rule, you can view the current configuration with the following command:

ufw status

3. Allow SSH connections (IMPORTANT!)

To prevent being locked out of your own server once you enable the firewall, it is important that you create a rule that allows you to connect through port 22 (or whichever you have designated for the SSH service).

You can create your first rule to allow incoming traffic in the following way:

ufw allow 22

Of course, you must specify the port that the service uses.

4. Allow other incoming connections by protocol, source IP, and other parameters

Below, we present several examples that will demonstrate the UFW syntax, allowing you to adapt each one according to your needs.

  • ufw allow 80 // Allows incoming connections on port 80.
  • ufw allow http // Allows incoming connections on port 80, using the alias 'http' instead of the numerical port.
  • ufw allow 80/tcp // Allows only incoming connections with the TCP protocol on port 80.
  • ufw allow 1000-2000 // Allows incoming connections in a port range.
  • ufw allow from 10.0.0.30 // Allows incoming connections to any port and protocol from IP 10.0.0.30.
  • ufw allow from 10.0.0.0/24 // Allows incoming connections to any port and protocol from a range of IPs using CIDR notation (from 10.0.0.0 to 10.0.0.255 in this case).
  • ufw allow from 10.0.0.30 to any port 22 // Allows incoming connections to port 22 from IP 10.0.0.30.
  • ufw allow from 10.0.0.30 to any port 22 proto tcp // Allows incoming connections to port 22, using the TCP protocol from IP 10.0.0.30.

This is only a sample of the countless combinations that UFW allows. Of course, remember that you can also use deny to achieve the opposite effect.

5. Delete rules

To delete a rule, it is advisable to first display them in a numbered list. You can do this with the following command:

ufw status numbered

Once the rules are displayed preceded by a number that identifies them, you can delete them in the following way:

ufw delete 3 // Delete rule number '3'.

6. Insert rules with a specific number

You can use the following syntax to specify rules in a specific location, ensuring that the rule takes priority over those that follow it.

ufw insert 3 allow 22 // Inserts a rule to allow incoming connections at position 3.

7. Enable / disable logs

UFW has the option to log all the actions it takes and all access attempts. You can enable or disable UFW logging in the following way:

  • ufw logging on // Enables logs.
  • ufw logging off // Disables logs.

8. Enable / Disable the firewall

Finally, we will show you how to activate the firewall once you have established the necessary configuration for your server:

  • ufw enable // Activates the firewall and puts all established rules into operation.
  • ufw disable // Disables (pauses) the firewall.
  • ufw reset // Deletes all rules and allows you to start from scratch, with the exception of the default behavior defined in step 1.

That's it!

If you have followed the steps correctly, you will now be able to successfully configure UFW and use it on your server. In case you do not have a server, do not worry. Our cloud infrastructure with its own datacenter is here to cover all your needs.