This guide provides a step-by-step tutorial on how to configure the Uncomplicated Firewall (UFW) on Ubuntu and Debian systems. UFW is designed to simplify the process of configuring iptables firewall rules, making it easier for users to manage their network security.

Enabling UFW

To start using UFW, you first need to enable it. This action will activate the firewall with default settings that deny all incoming connections and allow all outgoing connections.

  1. Open a terminal window on your Ubuntu or Debian system.
  2. Run the following command to enable UFW:
    sudo ufw enable
  3. You will be prompted to confirm enabling UFW. Type y and press Enter.

Allowing/Denying Ports

After enabling UFW, you can specify which ports should be allowed or denied. This is crucial for allowing legitimate traffic while blocking unwanted access.

Allowing a Port

To allow a specific port, use the allow command followed by the port number and protocol (TCP or UDP).

  1. For example, to allow HTTP traffic on port 80:
    sudo ufw allow 80/tcp
  2. To allow HTTPS traffic on port 443:
    sudo ufw allow 443/tcp

Denying a Port

To deny access to a specific port, use the deny command.

  1. For example, to deny all traffic on port 25:
    sudo ufw deny 25/tcp

Allowing SSH Connections

When configuring a firewall, it's essential to allow SSH connections if you plan to manage the server remotely. However, be cautious as allowing SSH can expose your system to brute-force attacks.

  1. To allow SSH on the default port 22:
    sudo ufw allow 22/tcp
  2. If you are using a non-standard SSH port, replace 22 with your custom port number.

Checking UFW Status and Rules

To verify that UFW is active and to see the rules you have set up, use the following command:

sudo ufw status verbose

This will display whether UFW is active and list all the rules currently in place.

Logging with UFW
  1. To enable logging:
    sudo ufw logging on
  2. The logs will be stored in /var/log/ufw.log. You can view them using a command like:
    tail -f /var/log/ufw.log

Deleting UFW Rules

If you need to remove a rule, you can do so by specifying the rule number or details.

  1. To delete a specific rule, first check the list of rules with their numbers:
    sudo ufw status numbered
  2. Delete the rule using its number. For example, to delete rule number 3:
    sudo ufw delete 3

Troubleshooting

  • Issue: UFW is blocking legitimate traffic.
    Solution: Check the rules using ufw status verbose. Ensure that you have allowed all necessary ports and protocols.
  • Issue: Cannot connect via SSH after enabling UFW.
    Solution: Verify that port 22 (or your custom SSH port) is allowed. You can also temporarily disable UFW to test connectivity:
    sudo ufw disable

By following this guide, you should be able to configure UFW on your Ubuntu or Debian system effectively, ensuring that your network traffic is secure and only the necessary services are accessible.