How to Set Up a Firewall with UFW on Debian 12

UFW stands for Uncomplicated Firewall. It is a user-friendly command-line tool for managing firewall rules on Linux distributions, including Debian. UFW is designed to simplify the process of setting up and managing a firewall by providing a straightforward interface.

To set up a firewall using UFW on Debian 12, you can follow these steps:

Step 1 : Update System Packages

Before setting up the firewall, it's recommended to update your system packages to ensure you have the latest security patches. Open a terminal and run the following commands:

Step 2 : Install UFW

UFW is likely already installed on Debian 12, but you can confirm this by running the following command:

If it's already installed, the command will indicate that UFW is already the newest version. If it's not installed, it will be installed successfully.

Step 3 : Configure Firewall Rules

Once UFW is installed, you can start configuring the firewall rules. UFW has default rules that deny all incoming connections and allow all outgoing connections.

To enable specific services or ports, you can use the ufw allow command followed by the port number or service name. For example, to allow SSH connections, use the following command:

You can also allow incoming connections from a specific IP address or subnet. For example, to allow incoming connections from IP address 192.168.0.100, run the following command:

Step 4 : Enable UFW

After configuring the firewall rules, you can enable UFW by running the following command:

You will be prompted with a warning that enabling the firewall may disrupt existing SSH connections. Confirm the prompt by typing 'y' and pressing Enter.

Step 5 : Check Firewall Status

To check the status of UFW and view the currently active rules, you can run:

This will display the status of the firewall and the rules that are currently in effect.

Step 6 : Additional Configuration

You can further customize UFW to suit your specific needs. Some useful commands include:

- sudo ufw disable: Disables the firewall.

- sudo ufw reset: Resets the firewall to default settings (disabling and deleting all rules).

- sudo ufw delete <rule number>: Deletes a specific rule identified by its number.

Remember to consult the UFW documentation or use the man ufw command for more information on advanced configuration options.

You have successfully set up a firewall using UFW on Debian 12.