Set up a firewall on your VPS
Your server firewall runs inside the operating system: ufw on Ubuntu and Debian, firewalld on Rocky Linux and AlmaLinux, or nftables directly on any of them. The rule set is the same idea everywhere, deny incoming by default, then open SSH and the few ports your services really need.
Where does the firewall live on a CheapVPS server?
On the server itself. There is no separate firewall product in the console, so the rules you write with ufw, firewalld or nftables are the ones that decide which packets reach your services. The network in front of every server filters volumetric DDoS floods, but it does not block individual ports for you.
That keeps things simple: the rules travel with the server, you can keep them in a Git repository next to your other config, and the same file works when you move to a bigger plan. All three tools below sit on top of the kernel netfilter layer, so pick one per server and do not mix them.
Which ports should be open?
As few as your services need, and you can list what is listening right now with:
ss -tulpnEvery line shows a protocol, an address and the program behind it. Addresses 127.0.0.1 and [::1] are only reachable from the server itself and need no rule. Anything on 0.0.0.0 or [::] is reachable from the internet unless the firewall stops it.
A typical web server needs 22 (or your custom SSH port), 80 and 443. A WireGuard VPN needs its UDP port, often 51820. A Minecraft server needs 25565. Databases such as MySQL on 3306 or PostgreSQL on 5432 should not be open to the world at all: bind them to 127.0.0.1, or allow only the IP of the app server that uses them.
How do you set up ufw on Ubuntu and Debian?
ufw is installed on Ubuntu images; on Debian run apt install -y ufw first. Set the defaults, open SSH before anything else, then switch it on:
ufw default deny incoming
ufw default allow outgoing
ufw limit OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enableufw limit works like allow but refuses an IP that opens six or more connections in 30 seconds, a cheap brake on brute force. If SSH runs on another port, use ufw limit 2222/tcp instead of the OpenSSH profile.
Check the result with ufw status verbose. To remove a rule, list them with numbers via ufw status numbered and run ufw delete 3. To let only one office IP reach a service: ufw allow from 198.51.100.7 to any port 5432 proto tcp. IPv6 rules are created at the same time as long as IPV6=yes is set in /etc/default/ufw, which is the default.
How do you set up firewalld on Rocky Linux and AlmaLinux?
firewalld groups rules into zones; the public zone applies to the main interface. Make sure it runs, then add services permanently and reload:
dnf install -y firewalld
systemctl enable --now firewalld
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-allRules added without --permanent apply at once but vanish at the next reload or reboot, which makes them useful for testing. Remove what the image enables and you do not need, for example firewall-cmd --permanent --remove-service=cockpit.
For a port with no service name use --add-port=25565/tcp. To limit a port to one source address, use a rich rule:
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=198.51.100.7 port port=5432 protocol=tcp accept"
firewall-cmd --reloadHow do you write the rules in plain nftables?
nftables is the kernel firewall that the other two tools drive, and writing it by hand gives one readable file with no extra layer. Debian 12 and 13 ship it by default; install it elsewhere with apt install -y nftables or dnf install -y nftables, and stop ufw or firewalld first. Put this in /etc/nftables.conf (on Rocky and Alma, /etc/sysconfig/nftables.conf):
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport 22 ct state new limit rate 10/minute accept
tcp dport { 80, 443 } accept
}
chain forward { type filter hook forward priority 0; policy drop; }
chain output { type filter hook output priority 0; policy accept; }
}The inet table covers IPv4 and IPv6 in one go. ICMP stays open because IPv6 needs it to work at all. Check the file without applying it, then load it and make it start at boot:
nft -c -f /etc/nftables.conf
nft -f /etc/nftables.conf
systemctl enable nftables
nft list rulesetHow do you test rules without locking yourself out?
Always keep a way back. There is no browser console, so the safety net is a timer that undoes the change unless you cancel it. Before enabling a new rule set, start a rollback in the background:
(sleep 300 && ufw disable) &
ufw enableIf you can still open a fresh SSH session, kill the timer with kill %1 or by its process ID. If you cannot, wait five minutes and the firewall turns itself off. The same trick works with firewall-cmd --panic-off or nft flush ruleset as the undo command.
Rules that only exist in memory are a second safety net: a test with firewalld without --permanent, or nftables loaded with nft -f but not enabled at boot, disappears when you press reboot on the server page. Make them permanent only after a fresh login works.
Why do Docker ports ignore ufw, and how do you stop it?
Docker writes its own netfilter rules for published ports, and that traffic takes the forward path, which the ufw input rules never see. A container started with -p 5432:5432 is reachable from the whole internet even though ufw status does not show the port. The cleanest fix is to publish only on the loopback address and put a reverse proxy in front:
ports:
- "127.0.0.1:5432:5432"For a port that must stay public but be limited to some sources, add rules to the DOCKER-USER chain, which Docker leaves for you. The Docker Compose guide shows this in a full setup.
How do you check the firewall from the outside?
The only test that counts is one from another machine. From your laptop or a second server, scan the address:
nmap -Pn -p 1-10000 203.0.113.10
nmap -Pn -6 -p 1-10000 2001:db8::10You should see only the ports you opened as open; everything else shows as filtered. Scan IPv6 too, because a service bound to [::] can be open there even when IPv4 looks clean. For a single port, nc -vz 203.0.113.10 5432 answers quickly. Repeat the scan whenever you install something new, since many packages start listening as soon as they are installed.
Questions about this guide
Is there a firewall in the CheapVPS console?
No. Firewall rules are set inside the operating system with ufw, firewalld or nftables. The network in front of every server filters DDoS floods, and everything else is decided by your own rules.
Should I use ufw or firewalld?
Use the one your image ships: ufw on Ubuntu, firewalld on Rocky Linux, AlmaLinux, CentOS Stream and Fedora. On Debian, ufw or plain nftables both work well. Do not run two of them at once.
Do I need to allow outgoing traffic?
Most servers allow all outgoing traffic, which keeps updates and API calls working. Filtering outgoing traffic is useful for strict setups but needs care, since package mirrors and DNS must stay reachable.
Will a firewall reduce my traffic usage?
Only slightly. Dropped incoming packets never reach your services, and incoming traffic is free anyway. What counts toward the included amount, such as 20 TB on P4 in Europe, is outgoing traffic.
Why is ICMP left open in the nftables example?
IPv6 depends on ICMPv6 for neighbour discovery and path MTU, and blocking it breaks connections in odd ways. Ping replies also help you and support check that the server is up.
Do I need to open ports for the IPv6 addresses separately?
With ufw, firewalld and an inet nftables table, one rule covers both families. Check both with an outside scan anyway.