UFW Firewall Setup Guide for Ubuntu
A firewall is the most important first step in securing any Linux server. UFW (Uncomplicated Firewall) is Ubuntu's user-friendly frontend for iptables.
1. Install and Check Status
UFW comes pre-installed on Ubuntu but is disabled by default:
sudo ufw status2. Set Default Policies
The safest approach is to deny all incoming traffic and allow all outgoing traffic, then selectively open the ports you need:
sudo ufw default deny incoming
sudo ufw default allow outgoing3. Allow SSH (Critical!)
WARNING: If you enable UFW without allowing SSH first, you will permanently lock yourself out of your server.
sudo ufw allow 22/tcpIf you use a custom SSH port (e.g., 2222):
sudo ufw allow 2222/tcp4. Allow Common Services
Add rules for the services you actually run:
# Web server (HTTP + HTTPS)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Minecraft
sudo ufw allow 25565/tcp
# FiveM
sudo ufw allow 30120/tcp
sudo ufw allow 30120/udp
# Teamspeak
sudo ufw allow 9987/udp5. Enable the Firewall
sudo ufw enableConfirm with y. The firewall is now active and will persist across reboots.
6. Useful Commands
sudo ufw status numbered # List all rules with numbers
sudo ufw delete 3 # Delete rule #3
sudo ufw allow from 192.168.1.0/24 # Allow an entire subnet
sudo ufw reload # Reload rules after changesLooking for the right Linux VPS?
Put your project into practice right away: Our KVM VPS provide full root access, lightning-fast NVMe storage, and reliable KVM isolation. Flexible prepaid billing with no subscription and personal Discord support.