Skip to content

VPS: Opening Ports and Managing the Firewall on Linux

Open ports on your Linux VPS: configure UFW and IPTables, check the network firewall and DDoS protection, and find blocked ports step by step.

29 November 202610 min read73 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.
If a port is still unreachable despite a rule, the step-by-step checks below can help. They cover every point between the internet and your service, including the network firewall and DDoS protection in the ZapX dashboard.

Before you start

To install the firewall, you need to connect to your server via SSH. If you’re not sure how to do that, check out our initial connection guide.
After connecting, you should update the server by running apt update and apt upgrade.

Info: Additional network firewall in the ZapX dashboard

This guide covers the firewall on your server (UFW/IPTables). Your VPS also has an additional network firewall in the ZapX dashboard (the Network firewall tab), which sits in front of your server. By default, it is set to ACCEPT for inbound and outbound traffic, so it won’t affect the steps below. However, if you’ve set more restrictive rules there yourself (such as 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 ones: UFW and IPTables.
We’ll start with UFW, as 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 use.

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 use.

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, replacing NUMBER with the rule’s number in the list.

Installing IPTables (Advanced)

Most systems have IPTables installed by default, but to be safe, 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 restart, install the iptables-persistent package with sudo apt install iptables-persistent.
Save the rules with sudo netfilter-persistent save and enable autostart 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 use.

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 use.

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, replacing NUMBER with the rule’s number in the list.

Tip

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

Port unreachable: Check step by step

There are several points between the internet and your service where connections can be blocked. A port is reachable only if each of them allows the traffic through. Follow the steps in order, replacing 25565 with your port.

  1. Check the service: Run sudo ss -tulpn | grep 25565. If no line appears, the service isn’t running or is using a different port. If the line shows 127.0.0.1:25565, the service is only reachable from the server itself. Change its configuration to 0.0.0.0 (all IPv4 addresses) or [::] (IPv6).
  2. Check the firewall on the server: Use sudo ufw status numbered or sudo iptables -L INPUT -n --line-numbers to see whether a rule exists for the port. Make sure it uses the right protocol: many games need UDP, while web and database services use TCP.
  3. Check the network firewall in the ZapX dashboard: Open your VPS and the Network firewall tab. If Default Inbound is set to DROP or REJECT, or one of your rules blocks the port, add a rule there with the ACCEPT action for the port and protocol.
  4. Test from outside: Check the port with our Port Checker. For TCP, the result is conclusive. An open UDP port often doesn’t respond to the test request, so only a “closed” result is conclusive. For this reason, it’s best to test UDP services directly with the game or program.
  5. Check DDoS protection: If the port is still unreachable, review the learned rules in the DDoS protection tab, as described in the next section.
Tip

You can find out which standard port each service uses, and see the corresponding firewall commands, in our Well-known Ports & Firewall Commands overview.

DDoS protection and custom ports

At our locations in Germany and the US, PletX DDoS protection filters incoming traffic before it reaches your VPS. The filter automatically detects the protocol for each port as soon as real traffic arrives and creates a rule for it, such as Minecraft on TCP 25565 or SSH on TCP 22. Traffic that doesn’t match this rule is filtered out.

Problems can occur in three main cases:

  • A service is running on a custom port for which the filter doesn’t have a rule yet, such as an anti-cheat proxy or SFTP for Pterodactyl Wings on TCP 2022.
  • A rule has identified the wrong service.
  • An upstream proxy such as TCPShield or NeoProtect changes the traffic in a way that makes the game filter identify it as invalid.

Here’s what to do:

  1. Open the DDoS protection tab for your VPS and check Learned rules to see which service is listed for your port.
  2. If the rule is missing or the service is incorrect, click Request rule. Enter the Port, Protocol, and the Service on this port, and briefly describe what isn’t working while the filter is enabled. Your request creates a ticket, and we’ll let you know when the rule is active.
  3. If your VPS has its own IP address, you can temporarily turn off protection by clicking Disable protection to test the connection. If the port works then, the filter is causing the problem. Turn protection back on afterward.
Warning

While protection is disabled, your VPS isn’t protected against DDoS attacks, and bandwidth is limited to the port speed included in your plan. After an attack is detected, protection remains mandatory until the date shown in the tab.

You can’t create or delete rules yourself. The guide to PletX DDoS protection explains how the filter works and what information we need when you have a connection issue. The OVH DDoS protection applies instead at our London, Montreal, Singapore, and Sydney locations.

Mail servers: Ports 25 and 465

On newly ordered VPSs, outgoing connections on ports 25 and 465 are blocked until you verify a phone number in your account. This helps protect our network from spam sent by compromised servers. While the block is active, your VPS displays the notice Email sending blocked.

  1. Click Verify phone number in the notice. You’ll be taken directly to the phone number section in your account settings.
  2. Verify your phone number there using the code. The block will then be automatically lifted for all VPSs in your account.
  3. Open the Settings tab for your VPS, go to the Reverse DNS section, and enter your mail server’s hostname, for example, mail.example.com. Add an A record for this hostname pointing to your VPS’s IP address with your domain provider. Many mail servers reject emails without a matching reverse DNS record.
Info

Port 587 isn’t blocked. This means you can send emails through an external SMTP relay service on port 587 even without requesting an unblock. We don’t block incoming connections on port 25: if your mail server isn’t reachable from the outside, the checks above can help.

If the notice says that Support blocked the ports, verifying a phone number won’t lift the block. In this case, contact our Support team.

Troubleshooting

My IP address is on a blacklist (e.g., Spamhaus)

First, check check.spamhaus.org to see which list your IP address is on.

  • Spamhaus PBL: This isn’t a penalty list. The PBL identifies address ranges that shouldn’t deliver email directly to other mail servers. If Spamhaus offers you the option to remove your IP, you can request it there. Otherwise, contact our Support team, and we’ll check whether your individual IP address can be delisted. In the meantime, you can use an external SMTP relay service on port 587.
  • Other lists (for example, Spamhaus SBL/XBL, UCEPROTECT, Barracuda): These listings are usually caused by actual spam or malware. First, find and fix the cause, such as a hacked script, an open mail relay, or an open proxy. Then request removal from the list operator.

Getting a new IP address usually doesn’t help, because addresses in the same range often have the same listing. Security filters used by individual internet providers to block your server for their customers aren’t blacklists. Only the provider or its customers can remove these blocks in their own settings.

Players can’t connect to the server through TCPShield or NeoProtect

External proxies change how the connection is established. If the PletX filter detects a game such as Minecraft on your port, it may discard this traffic as invalid. In this case, request a suitable rule using Request rule in the DDoS protection tab, and include the proxy details. It isn’t possible to allow the proxy’s IP ranges for just one port.

Also check your own firewall: if you allow the port only for the proxy’s addresses, make sure the list is complete and up to date. Also note that your VPS can only send packets with its own IP address as the sender. Tunnels that use other sender addresses won’t work.

My ports are closed again after a restart

For UFW, check whether the firewall is active with sudo ufw status, and enable it if needed with sudo ufw enable. IPTables rules are lost on restart unless you install the iptables-persistent package. Save them after each change with sudo netfilter-persistent save.

I’ve locked myself out of SSH

Open the Console tab for your VPS in the ZapX dashboard and log in there. Reopen the SSH port with sudo ufw allow 22 or temporarily disable UFW with sudo ufw disable. If you’ve set a rule in the Network firewall tab that blocks SSH, remove it there.

I can’t disable DDoS protection

You can disable protection only if you have your own IP address. If your server shares an IP address with others, the toggle is disabled. Also, after an attack is detected, protection remains mandatory for a while; the date is shown in the DDoS protection tab.

Conclusion

You’ve successfully installed a firewall on your Linux server and created your own port rules. You can also use this guide to open additional ports.
Our Support team is here to help every day if you have any questions or run into problems! 🙂

Free tools

Related tools for servers and developers

View all

FAQ

Frequently asked questions

With UFW, run sudo ufw allow 25565/tcp (or use /udp for UDP services), replacing 25565 with your port. If you’ve set restrictive rules in the Network firewall tab of the ZapX dashboard, allow the port there as well.
The service may not be listening on the port or may be listening only on 127.0.0.1, the rule may use the wrong protocol, the ZapX dashboard’s network firewall may be blocking it, or DDoS protection may have learned an incorrect rule for the port. The step-by-step checks in this guide cover each possibility.
Yes. Your VPS has an upstream network firewall in the Network firewall tab. By default, it allows inbound and outbound traffic. If you’ve set more restrictive rules there, you’ll need to allow ports there as well.

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.