UFW

Table of Contents
├── Allow Rules
├── Delete Rules
├── IPv6
├── Logging

🔗Status and Control

TaskCommand
Show status and active rulesufw status
Show status with rule numbersufw status numbered
Show verbose status (defaults, logging)ufw status verbose
Enable firewallufw enable
Disable firewallufw disable
Reload rules without disablingufw reload
Reset to factory defaults (disables UFW)ufw reset

🔗Allow Rules

TaskCommand
Allow port (TCP + UDP)ufw allow 53
Allow port TCP onlyufw allow 22/tcp
Allow port UDP onlyufw allow 53/udp
Allow port rangeufw allow 6000:6007/tcp
Allow by service nameufw allow ssh
Allow from any IP to any portufw allow from any
Allow from specific IPufw allow from 192.168.1.10
Allow from IP to specific portufw allow from 192.168.1.10 to any port 22
Allow from subnetufw allow from 10.0.0.0/8
Allow from subnet to portufw allow from 10.0.0.0/8 to any port 443
Allow to specific interfaceufw allow in on eth0 to any port 80
Allow to specific interface and IPufw allow in on eth0 from 192.168.1.0/24

🔗Deny and Reject Rules

TaskCommand
Deny portufw deny 23/tcp
Deny from IPufw deny from 203.0.113.5
Deny from subnetufw deny from 203.0.113.0/24
Reject port (sends RST, not silent drop)ufw reject 23/tcp
Reject from IPufw reject from 203.0.113.5

🔗Limit (Rate Limiting)

Limits connections to 6 per 30 seconds from a single IP. Useful for SSH brute-force mitigation.

TaskCommand
Limit portufw limit 22/tcp
Limit by service nameufw limit ssh

🔗Delete Rules

TaskCommand
Delete by rule numberufw delete 3
Delete by rule specificationufw delete allow 80/tcp
Delete deny ruleufw delete deny 23

To get rule numbers: ufw status numbered


🔗Default Policies

Set the default behaviour for traffic that matches no explicit rule.

TaskCommand
Default deny incomingufw default deny incoming
Default allow outgoingufw default allow outgoing
Default deny outgoingufw default deny outgoing
Default deny forwardedufw default deny routed

Recommended baseline:

ufw default deny incoming
ufw default allow outgoing

🔗Application Profiles

UFW ships with named profiles for common services stored in /etc/ufw/applications.d/.

TaskCommand
List available profilesufw app list
Show profile detailsufw app info Nginx
Allow by profileufw allow 'Nginx Full'
Deny by profileufw deny 'Nginx Full'
Update profiles after package installufw app update all

🔗IPv6

Ensure IPV6=yes is set in /etc/default/ufw to apply rules to IPv6 as well. Rules apply to both stacks by default once enabled.

Allow on IPv6 only:

ufw allow in on eth0 from fe80::/10 to any port 22

🔗Logging

TaskCommand
Enable logging (low level)ufw logging on
Set log levelufw logging low / medium / high / full
Disable loggingufw logging off

Logs go to /var/log/ufw.log and syslog. Format: [UFW BLOCK] or [UFW ALLOW].


🔗Common Baselines

######### Server Baseline

ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw enable

######### Web Server

ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp

######### Allow LAN, Block WAN

ufw allow from 192.168.0.0/16
ufw deny from any

######### Restrict Outbound DNS to One Resolver

ufw default deny outgoing
ufw allow out to 9.9.9.9 port 53
ufw allow out to any port 80
ufw allow out to any port 443

🔗Inserting Rules at a Specific Position

Rules are matched in order. Insert before existing rules with:

ufw insert 1 allow from 10.0.0.1

🔗Flags and Options

FlagDescription
allowPermit matching traffic
denyDrop matching traffic silently
rejectDrop and send RST to sender
limitRate-limit (6 connections per 30s per IP)
inApply to incoming traffic
outApply to outgoing traffic
on <iface>Restrict rule to a network interface
from <ip>Match source address
to <ip>Match destination address
port <n>Match destination port
proto tcp/udpMatch protocol