Skip to content

Firewall Management - UFW

UFW stands for Uncomplicated Firewall.

It is an interface to manage the Netfilter firewall on Linux, which is the system’s packet filtering system. UFW was developed to simplify the process of configuring firewall rules through the iptables utility.

UFW is the most popular and recommended method to manage the firewall on Debian- and Ubuntu-based distributions.

Most UFW commands require superuser privileges (sudo).

Check if UFW is active and view the current rules:

ActionCommandExample Output
Check Statusufw statusStatus: inactive (or active)
Check Detailsufw status verboseLists all rules in detail.

It’s crucial to set the rules before enabling to avoid locking yourself out of the server.

ActionCommandNote
Enable UFWufw enableWarning: If you don’t have an allow ssh rule, you’ll lose remote access.
Disable UFWufw disableTurns off the firewall (not recommended).
Reset Rulesufw resetRemoves all user-defined rules.

Configure what happens to traffic that does not match any specific rule.

ActionCommandResult
Deny Incoming (Recommended)ufw default deny incomingNo external connections are allowed unless explicitly specified.
Allow Outgoingufw default allow outgoingYour server can initiate connections to the outside world.
GoalCommandNote
Allow SSH (Port 22)ufw allow sshUses the service name to open port 22/TCP.
Allow HTTP (Port 80)ufw allow httpOpens port 80/TCP.
Allow HTTPS (Port 443)ufw allow 443/tcpOpens by port number and protocol.
Specific Portufw allow 5432/udpOpens port 5432 only for the UDP protocol.
Specific IP Trafficufw allow from 192.168.1.100 to any port 3306Allows only the IP 192.168.1.100 to access port 3306 (MySQL).

Removal can be done by rule number or by rule text.

ActionCommandNote
Remove by Textufw delete allow httpRemoves the rule allowing http (80/TCP).
Remove by Numberufw status numbered ufw status delete [número]The first command returns a list with existing rules and their positions, the second removes the rule at the selected position.