Securing VPS/Servers

VPS Firewall Setup: Provider Firewall vs ufw and firewalld

When to use the provider firewall and when the host firewall, a working rule set for ufw and firewalld, the Docker and IPv6 traps, and how to test the result from outside.

A firewall on a VPS has one job: only the ports you mean to offer are reachable from the internet. There are two places to enforce that, the provider's firewall in front of the server and the firewall on the server itself, and they fail in different ways. This guide explains when to use which, gives a working rule set for each Linux family, and shows how to test the result from outside.

Commands are given for Ubuntu 26.04/24.04 and Debian 13 (apt) and for AlmaLinux and Rocky Linux 9 and 10 (dnf). They are standard tools from each distribution; this guide is a reference and was not run on a test server for this article.

Two layers

Provider (cloud) firewallHost firewall (ufw, firewalld)
Where it runsOn the provider's network, before the VPSInside the VPS
Configured inThe provider's panel or APIThe server's command line
Uses your CPUNoA little
Affected by Docker's own rulesNoYes: published container ports bypass ufw
Survives a compromise of the serverYesNo: root can change it
Moves with the server to another providerNoYes
Can lock you outYes, fixed from the panelYes, fixed from the web console

Use both when your provider offers one. If you must pick, the provider firewall is the stronger outer wall, and the host firewall is the one you control completely. Not every provider has a cloud firewall, and some only filter IPv4, so check the result (last section) instead of assuming.

Decide what should be open

ServicePortOpen to
SSH22/tcp (or your custom port)Your IP if it is fixed, otherwise everyone with key-only login
Web80/tcp, 443/tcp (and 443/udp for HTTP/3)Everyone
Mail server25, 465, 587, 993/tcpEveryone, only if the server handles mail
Database3306, 5432/tcpNobody. Listen on 127.0.0.1; if remote access is required, one named IP
Redis, Memcached6379, 11211Nobody
Control panel, admin toolsVariesYour IP, or reach them through an SSH tunnel

Everything not on your list stays closed. Outgoing connections are normally left open.

Ubuntu and Debian: ufw

sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose

Allow SSH before ufw enable. If SSH runs on another port, use sudo ufw allow 2222/tcp instead of OpenSSH.

Everyday commands:

sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp   # one IP to one port
sudo ufw limit OpenSSH                                         # rate-limit new SSH connections
sudo ufw status numbered                                       # list rules with numbers
sudo ufw delete 3                                              # remove rule 3
sudo ufw reload

203.0.113.10 is a placeholder; put your own address there. ufw applies rules to IPv6 as well when IPV6=yes is set in /etc/default/ufw, which is the default.

AlmaLinux and Rocky Linux: firewalld

sudo dnf install firewalld
sudo systemctl enable --now firewalld
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

firewalld keeps a running configuration and a permanent one. A command without --permanent takes effect now and is lost at the next reload; with --permanent it is saved and takes effect after --reload. Using --permanent followed by a reload, as above, avoids surprises.

Everyday commands:

sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reload
sudo firewall-cmd --get-active-zones

The default zone on these systems may allow services you do not use, such as cockpit or dhcpv6-client. --list-all shows them; remove what you do not need.

Docker changes the picture

Docker manages its own packet filter rules. A container started with -p 8080:80 is reachable from the internet on port 8080 even when ufw shows that port as closed, because Docker's rules are evaluated before ufw's.

  • Publish to localhost when only a reverse proxy on the same server needs the port: -p 127.0.0.1:8080:80.
  • Do not publish ports for containers that only talk to other containers; put them on a shared Docker network.
  • Use the provider firewall as the outer layer. It is not affected by anything Docker does.

Common mistakes

  • Enabling the firewall before allowing SSH. Recovery is through the provider's web console; see SSH connection timed out.
  • Forgetting IPv6. A service closed on IPv4 and open on IPv6 is open. Test both.
  • Running two firewall managers. ufw, firewalld and hand-written iptables or nftables rules overwrite each other. Choose one.
  • Opening a database port "temporarily". Use an SSH tunnel instead: ssh -L 3306:127.0.0.1:3306 user@server.
  • Trusting the rule list instead of testing. The list shows what you asked for, not what is reachable.

Test from outside

On the server, see what is listening:

sudo ss -tulpn

From another machine, see what is reachable. Scan only servers you own.

nmap -Pn YOUR_SERVER_IP                 # common TCP ports over IPv4
nmap -Pn -6 YOUR_SERVER_IPV6            # the same over IPv6
nmap -Pn -p 3306,5432,6379 YOUR_SERVER_IP

The open ports should match your list from the second section exactly. "Filtered" means a firewall dropped the probe; "closed" means the packet got through and nothing was listening.

Older articles on this site go deeper into each tool: configuring ufw and an introduction to firewalld. The firewall is step 3 of the VPS hardening checklist.

Frequently asked questions

Do I need a firewall if nothing unnecessary is listening?

It is a second safeguard for the day something starts listening that you did not expect: a new package, a development server, a container. The cost is a few minutes.

Is the provider's firewall enough by itself?

It can be, if it filters both IPv4 and IPv6 and you keep it in step with the server. A host firewall protects you when a rule there is missed and goes with the server if you move.

Should I block outgoing traffic too?

For most small servers the effort outweighs the gain, because updates, APIs and DNS all need outgoing access. It is worth doing on servers that hold sensitive data, with an explicit list of allowed destinations.

Does a firewall stop attacks on my website?

No. Ports 80 and 443 are open by design. Application flaws need updates and, where it fits, a web application firewall.

Share story:

Continue reading other guides on How VPS.