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
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:
Ubuntu
Debian
First, connect to your VPS using SSH.
Linux servers are accessed using:
OpenSSH
Example:
ssh username@your_server_ipOnce connected, you can begin configuring the firewall.
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 ufwThis will install the firewall management tool.
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 OpenSSHThis ensures that SSH traffic on port 22 is allowed.
Once SSH access is allowed, enable the firewall.
Run:
sudo ufw enableYou may see a confirmation message. Type y and press Enter.
The firewall will now start protecting your server.
You can verify that the firewall is active by running:
sudo ufw statusYou should see output showing that the firewall is active and that SSH connections are allowed.
If you run services on your VPS, you may need to open additional ports.
Examples include:
Allow HTTP (web traffic):
sudo ufw allow 80Allow HTTPS:
sudo ufw allow 443Allow a custom port:
sudo ufw allow 25565Opening the required ports ensures that your applications or game servers can accept connections.
A firewall helps protect your VPS by:
Blocking unauthorized access attempts
Limiting which ports are accessible from the internet
Reducing the risk of attacks
Without a firewall, unnecessary services may be exposed to the public internet.
To set up a firewall on your Linux VPS:
Install UFW
Allow SSH connections
Enable the firewall
Open additional ports if needed
Using a firewall is one of the simplest and most effective ways to improve the security of your VPS.