# How do you set up a UFW firewall on a VPS?

> How to set up a UFW firewall on a VPS: deny incoming by default, allow SSH on port 22 first, open only the ports you use, then enable it and check.

- URL: https://buymonerovps.com/guides/how-to-set-up-a-ufw-firewall/
- Last updated: 2026-11-30
- Publisher: BuyMoneroVPS (https://buymonerovps.com)

**Quick answer**

To set up UFW on a VPS, install it, set the default policy to deny incoming and allow outgoing, allow SSH on port 22 before anything else, add one rule per service you run, then run ufw enable and ufw status verbose. On a **BuyMoneroVPS** Debian or Ubuntu server it takes about 5 minutes and covers IPv4 and IPv6.

**In brief**

- Ubuntu's documentation states that ufw is disabled until you enable it.
- Allow SSH before ufw enable, or the open session may be your last one.
- With IPv6 on, which is the default, a rule such as ufw allow 22/tcp covers both IPv4 and IPv6.
- ufw limit blocks an address that opens 6 or more connections within 30 seconds.

## Why put a firewall on a VPS that runs few services?

**Because the services you forget about are the ones that get found.** A database installed for a test, a monitoring agent or an admin panel can listen on a public port without telling you. Every BuyMoneroVPS server has a dedicated IPv4 and a /64 of IPv6, reachable from the whole internet from the moment the IP address appears in the dashboard.

UFW, the uncomplicated firewall, is a front end to the kernel's netfilter rules. You describe what may come in, it writes the rules for both IPv4 and IPv6. The policy in this guide is short: refuse all incoming connections, allow all outgoing ones, then open exactly the ports your services need. It is the sixth task of [securing a new Linux VPS](https://buymonerovps.com/guides/how-to-secure-a-new-linux-vps/ "How to secure a new Linux VPS").

## What are the steps, in a safe order?

1. **Install ufw** — Install it with apt if the ufw command is missing. Nothing is filtered until you enable it.
2. **Set the default policies** — Deny incoming and allow outgoing. Changing a default does not touch rules that already exist.
3. **Allow SSH first** — Run ufw allow 22/tcp, or ufw limit 22/tcp to slow down password guessing as well. If you moved SSH to another port, allow that port instead.
4. **Open the ports of your services** — Add one rule per service: 80 and 443 for a website, 51820/udp for WireGuard, 18080 for a Monero node.
5. **Enable and read the result** — Run ufw enable, confirm, then ufw status verbose to see the defaults and every rule.
6. **Test from outside** — Open a new SSH session before closing the current one. Then check each service from your own computer.

*A complete first setup (as root or with sudo)*

```bash
apt install ufw

ufw default deny incoming
ufw default allow outgoing

ufw limit 22/tcp          # SSH, with rate limiting
ufw allow 80/tcp          # website, HTTP
ufw allow 443/tcp         # website, HTTPS

ufw enable
ufw status verbose
```

## Which rule opens which service?

*Common UFW rules on a VPS, with the port each service uses by default, 30 November 2026*

| Service | Port | Rule | Open to |
| --- | --- | --- | --- |
| SSH | 22/tcp | ufw limit 22/tcp | Everyone, rate-limited |
| Website (nginx) | 80/tcp, 443/tcp | ufw allow 80/tcp and ufw allow 443/tcp | Everyone |
| WireGuard | 51820/udp | ufw allow 51820/udp | Everyone (keys do the filtering) |
| Monero node, peers | 18080/tcp | ufw allow 18080/tcp | Everyone |
| Monero node, restricted RPC | 18089/tcp | ufw allow proto tcp from <your IP> to any port 18089 | Your own address |
| Tor relay | ORPort, often 443/tcp | ufw allow 443/tcp | Everyone |

## How do you open a port to one address only?

**Name the source in the rule: ufw allow proto tcp from 198.51.100.7 to any port 18089.** Ubuntu's firewall page shows this form for SSH from a single host. It suits services that only you use, such as a node's wallet port, an admin page or a database you reach from home.

If your home address changes often, a source rule will lock you out of that service after each change. Two alternatives work better: an SSH tunnel, which needs no extra port at all, or a [WireGuard tunnel](https://buymonerovps.com/guides/how-to-set-up-wireguard-on-a-vps/ "How to set up WireGuard on a VPS") with rules that accept the port only from the tunnel subnet, for example from 10.8.0.0/24. For the node ports above, see [how to run a Monero node on a VPS](https://buymonerovps.com/guides/how-to-run-a-monero-node-on-a-vps/ "How to run a Monero node on a VPS").

*Read, delete and log*

```bash
ufw status numbered       # rules with their index
ufw delete 3              # remove rule number 3
ufw delete allow 80/tcp   # or repeat the rule with delete in front
ufw logging on
ufw app list              # application profiles installed packages provide
```

## How do you change or remove a rule safely?

**List the rules with numbers, delete by number or by repeating the rule, and check the list again.** The ufw manual adds a detail: a rule created for both IPv4 and IPv6 appears twice in the numbered list, and deleting by number removes only one of the two entries. Writing delete in front of the original rule removes both at once, which is usually what you want.

Never delete the SSH rule while you are connected through it, unless another way in is open. When you reorganise rules, add the new one first, test it, then delete the old one. Logging, turned on with ufw logging on, records blocked packets in the system log, useful when a service you expected to reach stays silent.

According to BuyMoneroVPS's survey of 30 November 2026, Hub 4 (2 vCPU / 4 GB / 65 GB) costs $8.18, 57% under the $18.98 mean of comparable plans. Once the firewall is up, [hosting a website on the VPS](https://buymonerovps.com/guides/how-to-host-a-website-on-a-vps/ "How to host a website on a VPS") needs only ports 80 and 443.

## Frequently asked questions

### Will ufw enable cut my SSH session?

Not if port 22, or your SSH port, is allowed first: with that rule in place the open session continues. Still keep a second session open while you test.

### Does ufw also protect IPv6?

Yes. IPv6 is on by default in /etc/default/ufw, and a rule like ufw allow 22/tcp applies to both families. Every BuyMoneroVPS server has a /64 of IPv6, so keep it that way.

### What does ufw limit do exactly?

It allows the connection but denies an address that opens 6 or more connections within 30 seconds. It is meant for SSH and similar login services.

### Do I still need ufw if the network has upstream protection?

Yes. Upstream protection deals with floods of traffic; it does not know which of your ports should be open. Only a firewall on the server decides that.

### WireGuard stopped routing after I enabled ufw. Why?

ufw's default forward policy drops routed packets. Set DEFAULT_FORWARD_POLICY="ACCEPT" in /etc/default/ufw and reload, as Ubuntu's firewall page shows for masquerading.

**Verdict**

- Deny incoming, allow outgoing, allow SSH, then enable: four lines cover most servers.
- Open each port for a named service, and to a single address when only you use it.
- Delete rules by repeating them with delete, so the IPv4 and IPv6 entries go together.

## Sources

1. [Firewall](https://ubuntu.com/server/docs/how-to/security/firewalls/) — Ubuntu Server documentation (read 2026-11-30)
2. [ufw(8) manual page](https://manpages.ubuntu.com/manpages/noble/en/man8/ufw.8.html) — Ubuntu Manpages (read 2026-11-30)

**A clean server to firewall** Hub 4: 2 vCPU · 4 GB memory · 65 GB NVMe disk · 2 TB monthly transfer in London, Frankfurt, Amsterdam, New York, Dallas or Los Angeles, paid in Monero.

- [Deploy Hub 4 for $8.18](https://buymonerovps.com/app/checkout/?plan=hub-4&term=1m&from=guides-how-to-set-up-a-ufw-firewall&lang=en)
- [Compare all plans](https://buymonerovps.com/pricing/ "BuyMoneroVPS pricing: what does a Monero VPS cost?")

---

BuyMoneroVPS: BuyMoneroVPS sells Linux KVM virtual servers paid in Monero first, and also in Bitcoin, Litecoin, Solana, Ether, USDC and USDT, with no ID check and no email required. Servers run in Amsterdam, Reykjavik, Zurich, Bucharest, New York, Dallas, Los Angeles, London, Frankfurt, from $3.78 a month. BuyMoneroVPS LLC, a New Mexico company, runs the service.
