Skip to content

Overview

UFW (Uncomplicated Firewall) is a user-friendly interface for managing firewall rules using iptables. It simplifies firewall configuration and is commonly used for:

  • Securing servers

  • Controlling network traffic

  • Restricting access to services

  • Basic cybersecurity defense

By default, UFW is disabled.


Installation

sudo apt-get install ufw

Explanation
Installs the UFW firewall package on the system.


Enable / Disable and Status

Enable UFW

sudo ufw enable

Explanation

  • Activates firewall

  • Applies rules

  • Enables at startup


Disable UFW

sudo ufw disable

Explanation
Stops firewall and disables it at startup.


Check Status

sudo ufw status

Verbose Status

sudo ufw status verbose

Explanation
Displays detailed information including default policies.


Numbered Rules

sudo ufw status numbered

Explanation
Shows rules with numbers for easy deletion.


Default Policies

Allow All Incoming Connections

sudo ufw default allow incoming

sudo ufw default deny incoming

Allow All Outgoing Connections

sudo ufw default allow outgoing

Deny All Outgoing Connections

sudo ufw default deny outgoing

Best Practice

sudo ufw default deny incoming
sudo ufw default allow outgoing

Explanation
Blocks unauthorized incoming traffic while allowing outbound connections.


Allow and Deny Rules

List Available Applications

sudo ufw app list

Allow a Service

sudo ufw allow ssh

Explanation
Allows SSH connections.


Deny a Service

sudo ufw deny ssh

Port-Based Rules

Allow Port

sudo ufw allow 80

Allow TCP Port

sudo ufw allow 80/tcp

Allow UDP Port

sudo ufw allow 80/udp

Deny Port

sudo ufw deny 80

Deny TCP/UDP Ports

sudo ufw deny 80/tcp
sudo ufw deny 80/udp

Advanced Rules

Allow from Specific IP

sudo ufw allow from 192.168.0.1

Allow from Subnet

sudo ufw allow from 192.168.0.0/24

Allow Specific IP to Specific Port

sudo ufw allow from 192.168.0.1 to any port 22

Allow with Protocol

sudo ufw allow from 192.168.0.1 to any port 22 proto tcp

Deny Rules

Same syntax applies using deny instead of allow.


Deleting and Resetting Rules

Delete Rule by Number

sudo ufw delete 1

Explanation
Deletes rule number 1 from the list.


Reset Firewall

sudo ufw reset

Explanation

  • Removes all rules

  • Resets to default state

  • Use carefully


Practical Examples

Secure Server Setup

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable

Allow Web Traffic

sudo ufw allow 80
sudo ufw allow 443

Restrict SSH to Specific IP

sudo ufw allow from 192.168.1.10 to any port 22

Important Notes

  • Always allow SSH before enabling UFW to avoid lockout

  • Use numbered rules for easy management

  • Prefer restrictive rules (deny by default)

  • Test rules before deploying in production


Summary Table

Command Purpose
ufw enable Enable firewall
ufw disable Disable firewall
ufw status Check status
ufw allow Allow traffic
ufw deny Deny traffic
ufw delete Remove rule
ufw reset Reset firewall
ufw default Set default policy

Conclusion

UFW is a powerful yet simple firewall tool that helps:

  • Secure Linux systems

  • Control incoming and outgoing traffic

  • Prevent unauthorized access

Mastering UFW is essential for system administrators and cybersecurity professionals to maintain a secure environment.