Back to Cheatsheets
Cheatsheet

UFW Firewall Cheatsheet

UFW (Uncomplicated Firewall)

UFW is the default frontend for iptables on Ubuntu/Debian. It simplifies firewall management with intuitive syntax.

Installation

# Ubuntu/Debian (usually pre-installed)
apt-get install ufw
 
# Arch Linux
pacman -S ufw

Basic Usage

# Enable/disable
ufw enable                           # Enable firewall
ufw disable                          # Disable firewall
ufw reload                           # Reload firewall
ufw reset                            # Reset to defaults
 
# Status
ufw status                           # Simple status
ufw status verbose                   # Detailed status
ufw status numbered                  # With rule numbers

Allow/Deny Rules

# By port number
ufw allow 22                         # Allow SSH
ufw allow 80                         # Allow HTTP
ufw allow 443                        # Allow HTTPS
ufw deny 23                          # Block Telnet
 
# By service name
ufw allow ssh                        # Allow SSH
ufw allow http                       # Allow HTTP
ufw allow https                      # Allow HTTPS
 
# By protocol
ufw allow 53/tcp                     # DNS TCP
ufw allow 53/udp                     # DNS UDP
 
# From specific IP
ufw allow from 192.168.1.100         # Allow all from IP
ufw allow from 192.168.1.0/24        # Allow subnet
 
# From IP to specific port
ufw allow from 192.168.1.100 to any port 22
 
# By application profile
ufw app list                         # List available profiles
ufw allow 'Nginx Full'               # Allow Nginx (80+443)
ufw allow 'OpenSSH'                  # Allow SSH

Advanced Rules

# Deny by direction
ufw deny in 22                       # Deny incoming SSH
ufw allow out 53                     # Allow outgoing DNS
 
# Limit connections (rate limit)
ufw limit ssh                        # Rate limit SSH (6 connections/30s)
ufw limit 22/tcp                     # Rate limit port 22
 
# Delete rules
ufw delete allow 80                  # Delete by rule
ufw delete deny 23
ufw delete 3                         # Delete by number (see `ufw status numbered`)
 
# Insert at position
ufw insert 1 allow 22                # Insert SSH as rule #1
 
# Default policies
ufw default deny incoming            # Block all incoming
ufw default allow outgoing           # Allow all outgoing
ufw default deny routed              # Block routed traffic

Logging

ufw logging on                       # Enable logging (default: low)
ufw logging off                      # Disable logging
ufw logging medium                   # Set log level: off|low|medium|high
 
# View logs
tail -f /var/log/ufw.log

Rules File

# Rules are stored in
/etc/ufw/                            # Main directory
/etc/ufw/user.rules                  # User-defined rules
/etc/ufw/user6.rules                 # IPv6 rules
/etc/ufw/before.rules                # Before hooks
/etc/ufw/after.rules                 # After hooks

UFW Cheatsheet

ufw enable                           # Turn on firewall
ufw disable                          # Turn off firewall
ufw status numbered                  # Show rules with IDs
ufw allow 22/tcp                     # Allow SSH
ufw deny 80                          # Block HTTP
ufw delete deny 80                   # Undo block
ufw limit ssh                        # Rate limit SSH
ufw default deny incoming            # Lockdown
ufw reload                           # Apply changes