Skip to content

UFW Firewall Setup & Hardening

Admin
October 1, 2026
5 min read
Intermediate
💻 Target OS: Ubuntu 24.04+ / Debian 12+ / RHEL 9+

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
Guide tools
0/0 Steps (0%)

The Uncomplicated Firewall (UFW) is a user-friendly frontend for iptables/nftables designed to make network security intuitive on Linux servers.


01

Check Current Firewall Status

Diagnostics

Before 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:

Terminal window
sudo ufw status verbose
❯ Illustrative Output (Results Vary)

Status: inactive


02

Allow Required Services Before Tightening Defaults

Rule Definition

Add 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.

Terminal window
# 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.


03

Enforce Default Deny Posture

Hardening

Once 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.

Terminal window
# 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)


04

Enable UFW & Review Numbered Rules

Activation

Commit the configuration to the Linux kernel and enable UFW to start automatically on system reboot:

Terminal window
# Enable UFW
sudo ufw enable
# Verify active numbered rules
sudo 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

Comments