How to Secure Your Linux VPS with UFW Firewall (Ubuntu 22.04 & 24.04 Guide)

A firewall is one of the first security measures you should configure after deploying a new VPS. UFW (Uncomplicated Firewall) is Ubuntu's official firewall management tool and provides an easy way to protect your server from unauthorized access.

This guide shows how to install, configure, and manage UFW on Ubuntu 22.04 and Ubuntu 24.04.

Requirements

  • Ubuntu 22.04 or Ubuntu 24.04 VPS
  • Root or sudo privileges
  • SSH access to your server

Step 1: Update Your Server

sudo apt update
sudo apt upgrade -y

Step 2: Install UFW

sudo apt install ufw -y

Step 3: Check Firewall Status

sudo ufw status verbose

By default, UFW is disabled.

Step 4: Allow SSH Access

Important: Always allow SSH before enabling the firewall to avoid locking yourself out.

sudo ufw allow OpenSSH

Or if your SSH server uses a custom port:

sudo ufw allow 2222/tcp

Step 5: Allow Common Services

HTTP:

sudo ufw allow 80/tcp

HTTPS:

sudo ufw allow 443/tcp

Mail Server (optional):

sudo ufw allow 25/tcp
sudo ufw allow 465/tcp
sudo ufw allow 587/tcp

Step 6: Enable UFW

sudo ufw enable

Confirm by typing:

y

Step 7: Verify Active Rules

sudo ufw status numbered

Block a Port

sudo ufw deny 23/tcp

Delete a Firewall Rule

sudo ufw delete allow 80/tcp

Limit SSH Login Attempts

Reduce brute-force attacks with rate limiting:

sudo ufw limit OpenSSH

Allow Only Specific IP Addresses

sudo ufw allow from 203.0.113.10 to any port 22

Disable UFW

sudo ufw disable

Recommended Firewall Rules

  • Allow only required ports.
  • Keep SSH on a custom port if possible.
  • Use SSH key authentication.
  • Combine UFW with Fail2Ban.
  • Regularly review firewall rules.

Why Use UFW?

  • Simple firewall management.
  • Protects against unauthorized access.
  • Supports IPv4 and IPv6.
  • Works perfectly with Ubuntu servers.
  • Minimal resource usage.

Conclusion

Configuring UFW is one of the easiest ways to improve your VPS security. A properly configured firewall blocks unnecessary traffic while allowing only trusted services to remain accessible.

If you're looking for a secure Linux VPS with dedicated IPv4, full root access, NVMe storage, instant deployment, and cryptocurrency payment support, VPS For Life provides reliable VPS hosting for developers, businesses, and email infrastructure worldwide.

  • UFW Firewall
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Set Up a Firewall on Your VPS for Better Security

Setting up a firewall on your VPS helps protect it from unauthorized access and attacks. Here’s...

Why You Should Disable Root Login on Your VPS

By default, the root user has full control over your VPS. Disabling direct root login helps...

How to Enable Automatic Security Updates on Your VPS

Automatic security updates ensure your VPS is always protected against the latest...

How to Back Up Your VPS to Prevent Data Loss

Backups are essential for protecting your VPS data from accidental loss or security incidents....

How to Install Fail2Ban on Ubuntu 24.04 VPS

Fail2Ban is one of the most effective security tools for Linux VPS servers. It monitors log files...