Prerequisites:
- Linux system with sudo/root access
- Basic command line knowledge
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
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
Web Server (HTTP/HTTPS)
Web Server (HTTP/HTTPS)
Allow HTTP and HTTPS traffic:Or allow both at once:
Database (MySQL/MariaDB)
Database (MySQL/MariaDB)
Allow MySQL connections (default port 3306):
PostgreSQL
PostgreSQL
Allow PostgreSQL connections (default port 5432):
Custom Application Ports
Custom Application Ports
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 Save the file (Ctrl+O, Enter, Ctrl+X in nano).
# ok icmp codes for INPUT and add the following rule right below it: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
Delete by rule number
Delete by rule number
First, list rules with numbers:Then delete by number:Replace
3 with the rule number you want to delete.Delete by rule specification
Delete by rule specification
Delete a rule by specifying it exactly:UFW will prompt you to confirm the deletion.
Resetting UFW
To remove all rules and start fresh:Disabling UFW
To temporarily disable UFW (rules are preserved):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.logRegular Audits
Regularly review your firewall rules:
Troubleshooting
Locked out of SSH
Locked out of SSH
If you’re locked out, you’ll need console access (KVM, VNC, or physical access):
- Access the server console
- Disable UFW:
sudo ufw disable - Reconfigure SSH access:
sudo ufw allow ssh - Re-enable UFW:
sudo ufw enable
Service not accessible after adding rule
Service not accessible after adding rule
Check if the rule was added correctly:Make sure the service is running and listening on the correct port:
UFW not starting
UFW not starting
Check UFW status and logs:Ensure UFW service is enabled:
Need more help? Check the UFW manual:
man ufw or visit the UFW documentation.