Skip to main content
Prerequisites:
  • Linux system with sudo/root access
  • Basic command line knowledge
UFW (Uncomplicated Firewall) is a user-friendly interface for managing iptables firewall rules on Linux. This guide will help you install, enable, and configure UFW to secure your system.

Installation

1

Install UFW

UFW is typically pre-installed on Ubuntu and Debian systems. If it’s not installed, use your package manager:Ubuntu/Debian:
CentOS/RHEL/Fedora:
2

Check UFW status

Verify that UFW is installed and check its current status:
If UFW is inactive, you’ll see Status: inactive. If it’s active, you’ll see a list of current rules.

Enabling UFW

Before enabling UFW, make sure you have SSH access configured, or you may lock yourself out of your server. Always allow SSH first!
1

Allow SSH (Critical!)

Before enabling UFW, allow SSH connections to prevent being locked out:
2

Enable UFW

Once SSH is allowed, enable UFW:
You’ll be prompted to confirm. Type y and press Enter.
3

Verify UFW is active

Check that UFW is now active:

Configuring Ports for Services

Configure UFW to allow traffic for your running services.

Common Service Ports

Allow HTTP and HTTPS traffic:
Or allow both at once:
Allow MySQL connections (default port 3306):
Only allow database access from trusted IPs in production. Use: sudo ufw allow from YOUR_IP to any port 3306
Allow PostgreSQL connections (default port 5432):
Allow traffic on any custom port:

Allow from Specific IP Addresses

To restrict access to specific IP addresses:

Blocking ICMP Echo Requests (Ping)

To block ping requests and make your server less visible to network scans:
1

Edit UFW before.rules

Open the UFW before.rules file:
2

Add ICMP blocking rule

Locate the line # ok icmp codes for INPUT and add the following rule right below it:
Save the file (Ctrl+O, Enter, Ctrl+X in nano).
3

Reload UFW

Apply the changes:
After this change, your server will not respond to ping requests, which can help prevent some types of network scanning and attacks.

Managing Firewall Rules

Viewing Rules

Deleting Rules

First, list rules with numbers:
Then delete by number:
Replace 3 with the rule number you want to delete.
Delete a rule by specifying it exactly:
UFW will prompt you to confirm the deletion.

Resetting UFW

To remove all rules and start fresh:
This will delete all UFW rules and disable the firewall. You’ll need to reconfigure it afterward.

Disabling UFW

To temporarily disable UFW (rules are preserved):
To re-enable:

Best Practices

Default Policies

Set default deny policies:

Rate Limiting

Enable rate limiting for SSH:

Logging

Enable logging to monitor firewall activity:
Logs are stored in /var/log/ufw.log

Regular Audits

Regularly review your firewall rules:

Troubleshooting

If you’re locked out, you’ll need console access (KVM, VNC, or physical access):
  1. Access the server console
  2. Disable UFW: sudo ufw disable
  3. Reconfigure SSH access: sudo ufw allow ssh
  4. Re-enable UFW: sudo ufw enable
Check if the rule was added correctly:
Make sure the service is running and listening on the correct port:
Check UFW status and logs:
Ensure UFW service is enabled:
Need more help? Check the UFW manual: man ufw or visit the UFW documentation.
Last modified on January 26, 2026