Skip to content

VPS: Managing the Firewall on Linux

Learn how to configure a firewall to secure your server and allow the external connections you need → Find out more

26 November 20264 min read64 Views0 helpful

Introduction

Many services and programs require you to allow external connections. The best way to do this is to set up a firewall.
In this guide, you'll learn how to configure a firewall and allow these connections.

Before you begin

To install the firewall, you need to connect to your server via SSH. If you don't know how, take a look at our initial connection guide.
After connecting, update your server to the latest version with apt update and apt upgrade.

Info

Additional network firewall in the ZapX dashboard This guide covers the firewall on your server (UFW/IPTables). In addition, your VPS has an extra network firewall in the ZapX dashboard (the Network Firewall tab), which is applied before traffic reaches your server. By default, it is set to ACCEPT for incoming and outgoing traffic, so it won't affect the steps below. However, if you've set more restrictive rules there yourself (e.g., a DROP/REJECT policy or custom firewall rules), you'll also need to allow the same ports there.

Using UFW (Easy)

There are several firewall programs for Linux. In this guide, we'll focus on the two most popular: UFW and IPTables.
We'll start with UFW because it's easier to set up and manage.

Installing UFW

You can easily install UFW via APT with sudo apt install ufw.
To avoid losing your SSH connection, don't enable the firewall yet. Configure it properly first.

Configuring UFW

We recommend the following default policies:

sudo ufw default deny incoming
and
sudo ufw default allow outgoing

Now you should allow your SSH connection and any other ports you want to open:

sudo ufw allow 22 for SSH

sudo ufw allow 80 as an example for HTTP

sudo ufw allow 25565 as an example for a Minecraft game server

To enable UFW, run sudo ufw enable. You can open additional ports at any time with sudo ufw allow PORT.

Adding port rules

To allow a port, you can simply use this command:

For TCP ports:

sudo ufw allow PORT/tcp – replace PORT with the port you want to allow.

Example: sudo ufw allow 25565/tcp for a Minecraft game server

For UDP ports:

sudo ufw allow PORT/udp – replace PORT with the port you want to allow.

Example: sudo ufw allow 9987/udp for a TeamSpeak 3 server

Viewing and removing port rules

To view all port rules, use sudo ufw status numbered.
To delete a rule, run sudo ufw delete NUMBER, where NUMBER is the rule's number in the list.

Installing IPTables (Advanced)

Most systems have IPTables installed by default, but to be sure, you can run sudo apt install iptables.

Configuring IPTables

First, set the default policies and settings:

sudo iptables -P INPUT DROP – block all incoming connections

sudo iptables -P FORWARD DROP – block all forwarded connections

sudo iptables -P OUTPUT ACCEPT – allow all outgoing connections

sudo iptables -A INPUT -i lo -j ACCEPT – allow loopback traffic

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT – allow all existing connections

Now you should allow SSH and any other ports you want to open:

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT for SSH

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT as an example for HTTP

sudo iptables -A INPUT -p tcp --dport 25565 -j ACCEPT as an example for a Minecraft game server

To keep the rules after a reboot, install the iptables-persistent package with sudo apt install iptables-persistent.
Save the rules with sudo netfilter-persistent save and enable automatic startup with sudo systemctl enable netfilter-persistent.

Adding port rules

To allow a port, you can use this command:

For TCP ports:

sudo iptables -A INPUT -p tcp --dport PORT -j ACCEPT – replace PORT with the port you want to allow.

Example: sudo iptables -A INPUT -p tcp --dport 25565 -j ACCEPT for a Minecraft game server

For UDP ports:

sudo iptables -A INPUT -p udp --dport PORT -j ACCEPT – replace PORT with the port you want to allow.

Example: sudo iptables -A INPUT -p udp --dport 9987 -j ACCEPT for a TeamSpeak 3 server

Viewing and removing port rules

You can view all rules with sudo iptables -L --line-numbers.
To delete a rule, use sudo iptables -D INPUT NUMBER, where NUMBER is the rule's number in the list.

Tip

Block attackers automatically: With CrowdSec you can detect brute-force attacks and scanners in your logs and block them automatically in the firewall.

Conclusion

You've successfully installed a firewall on your Linux server and created your own port rules. You can also use this guide to allow additional ports.
If you have any questions or run into problems, our support team is here to help every day! 🙂

Free tools

Related tools for servers and developers

View all

FAQ

Frequently asked questions

UFW and IPTables. The guide starts with UFW because it's easier to set up and manage.
Run 'sudo apt install ufw'.
Run 'sudo ufw default deny incoming' and 'sudo ufw default allow outgoing'. Then explicitly allow SSH and any other required ports (e.g., 'sudo ufw allow 22') before enabling UFW.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP-Hosting support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.