How to Set Up a Firewall on a Linux VPS

Setting up a firewall is an important step when securing your Linux VPS. A firewall helps control incoming and outgoing network traffic and prevents unauthorized access to your server.

Most modern Linux distributions include a simple firewall management tool called:

UFW makes it easy to configure firewall rules without needing to manage complex firewall configurations manually.

This guide explains how to enable and configure a firewall on common Linux distributions such as:


Step 1: Connect to Your Server

First, connect to your VPS using SSH.

Linux servers are accessed using:

Example:

ssh username@your_server_ip

Once connected, you can begin configuring the firewall.


Step 2: Install UFW

On many systems, UFW is already installed. If it is not installed, you can install it using the package manager.

Run the following command:

sudo apt install ufw

This will install the firewall management tool.


Step 3: Allow SSH Connections

Before enabling the firewall, it is important to allow SSH connections. Otherwise, you could accidentally block access to your server.

Run the following command:

sudo ufw allow OpenSSH

This ensures that SSH traffic on port 22 is allowed.


Step 4: Enable the Firewall

Once SSH access is allowed, enable the firewall.

Run:

sudo ufw enable

You may see a confirmation message. Type y and press Enter.

The firewall will now start protecting your server.


Step 5: Check Firewall Status

You can verify that the firewall is active by running:

sudo ufw status

You should see output showing that the firewall is active and that SSH connections are allowed.


Opening Additional Ports

If you run services on your VPS, you may need to open additional ports.

Examples include:

Allow HTTP (web traffic):

sudo ufw allow 80

Allow HTTPS:

sudo ufw allow 443

Allow a custom port:

sudo ufw allow 25565

Opening the required ports ensures that your applications or game servers can accept connections.


Why a Firewall Is Important

A firewall helps protect your VPS by:

Without a firewall, unnecessary services may be exposed to the public internet.


Summary

To set up a firewall on your Linux VPS:

  1. Install UFW

  2. Allow SSH connections

  3. Enable the firewall

  4. Open additional ports if needed

Using a firewall is one of the simplest and most effective ways to improve the security of your VPS.