UFW Commands

Uncomplicated Firewall (UFW) commands for easy firewall management on Ubuntu/Debian.

Basic Operations

sudo ufw status

Check firewall status

sudo ufw status verbose

Show detailed firewall status

sudo ufw status numbered

Show rules with numbers

sudo ufw enable

Enable firewall

sudo ufw disable

Disable firewall

sudo ufw reload

Reload firewall rules

sudo ufw reset

Reset firewall to default settings

Default Policies

sudo ufw default deny incoming

Deny all incoming traffic by default

sudo ufw default allow outgoing

Allow all outgoing traffic by default

sudo ufw default deny outgoing

Deny all outgoing traffic by default

sudo ufw default allow incoming

Allow all incoming traffic by default

sudo ufw default reject incoming

Reject all incoming traffic by default

Allow Rules

sudo ufw allow 22

Allow port 22 (SSH)

sudo ufw allow 80/tcp

Allow port 80 TCP (HTTP)

sudo ufw allow 443/tcp

Allow port 443 TCP (HTTPS)

sudo ufw allow ssh

Allow SSH service

sudo ufw allow http

Allow HTTP service

sudo ufw allow https

Allow HTTPS service

sudo ufw allow 1000:2000/tcp

Allow TCP port range 1000-2000

sudo ufw allow from 192.168.1.0/24

Allow from specific subnet

sudo ufw allow from 192.168.1.100 to any port 22

Allow SSH from specific IP

sudo ufw allow in on eth0 to any port 80

Allow HTTP on specific interface

Deny Rules

sudo ufw deny 23

Deny port 23 (Telnet)

sudo ufw deny 3306/tcp

Deny MySQL port

sudo ufw deny from 192.168.1.100

Deny specific IP address

sudo ufw deny from 10.0.0.0/8

Deny entire subnet

sudo ufw deny out 25

Deny outgoing SMTP traffic

sudo ufw deny proto tcp from any to any port 445

Deny SMB traffic

Delete Rules

sudo ufw delete allow 80

Delete allow rule for port 80

sudo ufw delete deny 23

Delete deny rule for port 23

sudo ufw delete 3

Delete rule number 3 (use status numbered first)

sudo ufw delete allow from 192.168.1.100

Delete specific allow rule

Application Profiles

sudo ufw app list

List available application profiles

sudo ufw app info "Apache Full"

Show info about Apache profile

sudo ufw allow "Apache Full"

Allow Apache Full profile

sudo ufw allow "Nginx Full"

Allow Nginx Full profile

sudo ufw allow "OpenSSH"

Allow OpenSSH profile

sudo ufw delete allow "Apache"

Delete Apache profile rule

Advanced Rules

sudo ufw limit ssh

Rate limit SSH connections (max 6 in 30s)

sudo ufw limit 22/tcp

Rate limit port 22

sudo ufw reject out to 192.168.1.100

Reject outgoing to specific IP

sudo ufw allow from 192.168.1.0/24 to any app Apache

Allow subnet to Apache

sudo ufw insert 1 allow from 192.168.1.50

Insert rule at position 1

sudo ufw prepend deny from 10.0.0.0/8

Prepend rule to beginning

Rule Comments

sudo ufw allow 22 comment "SSH access"

Allow port with comment

sudo ufw allow from 192.168.1.0/24 comment "Local network"

Allow subnet with comment

sudo ufw deny 3306 comment "Block MySQL"

Deny port with comment

sudo ufw allow 80/tcp comment "Web server HTTP"

Allow HTTP with comment

sudo ufw allow 443/tcp comment "Web server HTTPS"

Allow HTTPS with comment

sudo ufw allow from 10.0.0.5 to any port 22 comment "Admin SSH"

Allow specific IP with comment

sudo ufw show added

Show rules with comments

sudo ufw status verbose

View rules (comments shown in raw files)

Logging

sudo ufw logging on

Enable logging

sudo ufw logging off

Disable logging

sudo ufw logging low

Set logging level to low

sudo ufw logging medium

Set logging level to medium

sudo ufw logging high

Set logging level to high

sudo ufw logging full

Set logging level to full

sudo tail -f /var/log/ufw.log

View UFW log file

IPv6 Support

sudo ufw allow from ::1

Allow IPv6 localhost

sudo ufw allow from 2001:db8::/32

Allow IPv6 subnet

sudo ufw deny from 2001:db8::1

Deny specific IPv6 address

Configuration Files

sudo nano /etc/ufw/ufw.conf

Edit main UFW configuration

sudo nano /etc/default/ufw

Edit UFW defaults

sudo nano /etc/ufw/before.rules

Edit rules processed before UFW rules

sudo nano /etc/ufw/after.rules

Edit rules processed after UFW rules

sudo nano /etc/ufw/user.rules

Edit user-defined rules