Introduction to UFW
UFW (Uncomplicated Firewall) is a user-friendly interface for managing iptables firewall rules on Linux systems. It’s particularly well-suited for Debian and Ubuntu servers, providing a simplified way to configure your system’s firewall.
Installation
Install UFW using the following command:
Basic UFW commands
Check UFW status
This command shows if UFW is active and lists all current rules.
For a more detailed view, use:
Enable and disable UFW
Before enabling UFW, ensure you have configured rules to allow SSH access (port 22)
to prevent being locked out of your server.
Allow your actual SSH TCP port first (replace 22 if needed), then enable UFW:
To disable UFW:
Managing basic rules
Allow incoming connections
Allow specific ports:
Allow specific services by name:
After activation, verify a new SSH connection before closing your current session. See the UFW walkthrough for a complete sequence.
Deny incoming connections
Deny specific ports:
Delete rules
Delete rules by number:
- List rules with numbers:
- Delete a specific rule:
Delete rules by specification:
Advanced configuration
Allow specific IP addresses
Allow a specific IP address:
Allow an IP address to access a specific port:
Set default policies for incoming and outgoing traffic:
Best practices
-
Always allow SSH first
Before enabling UFW, ensure SSH access is allowed:
-
Use specific rules
Instead of allowing all traffic to a port, specify the service and protocol:
-
Regular audits
Regularly review your firewall rules:
-
Back up rules
Back up your UFW rules periodically:
Common configuration examples
Basic web server setup
Database server setup
Rate limiting
Enable rate limiting for SSH to prevent brute force attacks:
Troubleshooting
If you encounter issues:
-
Check UFW status
-
View UFW logs
-
Reset UFW
Resetting UFW deletes configured rules and disables it. Use this only when deliberately rebuilding the firewall with console access available:
Conclusion
UFW provides a straightforward way to manage your firewall rules on Debian and Ubuntu systems. Remember to always configure SSH access before enabling the firewall, and regularly audit your rules to maintain security. For more advanced configurations, consult the UFW documentation.Last modified on September 30, 2026