Back to all guides
Server Administration

UFW Firewall Setup Guide for Ubuntu

3 read2026-04-10

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:

bash
sudo ufw status

2. Set Default Policies

The safest approach is to deny all incoming traffic and allow all outgoing traffic, then selectively open the ports you need:

bash
sudo ufw default deny incoming
sudo ufw default allow outgoing

3. Allow SSH (Critical!)

WARNING: If you enable UFW without allowing SSH first, you will permanently lock yourself out of your server.

bash
sudo ufw allow 22/tcp

If you use a custom SSH port (e.g., 2222):

bash
sudo ufw allow 2222/tcp

4. Allow Common Services

Add rules for the services you actually run:

bash
# 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/udp

5. Enable the Firewall

bash
sudo ufw enable

Confirm with y. The firewall is now active and will persist across reboots.

6. Useful Commands

bash
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 changes
KVM Linux VPS

Looking 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.

Full Root Access (SSH)
Datacenter NVMe SSD
4+ Tbps DDoS Protection
100% Prepaid (No Subscription)