What is UFW?
UFW (Uncomplicated Firewall) is a simple yet powerful tool for managing firewall rules in Linux, especially on Debian-based systems like Ubuntu. It’s designed to make iptables easier to use.
1. Installing UFW
On most systems, UFW comes pre-installed. But just in case:
sudo apt update
sudo apt install ufw
2. Check UFW Status
Before configuring anything, let’s see if UFW is active:
sudo ufw status
Output example:
Status: inactive
3. Enable UFW
sudo ufw enable
💡 This will activate the firewall and keep it persistent across reboots.
4. Allowing SSH
To avoid getting locked out when enabling the firewall, always allow SSH first:
sudo ufw allow ssh
# or explicitly
sudo ufw allow 22/tcp
5. Allowing Other Services
Common examples:
# HTTP
sudo ufw allow 80/tcp
# HTTPS
sudo ufw allow 443/tcp
# Custom port (e.g., 8080)
sudo ufw allow 8080/tcp
6. Deny Access
Block a specific port:
sudo ufw deny 23
Block a specific IP:
sudo ufw deny from 203.0.113.10
7. Default Policies
Set default rules:
sudo ufw default deny incoming
sudo ufw default allow outgoing
This means:
- All incoming connections are blocked by default
- All outgoing connections are allowed
8. Application Profiles
List available app profiles:
sudo ufw app list
Allow profile by name:
sudo ufw allow "OpenSSH"
9. View Rules
sudo ufw status numbered
Example output:
Status: active
To Action From
-- ------ ----
[ 1] 22 ALLOW Anywhere
[ 2] 80 ALLOW Anywhere
10. Deleting Rules
Use the number from the previous command:
sudo ufw delete 2
11. Disable UFW
sudo ufw disable
12. Reset UFW
Reset all rules to default:
sudo ufw reset
Conclusion
UFW is an excellent tool for securing your Linux server without dealing with complex iptables rules. Just remember:
- Always allow SSH before enabling UFW
- Use numbered view to easily manage rules
- Keep your rules minimal and purposeful
Source:
https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands
https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts
https://blog.rtsp.us/ufw-uncomplicated-firewall-cheat-sheet-a9fe61933330?gi=6d2c2f87d0b1