Quick answer
How to set up a firewall with UFW?
To set up a firewall with UFW, install it, keep the default of denying incoming traffic, allow SSH before anything else, open only the ports your service needs, then run ufw enable and ufw status verbose. On an IronKVM VPS this takes 6 commands and works the same on Debian 13 and Ubuntu 24.04.
In brief
- UFW installs with incoming traffic denied and outgoing traffic allowed.
- Allow SSH before you enable it: enabling may drop existing connections.
ufw limitdenies an address that opens 6 or more connections within 30 seconds.- Ports published by Docker containers are not filtered by UFW.
- Outgoing port 25 is closed on every VPS and stays closed; ports 465 and 587 towards a mail provider are open.
Which UFW rules does a typical VPS need?
| Service | Rule | Note |
|---|---|---|
| SSH | ufw limit 22/tcp | Rate-limited; add before enabling |
| Website | ufw allow 80/tcp and ufw allow 443/tcp | Needed for HTTP-01 certificates too |
| WireGuard | ufw allow 51820/udp | Match your ListenPort |
| One trusted address | ufw allow from 203.0.113.5 to any port 3306 proto tcp | Database from a known IP only |
| Remove a rule | ufw status numbered, then ufw delete NUM | Deletes only that rule |
What are the steps to set up a firewall with UFW?
Install UFW
Debian images may not include it. Install the ufw package with apt.
Set the default policies
Deny incoming and allow outgoing. These are the defaults, but setting them makes the intent clear.
Allow SSH first
Add ufw limit 22/tcp, or allow your custom SSH port, before turning the firewall on.
Open the service ports
Allow 80/tcp and 443/tcp for a website, 51820/udp for WireGuard, nothing more.
Enable the firewall
Run ufw enable. Under SSH it asks for confirmation; answer y.
Check the rules
Run ufw status verbose, and open a second SSH session to be sure you still get in.
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verboseHow do you avoid locking yourself out with UFW?
Add the SSH rule first, then enable, and keep the current session open. The ufw manual says it prompts when you enable it under SSH, and warns that enabling may drop existing connections. With an allow or limit rule for SSH already in place, the session survives.
If you moved SSH to another port, allow that port instead of 22. Test from a second terminal before closing the first one. If you do lock yourself out, a reinstall from the dashboard restores access but wipes the disk. The offshore VPS security guide lists the safe order of every step.
Which is better for SSH: allow or limit?
limit is the better default for SSH on a public VPS. The ufw manual says a limit rule denies connections from an address that tries to open 6 or more within 30 seconds. Normal logins never hit that rate.
For longer bans based on failed logins, add fail2ban on top. It reads the SSH log and bans an address for a set time after too many failures. The fail2ban guide shows the jail.local file. Limit and fail2ban work together without conflict.
Why does Docker ignore my UFW rules?
Docker routes traffic to published container ports before UFW’s chains see it. Docker documentation says such packets are routed before the firewall rules apply, which effectively ignores your configuration. A container started with -p 8080:80 is open to the world even if UFW denies 8080.
Publish container ports on 127.0.0.1 and put a reverse proxy such as nginx in front. The Docker guide shows the syntax. WireGuard has no such issue; the WireGuard guide opens 51820/udp with a normal rule.
What does a firewalled VPS cost?
UFW is free and comes with every Linux image; the price is the plan. On IronKVM, Billet costs $4.46 a month on 12 months and Girder $13.50. According to IronKVM's survey of 16 November 2026, plans comparable to Girder cost a median of $47.10 a month across 18 hosts. No DDoS protection is sold; only the default filtering of each datacenter network applies.
UFW firewall: questions
Is UFW the best firewall for a VPS?
For most single servers, yes: it is short to write and read. nftables directly gives more control for complex setups.
How do I delete a UFW rule?
Run ufw status numbered, then ufw delete followed by the rule number.
How do I see blocked connections?
Run ufw logging on. Blocked packets then appear in the kernel log.
Does UFW block outgoing mail?
Not by default. On IronKVM, outgoing port 25 is closed at the network level anyway; 465 and 587 are open.
Can I list the apps UFW knows?
Yes: ufw app list shows the profiles installed packages provide, such as OpenSSH or Nginx.
Verdict
- Deny incoming, limit SSH, allow only service ports, then enable: that order never locks you out.
- Watch Docker: publish container ports on 127.0.0.1, since UFW does not filter them.
Sources (4)
- ufw(8) manual, Ubuntu 24.04 — Canonical (read 2026-11-16)
- Firewalls — Canonical (read 2026-11-16)
- Packet filtering and firewalls — Docker (read 2026-11-16)
- IronKVM offshore VPS survey (CSV) — IronKVM (read 2026-11-16)
Get a VPS to firewall
Girder, 4 vCPU, 8 GB RAM, 160 GB NVMe, 8 TB traffic, $13.50 a month on 12 months, in Amsterdam, Bucharest or Chisinau.