What is UFW?
UFW (Uncomplicated Firewall) is a user-friendly frontend for iptables/nftables. It simplifies firewall management while providing robust protection.
Installation and Basics
sudo apt install -y ufw
# Check status
sudo ufw status verboseDefault Policies
# Deny all incoming, allow all outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoingAllow Common Services
# SSH (CRITICAL: do this before enabling UFW!)
sudo ufw allow ssh
# Or specific port
sudo ufw allow 2222/tcp
# Web traffic
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Using application profiles
sudo ufw allow "Nginx Full"Advanced Rules
# Allow from specific IP
sudo ufw allow from 198.51.100.10
# Allow from subnet to specific port
sudo ufw allow from 10.0.0.0/8 to any port 3306
# Allow port range
sudo ufw allow 6000:6010/tcp
# Deny specific IP
sudo ufw deny from 203.0.113.50Enable the Firewall
sudo ufw enableWarning: Make absolutely sure you have allowed SSH before enabling UFW, or you will lock yourself out.
Managing Rules
# List rules with numbers
sudo ufw status numbered
# Delete a rule by number
sudo ufw delete 3
# Delete by specification
sudo ufw delete allow 8080/tcp
# Reset all rules
sudo ufw reset