UFW Firewall Setup & Hardening
Before you begin
- Applies to:Ubuntu and Debian hosts using UFW.
- Where to start:Start on the affected host and inspect the current firewall state. If connected remotely, keep console or recovery access available.
- What you need:Have administrative access and the approved management source, SSH port, and application ports before enabling the firewall.
Guide tools
The Uncomplicated Firewall (UFW) is a user-friendly frontend for iptables/nftables designed to make network security intuitive on Linux servers.
Check Current Firewall Status
DiagnosticsBefore modifying any rules, inspect the active status of UFW. On fresh Debian and Ubuntu installations, UFW is typically installed but remains disabled by default.
Running with the verbose flag displays default policies, logging levels, and existing rules:
sudo ufw status verbose❯ Illustrative Output (Results Vary)
Status: inactive
Allow Required Services Before Tightening Defaults
Rule DefinitionAdd SSH from the approved management source and any application ports this host serves before tightening the default policy. Replace the documentation-only IPv4 CIDR with the actual management subnet; add a separate IPv6 rule if administrators connect over IPv6. Keep HTTP and HTTPS rules only on a host that serves public web traffic. Do not enable UFW until all required rules are present.
# Replace the documentation CIDR in both commands; preview first, then apply:sudo ufw --dry-run allow from 192.0.2.0/24 to any port 22 proto tcp comment 'SSH from management network'sudo ufw allow from 192.0.2.0/24 to any port 22 proto tcp comment 'SSH from management network'
# Add these only if this host serves public web traffic:sudo ufw allow 80/tcp comment 'Allow HTTP'sudo ufw allow 443/tcp comment 'Allow HTTPS'❯ Illustrative Output (Results Vary)
The dry-run prints a generated ruleset without applying it. Each applied rule normally reports “Rule added”; UFW may also report “Rule added (v6)” when IPv6 is enabled.
Enforce Default Deny Posture
HardeningOnce the required service rules are in place, set Default Deny for unsolicited incoming traffic and allow outbound traffic. Review the current rules before enabling UFW; existing inbound services need explicit rules.
# Drop incoming connections by default:sudo ufw default deny incoming
# Allow outgoing connections by default:sudo ufw default allow outgoing❯ Illustrative Output (Results Vary)
Default incoming policy changed to ‘deny’
(be sure to update your rules accordingly)
Default outgoing policy changed to ‘allow’
(be sure to update your rules accordingly)
Enable UFW & Review Numbered Rules
ActivationCommit the configuration to the Linux kernel and enable UFW to start automatically on system reboot:
# Enable UFWsudo ufw enable
# Verify active numbered rulessudo ufw status numbered❯ Illustrative Output (Results Vary)
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
Status: active
To Action From
— ------ ----
[ 1] 22/tcp ALLOW IN 192.0.2.0/24 # SSH from management network
[ 2] 80/tcp ALLOW IN Anywhere # Allow HTTP
[ 3] 443/tcp ALLOW IN Anywhere # Allow HTTPS
Your feedback keeps these production procedures accurate and battle-tested.