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.
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.
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.
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.
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.
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.
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
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.
Most systems have IPTables installed by default, but to be sure, you can run sudo apt install 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.
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
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.
Block attackers automatically: With CrowdSec you can detect brute-force attacks and scanners in your logs and block them automatically in the firewall.