Article Background
Back to Knowledgebase

How to Set Up a Firewall on Your Ubuntu VPS with UFW

VPS
Cubes Support
July 20, 2026

Securing your Cubes Hosting Ubuntu VPS with a firewall is essential for protecting your server from port scans, brute-force SSH attacks, and unauthorized access to background services.

UFW (Uncomplicated Firewall) is the default firewall configuration interface for Ubuntu. It provides a simple, human-readable command-line interface over iptables rules.

This guide covers installing UFW, setting default security policies, allowing common game and web ports, and managing firewall rules safely.


⚠️ CRITICAL STEP: Always Allow SSH Before Enabling UFW!

WARNING: Enabling UFW without first allowing your SSH port will instantly lock you out of your VPS! Always execute Step 3 (Allowing SSH) before running sudo ufw enable.


Step 1: Check UFW Status & Install

  1. Connect to your VPS via SSH as root or a user with sudo privileges.
  2. Check if UFW is installed and active:
    sudo ufw status
    

    (By default on new Ubuntu installations, status will report Status: inactive).
  3. If UFW is not installed, install it via apt:
    sudo apt update && sudo apt install ufw -y
    

Step 2: Set Default Firewall Policies

A secure firewall configuration follows a Default Deny rule: block all incoming connections by default, while allowing all outgoing traffic from your server.

Run these commands to set the default policies:

# Block all uninvited incoming connections
sudo ufw default deny incoming

# Allow all outgoing connections initiated by your VPS
sudo ufw default allow outgoing

Step 3: Allow SSH Connection (Port 22)

Before turning on the firewall, authorize SSH connections so your current terminal session remains connected:

# Allow standard SSH port 22
sudo ufw allow 22/tcp

# OR allow by service name
sudo ufw allow ssh

(If you changed your SSH port to a custom port like 2222, run sudo ufw allow 2222/tcp instead).


Step 4: Allow Ports for Your Hosted Services

Depending on what applications or game servers you run on your VPS, open the required ports:

Web Hosting (HTTP & HTTPS)

sudo ufw allow 80/tcp    # HTTP
sudo ufw allow 443/tcp   # HTTPS (SSL)

Minecraft Server

sudo ufw allow 25565/tcp  # Java Edition
sudo ufw allow 19132/udp  # Bedrock Edition

BeamMP Server

sudo ufw allow 30814/tcp
sudo ufw allow 30814/udp

FiveM Server & txAdmin

sudo ufw allow 30120/tcp  # Game port
sudo ufw allow 30120/udp  # Game port
sudo ufw allow 40120/tcp  # txAdmin Web Panel

Terraria & Unturned

sudo ufw allow 7777/tcp   # Terraria
sudo ufw allow 27015/udp  # Unturned / Steam Query

Step 5: Enable UFW Firewall

Once you have added SSH and your required service ports, enable UFW:

sudo ufw enable

(Press y and Enter when prompted: Command may disrupt existing ssh connections. Proceed with operation?).

To verify your active rules, run:

sudo ufw status verbose

Step 6: Advanced Rules (IP Whitelisting & Deleting Rules)

Restrict SSH Access to Your Home IP Address:

For maximum security, restrict SSH Port 22 so that only your home or office IP address can attempt SSH logins:

sudo ufw allow from 203.0.113.50 to any port 22 proto tcp

(Replace 203.0.113.50 with your actual public IP address).

How to Delete a Firewall Rule:

  1. List all rules with their line numbers:
    sudo ufw status numbered
    
  2. Delete a rule by its number:
    sudo ufw delete 2
    

Disabling or Resetting UFW:

  • Temporarily Turn Off UFW: sudo ufw disable
  • Reset All Rules to Default: sudo ufw reset

Conclusion

Configuring UFW on your Cubes Hosting Ubuntu VPS takes less than two minutes and provides a strong defense against automated attacks. Always remember to allow SSH Port 22 first, open only the ports your services require, and keep your VPS secure!

Special Offer

Ready to Start Your Server?

Get 10% OFF your first month on any server with promo code CUBES10 at checkout!

24/7 Assistance

Need Help With Your Server?

Already a Cubes Hosting customer and couldn't find your answer? Our dedicated support team is available 24/7.