Installing and Configuring UFW on Debian: A Step-by-Step Guide

Uncomplicated Firewall (UFW) is a user-friendly interface for managing iptables on Linux systems. This guide walks you through installing and configuring UFW on Debian to secure your server.


1. Update Package List

Make sure your package index is up to date:

sudo apt-get update

2. Install UFW

Install the UFW package using:

sudo apt-get install ufw

3. Check UFW Status

Verify UFW’s current status:

sudo ufw status

4. Allow SSH Connections

Permit SSH connections to avoid locking yourself out:

sudo ufw allow ssh

5. Enable UFW

Enable the firewall:

sudo ufw enable

6. Configure UFW

  • Set Default Policies:

    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    
  • Allow HTTP and HTTPS Traffic:

    sudo ufw allow http
    sudo ufw allow https
    
  • Allow Specific Ports:

    sudo ufw allow 80
    sudo ufw allow 443
    
  • Allow Specific IP Addresses:

    sudo ufw allow from 203.0.113.4
    

7. Check UFW Status with Rules

View active rules and the status of UFW:

sudo ufw status verbose

8. Disable UFW

If you need to temporarily disable UFW:

sudo ufw disable

9. Reset UFW

To reset UFW back to its default settings:

sudo ufw reset

Managing UFW Logging

  • Disable UFW Logging:

    sudo ufw logging off
    
  • Prevent Logs from Appearing in Terminal:

    1. Edit the rsyslog configuration:

      sudo nano /etc/rsyslog.d/20-ufw.conf
      
    2. Uncomment the line containing & ~. This prevents UFW logs from being written to the kernel log (read by dmesg).

    3. Restart rsyslog:

      sudo systemctl restart rsyslog
      

Conclusion

With these steps, you can efficiently install, configure, and manage UFW on Debian to enhance your server’s security. Adjust the rules to meet your specific needs, and remember to regularly review your firewall configurations.

Related Posts