Ubuntu: How to Set Up a Firewall with UFW
UFW (Uncomplicated Firewall) is a user-friendly interface for managing iptables on Ubuntu. It allows you to easily control incoming and outgoing traffic to secure your system. This guide walks you through enabling and configuring UFW on Ubuntu 20.04, 22.04, or 24.04 LTS.
Step-by-Step UFW Setup
1 Install UFW (if not already installed)
sudo apt update
sudo apt install ufw
2 Set Default Policies
sudo ufw default deny incoming
sudo ufw default allow outgoing
This blocks all incoming traffic by default and allows all outgoing traffic.
3 Allow Essential Services
- Allow SSH (port 22):
sudo ufw allow ssh
- Allow HTTP/HTTPS:
sudo ufw allow http
sudo ufw allow https
- Allow Custom Port:
sudo ufw allow 8080/tcp
4 Enable UFW
sudo ufw enable
Confirm with y when prompted.
5 Check Firewall Status
sudo ufw status verbose
6 Disable UFW (if needed)
sudo ufw disable
Tips & Best Practices
- Always allow SSH before enabling UFW to avoid locking yourself out
- Use ufw delete to remove rules
- Use ufw logging on to enable logging
- For GUI users, install gufw for graphical management
Note: UFW is ideal for simple setups. For advanced configurations, consider using iptables or firewalld.