Quick answer
How do I set up a firewall on a VPS?
To set up a firewall on a VPS with ufw, set incoming traffic to deny and outgoing to allow, allow SSH before anything else, open only the ports your services use, then run ufw enable and check ufw status verbose. On a SaltVPS server, ufw filters both the IPv4 and the IPv6 /64 in 6 commands.
In brief
- Tool: ufw, a front end to the kernel firewall, packaged on Debian and Ubuntu.
- Golden rule:
ufw allow OpenSSHbeforeufw enable, or you lock yourself out. - Defaults: deny incoming, allow outgoing.
- IPv6: covered when
IPV6=yesin/etc/default/ufw, the default. - AlmaLinux and Rocky Linux: use firewalld instead.
How do you set up a ufw firewall on a VPS step by step?
Run as root on Ubuntu or Debian, with a working SSH session open.
Install ufw
Ubuntu usually ships it. On Debian, run
apt install ufw.Set the defaults
Run
ufw default deny incomingandufw default allow outgoing.Allow SSH first
Run
ufw allow OpenSSH, orufw allow 22/tcp. If SSH listens on another port, allow that number instead.Open service ports
Add only what you run:
ufw allow 80/tcpandufw allow 443/tcpfor a website,ufw allow 51820/udpfor WireGuard.Enable the firewall
Run
ufw enableand answer y. Your current SSH session stays connected.Check the rules
Run
ufw status verbose. Each rule appears twice, once with (v6), when IPv6 is on.
Which ufw rules do common VPS services need?
Open the fewest ports possible; each line below is one command.
| Service | ufw command | Protocol and port |
|---|---|---|
| SSH | ufw allow OpenSSH | TCP 22 |
| Website (nginx) | ufw allow 'Nginx Full' | TCP 80 and 443 |
| WireGuard | ufw allow 51820/udp | UDP 51820 |
| Bitcoin Core peers | ufw allow 8333/tcp | TCP 8333 |
| Monero peers | ufw allow 18080/tcp | TCP 18080 |
| SSH from one address only | ufw allow from 203.0.113.5 to any port 22 proto tcp | TCP 22, one source |
How do you avoid locking yourself out?
Allow SSH, then enable, then test from a second terminal before closing the first. ufw keeps established connections, so the session you typed in survives. A missing SSH rule only shows when you try to log in again.
If it happens anyway, a reinstall from the dashboard is the way back, and it erases the disk. Rate-limiting helps too: ufw limit OpenSSH refuses an address that opens 6 or more connections within 30 seconds.
Does ufw protect Docker containers?
Not for published ports: Docker adds its own iptables rules that run before ufw's. A container started with -p 8080:80 is open to the internet even with ufw set to deny. Bind such ports to 127.0.0.1, or publish only what should be public. The Docker guide explains the setup.
What is the firewalld equivalent on AlmaLinux and Rocky Linux?
firewalld is the standard firewall on both, and its default zone already allows SSH. If it is missing, run dnf install firewalld and systemctl enable --now firewalld. Then use firewall-cmd --permanent --add-service=http, then firewall-cmd --reload, and list rules with firewall-cmd --list-all.
SaltVPS checked its plan list on 16 November 2026: the same 6 images are offered on all 7 plans, two of them from the Red Hat family. Whatever the system, a firewall is one of the first steps in the Linux VPS security guide, followed by Fail2ban.
How do you log what the firewall blocks?
Turn on logging with ufw logging on; blocked packets then appear with a [UFW BLOCK] tag. Read them with journalctl -k | grep UFW or in /var/log/ufw.log where rsyslog is installed. A steady stream of blocks on random ports is normal for any public address. The level low is enough; medium and high log far more lines and fill the disk faster.
VPS firewall questions
Is ufw the best firewall for an Ubuntu VPS?
It is the simplest. It writes nftables or iptables rules for you; use those directly only if you need rules ufw cannot express.
How do I delete a ufw rule?
Run ufw status numbered, then ufw delete followed by the rule number.
Do I need a firewall if my host filters traffic?
Yes. The data-centre network filters attacks on its side; your firewall decides which services on the server are reachable.
How do I set up a firewall on a VPS for IPv6?
With ufw, keep IPV6=yes in /etc/default/ufw. Every allow rule then covers IPv4 and IPv6.
How do I turn the firewall off for a test?
Run ufw disable. The rules stay saved; ufw enable brings them back.
Verdict
- Setting up a firewall on a VPS with ufw takes six commands, with SSH allowed first.
- Docker ports are the exception: bind them to 127.0.0.1 or ufw will not stop them.
Sources (3)
- ufw(8) manual page — Ubuntu (2026-11-16)
- Ubuntu Server documentation: firewall — Canonical (2026-11-16)
- firewalld documentation — firewalld project (2026-11-16)
A server with IPv4 and IPv6 to protect
Grain gives root over SSH, a dedicated IPv4 and a routed IPv6 /64, paid in crypto.
- Built by a person, 1 hour at most after confirmation
- Lost days credited back if a server cannot be delivered
- Pay in BTC, XMR, USDT, USDC, ETH, SOL or LTC
- DomaineGoat LLC · Panama