How to Set Up a Firewall with UFW on Debian 12

In today’s world, it is very important to set up a firewall for network security because the firewall of the computer behaves as the protective layer between the computer and the threats. 

One of the user-friendly firewalls is UFW (Uncomplicated Firewall) and also a very popular firewall in Linux distributions including Ubuntu and Debian. Setting up a firewall with the UFW on Debian 12 by method will be explained in this post. 

What is the Method to Install UFW on Debian 12?

Install the UFW on Debian 12 by using its available package in the default repository on Debian 12. 

Execute the commands mentioned in the steps next to install UFW on Debian 12.

Step 1: Update the Packages

Update all the packages of Debian 12 to ensure that the updated package of UFW will be install:

$ sudo apt update

Step 2: Install UFW

After making sure all the packages are up to date, install the UFW:

$ sudo apt install ufw -y

Step 3: Enable the UFW

By default the UFW is “inactive” and to make it active, enable it:

$ sudo ufw enable

Note: To disable the UFW, use the below-mentioned command:

$ sudo ufw disable

Step 4: Check the Status of UFW

To check the status of the UFW on Debian 12, use the command:

$ sudo ufw status

The above output verifies the installation of the UFW as well as it is in “active” status.

How to Define the Rules of the UFW on Debian 12?

The network traffic on Debian 12 can be managed by defining some UFW rules. The explanation of the rules of UFW are:

  • The “allow” will grant access to incoming or outgoing traffic to the mentioned ports, services, or IP address
  • The “deny” will stop access to incoming or outgoing traffic to the mentioned ports, services, or IP address
  • The ports can be specified by the port numbers like “22” or by their name like “SSH
  • The specific IP address can be specified or use the “any” word to allow all the IP addresses
  • By default, UFW blocks all the IP addresses on the computer

How to Set the Default Rules of UFW on Debian 12?

To set the default rules of UFW on Debian 12, consider the mentioned-below rules. 

Allow all Outgoing Connections

By default, UFW denies all the outgoing connections and to allow the outgoing connections, use the command:

$ sudo ufw default allow outgoing

Block all Incoming Connections

To block all the incoming connections with UFW, run the command:

$ sudo ufw default deny incoming

Allow a Specific Port

To allow the network traffic on the specific port with the UFW, use either a port number or port name:

$ sudo ufw allow 22
$ sudo ufw allow ssh

Deny a Specific Port

To deny the network traffic on the specific port with the UFW, similar to the allow rule, either use the port number or port name. For example, “ftp” service can be denied by using its name or port number which is 443:

$ sudo ufw deny 443

Allow/Deny the IP Address

To allow/deny the traffic from the specific IP address, use the command:

$ sudo ufw allow from 192.168.9.12
$ sudo ufw deny from 192.168.9.12

Similarly to allow/deny the traffic to the specific IP address, use the command:

$ sudo ufw allow to 192.168.9.12
$ sudo ufw deny to 192.168.9.12

After defining the rules, restart the UFW to apply all the changes:

$ sudo systemctl restart ufw

Display the Status

To display the UFW status and its active rules, execute the command:

$ sudo ufw status verbose

Reset the UFW Rules

To reset all the rules of UFW to its default rules, run the command:

$ sudo ufw reset

How to Install the UFW’s GUI Package on Debian 12?

The UFW can also be installed with its GUI package. To install the GUI package of the UFW on Debian 12, run the command:

$ sudo apt install gufw -y

Now, search for the “GUFW” in the application’s menu and launch it:

Now with the toggle button and the dropdown list, one can easily manage the UFW using the GUI tool:

How to Uninstall UFW on Debian 12?

To uninstall both the GUI and command-line package of the UFW, execute the command:

$ sudo apt purge ufw gufw -y

That’s all about setting up a Firewall with UFW on Debian 12.

Conclusion

To set up the Firewall with the UFW on Debian 2, install it with “sudo apt install ufw -y”. Then allow and deny the network traffic to specific ports by defining rules. This post has explained the installation of UFW on Debian 12. Also, it explains the basic commands of UFW to define the rules for managing network traffic.