How to Set Up a Firewall on Your VPS: A Complete Guide

We earn commissions when you shop through the links below.

calendar_month March 17, 2025 schedule 5 min read visibility 2414 views
person
Valebyte Team
How to Set Up a Firewall on Your VPS: A Complete Guide
summarize

TL;DR

  • • Use iptables or firewalld to manage VPS traffic and block unauthorized connections via an SSH client.
  • • Set the default INPUT policy to DROP to block all incoming traffic except for explicitly allowed rules.
  • • Allow established and related connections to ensure ongoing network sessions are not interrupted.
  • • Save rules to /etc/iptables/rules.v4 to ensure the firewall configuration persists after a server reboot.
  • • Verify active firewall rules by running 'sudo iptables -L' to confirm the server is properly protected.
dns No server for this guide yet? Vultr, DigitalOcean, Contabo sellDeal WebHosting UK: 10% off managed VPS until Nov 30 Get deal →

How to Configure a Firewall on a VPS?

A digital shield protecting a server from cyber threats, symbolizing the critical role of a firewall. Security on the internet plays a huge role. We increasingly hear about cyberattacks and data leaks. One way to protect your server from hackers and malware is to configure a firewall. In this article, we'll tell you how to configure a firewall on a VPS hosting and ensure the security of your server.

A firewall is a program or hardware device that controls the flow of data between your server and the external network. It filters network traffic, blocking potentially dangerous connections.

To configure a firewall on a VPS, you'll need access to the server console. In most cases, you can use an SSH client, such as PuTTY, to connect to the server.

Step 1: Installing and Configuring the Firewall

The first step is to install and configure the firewall on your server. The most popular programs for this purpose are iptables and firewalld. Let's look at configuring iptables.

1. Install the iptables program on your server if it's not already there:

sudo apt-get install iptables

2. Create basic rules for the firewall:

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

sudo iptables -A INPUT -i lo -j ACCEPT

sudo iptables -P INPUT DROP

3. Save the firewall settings:

sudo iptables-save > /etc/iptables/rules.v4

Step 2: Checking Firewall Settings

After configuring iptables, you need to make sure that the firewall is working correctly and blocking malicious traffic. To do this, run the following command:

sudo iptables -L

You will see a list of rules that apply to your server. Make sure that all the rules are configured correctly and do not block necessary traffic.

dnsA server for this guide

Matching plans from verified providers, prices updated daily.

sell WebHosting UK: 10% off managed VPS · until Nov 30 Get deal →

Compare:Vultr vs DigitalOceanVultr vs ContaboAll VPS by price

Affiliate links: we may earn a commission, your price stays the same.

Step 3: Additional Firewall Configuration

In addition to basic rules, you can add additional rules to enhance the security of your server. For example, you can block specific ports or IP addresses, and also configure logging to track network traffic.

Don't forget to periodically update the firewall settings to protect against new threats. It is also recommended to create backups of the firewall rules, so that in case of failure, you can quickly restore the previous configuration.

Now you know how to configure a firewall on a VPS. Follow our instructions and provide reliable protection for your server!

Affiliate links: we may earn a commission, your price stays the same.