Debian

How to configure ufw on Debian

Ufw is an uncomplicated firewall, but before going to the explanation of UFW let’s understand what a firewall is. The network traffic is monitored by the firewall and is also used to permit or to restrict the specific traffic according to the set rules, which allows the network to be secure from unwanted traffic. Now, what is the UFW? UFW deals with the iptables, it manages them and monitors them.

In this guide, we will discuss how to install and also the procedure to configure the ufw on Debian.

How to install and configure ufw on Debian

We will discuss the installation and configuration procedure of ufw on Debian and its version is 11.

Installation of ufw : First, we will upgrade the repository of Debian using the update command.

$ sudo apt update

To install the ufw on Debian we will execute the following command in the terminal.

$ sudo apt install ufw

Enabling the UFW : Before enabling the ufw let’s check its status, by default the status of ufw is always inactive.

$ sudo ufw status verbose

To enable or activate the ufw we will run the command.

$ sudo ufw enable

Default policies of ufw : By default, the ufw policies are restricted, and neither allows any incoming traffic nor outgoing traffic. We can change the policies by the command whose general syntax is:

$ sudo ufw default [policy] [chain]

The ufw policies are present at the path of /etc/default/ufw by accessing it we can make the changes or we can change policy settings by running the commands. We are denying the incoming traffic so none of the users can connect to our server.

$ sudo ufw default deny incoming

Similarly, to permit the outgoing:

$ sudo ufw default allow outgoing

Applications profiles in UFW : Whenever we install any software or application on Debian, it stores the application profiles at the path /etc/ufw/applications.d directory. If we want to display the list of the applications then we will run the following command:

$ sudo ufw app list

It is clear from the output that this command displays all the applications files which are installed on this machine. Now if we want to get the details of a specific application, let’s say we need all the details of the samba application then we will execute the command.

$ sudo ufw app info ‘Samba’

Enabling IPv4 and IPv6 ports : To check whether these two basic ports are enabled or not, we will open the file /etc/default/ufw:

$ sudo nano /etc/default/ufw

We will search the IPv4 and IPv6 ports and make them enabled by entering yes if they are not enabled.

Now press CTRL + S to save the file and CTRL + X to exit the editor. Reload the ufw so it can configure its new settings.

$ sudo ufw reload

Permitting ssh connection on ufw : We cannot enable the ufw firewall unless we do not permit the incoming ssh connection. To permit the ssh connection we will execute a command.

$ sudo ufw allow OpenSSH

By default, the ssh connection is listed on port 22 but if you are using some other port then you have to open it on the ufw firewall. For example, we are using port 4444 then enter the following command:

$ sudo ufw allow 4444/tcp

We will not run the following command because we are using the default port. We can also restrict the ssh connections as:

$ sudo ufw deny ssh/tcp

If you are using the customized port then you will have to execute the following syntax.

$ sudo ufw deny [port-number]/tcp

Opening of the ports : Now we will have to open the ports for the applications so they can run successfully on those ports. To understand it we will consider HTTP and enable port 80 for it, we can do this.

$ sudo ufw allow http

You can also use the port number instead of HTTP like this:

$ sudo ufw allow 80/tcp

If we want to set up the web server then along with HTTP we have to open the port for HTTPS which is on 443 port by default:

$ sudo ufw allow https

Using the port number.

$ sudo ufw allow 443/tcp

Permits port ranges on ufw : We can also set the port ranges for example we want to set a range of 4000-4500 for both the tcp and udp. We can set this by commands:

$ sudo ufw allow 4000:4500/tcp

Similarly, for udp:

$ sudo ufw allow 4000:4500/udp

Permit specific IP addresses : We can also permit only the specific IP addresses by using the command.

$ sudo ufw allow from 192.168.1.0

Permit specific IP address to a specific port : We can also permit the specific port to the specific IP address by the following command. Change the IP address as well 44 to your required IP address and port number.

$ sudo ufw allow from 1.1.1.1 to any port 44

Allow specific network interface : To allow connections of a specific network interface eth2 on a particular port let’s say 22 (SSH), execute the following command.

$ sudo ufw allow in on eth2 to any port 22

Deny connections on ufw : By default, all the connections are blocked on ufw as we discussed at the start but we have two more options either to block the connections for a specific range or specific port. To block the connections of the specific ip address, we use the command.

$ sudo ufw deny from 203.0.113.4/23

To block addresses on the specific port we use.

$ sudo ufw deny from 203.0.113.4/23 to any port 22

Deletion of ufw rules: We can delete the ufw rules either by the actual rule number or by the actual rule. But before this, we can list down all the rules.

$ sudo ufw status numbered

If we want to delete the Openssh using the rule number then we will use the following command.

$ sudo ufw delete 1

And if we want to delete rule 443 by its actual rule then run the command.

$ sudo ufw delete allow 443

Dry-run of ufw : We can dry run the ufw without making any changes in the firewall.

$ sudo ufw --dry-run enable

Reset command of ufw : We can reset the ufw by running the following command.

$ sudo ufw reset

The GUI of ufw

We can also install the GUI (graphical user interface) for the configuration of the ufw:

$ sudo apt install gufw

Now go to the search bar and type firewall.

Click on the icon and it will ask for the root password for the authentication:

A GUI will be open anywhere you can change the status of the ufw and also allow or deny the incoming and outgoing. To add the rule click on the rules option and click on the add symbol (+).

Another prompt will be open.

Choose the policy, according to your requirement, to allow, to deny, to reject, or to limit the incoming or outgoing.

Click on the direction, and choose in, if you are adding the rule for incoming, out if you are adding a rule for the outgoing, or choose both if you are adding a rule for both incoming and outgoing.

Choose the category,

Also, you can choose the subcategory:

Next, choose the application for this subcategory:

And press the Add button below.

You can also configure, by simply clicking on the Simple, by choosing the type of policy, its direction, protocol, and port number. Then press the Add button.

And if you want to add a rule for any certain range then click on Advanced tab.

Conclusion

To secure the network system is the primary need of every user so neither one can hack the network nor can harm the network system files, for this purpose the Linux distribution has the tool known as “ufw ” which is an uncomplicated firewall and it is very easy to use and it’s more efficient. In this guide, we have discussed the installation process of the ufw and also how we can configure the ufw on Debian by both the terminal and GUI method.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.