# How do you set up a firewall with ufw on a VPS?

> UFW firewall on a VPS: deny incoming by default, allow SSH first, then open only the ports each service needs. Commands for Debian and Ubuntu, 2026.

- URL: https://vpsromania.com/guides/how-to-set-up-a-firewall-with-ufw/
- Last updated: 2026-12-01
- Publisher: VPSRomania (https://vpsromania.com)

**Quick answer**

A ufw firewall on a VPS takes five commands: install ufw, allow OpenSSH, set the default to deny incoming and allow outgoing, open each service port, then run ufw enable. **VPSRomania** servers start with Debian or Ubuntu, where ufw is one apt install away, on plans from $4.82 a month paid in Monero or other crypto.

**In brief**

- **Rule zero**: allow SSH before ufw enable, or the session you are typing in is cut.
- **Defaults**: deny incoming, allow outgoing; then one allow line per service.
- **IPv6**: covered when IPV6=yes in /etc/default/ufw, the Debian and Ubuntu default.
- **Port 25**: outgoing SMTP is closed upstream on every VPSRomania server, whatever ufw says.
- **Docker**: published container ports skip ufw's rules; bind them to 127.0.0.1 instead.

## Which ports should you open for common VPS services?

Open only what a service needs to be reached from outside. Databases, Redis and admin panels belong on 127.0.0.1 or behind a VPN, not in this list.

*ufw rules for frequent self-hosted services on a VPS, 30 November 2026*

| Service | Port and protocol | ufw command |
| --- | --- | --- |
| SSH | 22/tcp | ufw limit OpenSSH |
| Website (HTTP and HTTPS) | 80/tcp, 443/tcp | ufw allow 'Nginx Full' |
| HTTP/3 (QUIC), if enabled | 443/udp | ufw allow 443/udp |
| WireGuard | 51820/udp | ufw allow 51820/udp |
| Tor relay or bridge ORPort | the port you chose, often 443 or 9001/tcp | ufw allow 9001/tcp |
| Monero node, peer-to-peer | 18080/tcp | ufw allow 18080/tcp |
| Monero restricted RPC, if public | 18089/tcp | ufw allow 18089/tcp |
| Matrix federation | 8448/tcp | ufw allow 8448/tcp |

Application profiles such as OpenSSH and Nginx Full exist once the matching package is installed; ufw app list shows them.

## How to enable ufw without locking yourself out?

1. **Install ufw** — On Debian or Ubuntu, run apt install ufw. It is inactive after install, so nothing changes yet.
2. **Allow SSH first** — Run ufw limit OpenSSH (or ufw limit 22/tcp). limit allows the port but blocks an address that opens 6 or more connections within 30 seconds.
3. **Set the defaults** — Run ufw default deny incoming and ufw default allow outgoing.
4. **Open the service ports** — Add one rule per service from the table above, and nothing else.
5. **Enable and check** — Run ufw enable, answer y, then ufw status verbose. Open a new SSH session to prove port 22 still answers before closing the old one.

*A web server with SSH, on Debian or Ubuntu*

```bash
apt install -y ufw
ufw limit OpenSSH
ufw default deny incoming
ufw default allow outgoing
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status verbose
```

*Day-to-day rule management*

```bash
ufw status numbered          # list rules with their numbers
ufw delete 3                 # remove rule number 3
ufw allow from 198.51.100.7 to any port 22 proto tcp   # SSH from one address only
ufw deny out 25/tcp          # mirror the upstream block locally (optional)
ufw reload
```

## What does ufw actually do on a VPS?

**ufw is a front end that writes netfilter rules for you through the iptables tools, which run on the nftables backend on Debian 12 and 13 and Ubuntu 22.04 and later.** It keeps a short, readable list of allow and deny lines and turns them into the kernel's packet filter, for IPv4 and IPv6 at once when IPV6=yes. It filters traffic that reaches the server; it does not stop a flood before it reaches the network port, which is a job for the upstream network. Rules survive reboots, because ufw restores them from /etc/ufw at boot. On AlmaLinux and Rocky Linux, the equivalent is firewalld, already installed and running.

## Why is ufw limit better than allow for SSH?

**limit keeps SSH open to you while slowing anyone who hammers it: an address that opens 6 connections in 30 seconds is denied for a while.** That cuts log noise from password scanners at no cost to a person logging in normally. It is not a substitute for key-only authentication, which makes guessing pointless; it is a cheap second layer. If several of your scripts open SSH connections in a burst, for example an rsync loop, use allow from your own address for that source instead, so the limit does not trip on your own jobs.

## Why does Docker ignore ufw rules?

**Docker writes its own netfilter rules for published ports and places them ahead of ufw's chains, so a port published with -p 5432:5432 is reachable even when ufw denies it.** This surprises many people the first time a database shows up in a scan. The simplest fix is to publish container ports on the loopback address only, as -p 127.0.0.1:5432:5432, and put a reverse proxy such as nginx in front of what must be public. Docker's own documentation describes the interaction and the DOCKER-USER chain for custom filtering.

## Can ufw open port 25 for outgoing mail?

**No: outgoing port 25 is closed on every VPSRomania server at the network level, so a ufw rule cannot reopen it.** Ports 465 and 587 stay open, which is what a mail client or an application sending through a relay uses. Self-hosting a server that delivers mail directly to other domains is therefore not possible here. For the rest of the first-day setup, read [how to secure a linux vps](https://vpsromania.com/guides/how-to-secure-a-new-linux-vps/ "How to secure a new Linux VPS"); for a VPN, [wireguard vps setup](https://vpsromania.com/guides/how-to-set-up-wireguard-on-a-vps/ "How to set up WireGuard on a VPS") adds the UDP rule and NAT. If a rule locks you out, [server not responding](https://vpsromania.com/help/server-not-responding/ "VPS server not responding: what to check") lists what support can do.

## Does the firewall choice change with the server's country?

**No: ufw behaves the same in Bucharest, Reykjavik and Zurich, and the plans cost the same in all three.** According to VPSRomania's survey of 30 November 2026, the median monthly price of a 1 vCPU / 1 GB crypto VPS in Switzerland was $5.75 across 8 hosts; VR 1 in Zurich costs $4.82, as on the [switzerland vps](https://vpsromania.com/switzerland-vps/ "Switzerland VPS paid in crypto") page.

## UFW firewall questions

### Is ufw the best firewall for a small VPS in 2026?

For Debian and Ubuntu, yes: it is packaged, readable and enough for a handful of services. Write nftables rules directly only when you need features ufw does not expose.

### I enabled ufw and lost SSH. What now?

The rules are on disk, so a reboot does not help. Write to support with the server ID; a reinstall from the server page also works but erases the disk.

### Does ufw protect IPv6 too?

Yes when /etc/default/ufw contains IPV6=yes, the default on Debian and Ubuntu. Every allow rule then applies to both address families.

### Which ports does VPSRomania block on its network?

Only outgoing port 25, closed upstream on every server; ports 465 and 587 stay open. Everything else is left to your own firewall, which is why ufw belongs in the first login.

### Should I block ping with ufw?

It is rarely worth it. ICMP helps diagnose latency and path problems, and hiding from ping does not hide open ports from a scanner.

**Verdict**

- ufw gives a VPS a default-deny firewall in five commands; allow SSH first and test before logging out.
- Keep databases on 127.0.0.1, watch Docker's published ports, and remember outgoing port 25 is closed.

## Put a firewalled server in Zurich

VR 1: 1 vCPU, 1 GB RAM, 20 GB NVMe in Zurich at $4.82 a month, same price as Bucharest.

[Deploy VR 1 in Zurich — $4.82/mo](https://vpsromania.com/app/checkout/?plan=vr-1&location=ch-zrh&term=3m&from=guides-how-to-set-up-a-firewall-with-ufw&lang=en)

## Sources

1. [ufw(8) manual page](https://manpages.debian.org/bookworm/ufw/ufw.8.en.html) — Debian (read 30 Nov 2026)
2. [UFW community documentation](https://help.ubuntu.com/community/UFW) — Ubuntu (read 30 Nov 2026)
3. [Packet filtering and firewalls](https://docs.docker.com/engine/network/packet-filtering-firewalls/) — Docker Docs (read 30 Nov 2026)
4. [VPS prices paid in crypto, Switzerland (survey data)](https://vpsromania.com/romania-vps-prices-2026/) — VPSRomania (read 30 Nov 2026)

---

VPSRomania: VPSRomania is a VPS host with KVM servers in Bucharest, Romania, and in Reykjavik and Zurich for buyers who want Iceland or Switzerland, paid only in cryptocurrency, Monero included, without any identity check. Plans start at $4.82 a month. VPSRomania LLC, a New Mexico company, runs it.
