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

> How to set up a UFW firewall on a VPS in 6 commands: allow SSH first, open web ports, rate-limit logins and read the status.

- URL: https://jadekvm.com/guides/how-to-set-up-a-ufw-firewall-on-a-vps/
- Last updated: 2026-11-16
- Publisher: JadeKVM (https://jadekvm.com)

This is how to set up a UFW firewall on a VPS running Debian 13 or Ubuntu 24.04, without cutting your own SSH session.

**Quick answer — How do you set up a UFW firewall on a VPS?**

To set up a UFW firewall on a VPS, install ufw, deny incoming traffic by default, allow OpenSSH, open 80 and 443 only for a website, then run `ufw enable`. Six commands are enough. On a **JadeKVM** server, outbound port 25 is already closed, so UFW only needs to guard the inbound side.

**In brief**

- **Allow SSH before enabling**: otherwise the firewall drops your own session.
- **Default deny incoming, allow outgoing**: the safe base for a single server.
- **`ufw limit OpenSSH`** slows password-guessing bots at the firewall level.
- **Docker warning**: published container ports skip UFW rules.

## How to set up a UFW firewall on a VPS in 6 steps?

Run each command as a sudo user. Ubuntu 24.04 ships ufw; on Debian 13 you install it first.

1. **Install ufw** — Run sudo apt install ufw. The package is small and has no service to tune.
2. **Set the defaults** — Deny incoming and allow outgoing with two ufw default commands.
3. **Allow SSH** — Run sudo ufw limit OpenSSH. It allows port 22 and slows repeat connections.
4. **Open service ports** — Allow 80 and 443 for a website, or the single port your app needs.
5. **Enable the firewall** — Run sudo ufw enable and confirm with y. Rules load now and at every boot.
6. **Read the status** — Run sudo ufw status verbose and check that only the ports you chose are listed.

## Which UFW rule fits which service?

*Common UFW rules on a VPS, the port each one opens and when to use it*

| Service | Rule | Port | Open it when |
| --- | --- | --- | --- |
| SSH | ufw limit OpenSSH | 22/tcp | Always, before ufw enable |
| Website | ufw allow 80,443/tcp | 80 and 443/tcp | You run nginx or another web server |
| Telegram webhook | ufw allow 8443/tcp | 8443/tcp | Your bot uses a webhook on 8443 |
| WireGuard | ufw allow 51820/udp | 51820/udp | You run a WireGuard endpoint |
| Database | none | 3306 or 5432 | Never to the world; use SSH tunnels |

## Why allow SSH before you run ufw enable?

**Because ufw enable applies the default deny at once, and a missing SSH rule closes the door you are standing in.** The tool warns that existing SSH connections may drop. Answer y only after `ufw show added` lists OpenSSH.

If it goes wrong, a dashboard reboot does not help: the rules persist. Reboot, reinstall and delete requests are sent from the dashboard and carried out by a person; a reinstall wipes the disk. Write to support, and test changes in a second window next time.

*A UFW base for a web server*

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

## Which is the best UFW setup for an Asia VPS in 2026?

**The same short list as anywhere: SSH limited, web ports open if needed, everything else denied.** A server in Hong Kong, Tokyo or Singapore draws the same scans as one in Europe. UFW does not change latency either.

JadeKVM read WonderNetwork's public pings on 16 November 2026: Singapore sits 15 ms from Jakarta and 8 ms from Kuala Lumpur. Latency figures on JadeKVM pages are public averages measured by WonderNetwork between its own servers on 16 November 2026; they show how far apart the cities sit on the network, not a promise about any one JadeKVM server. The firewall adds no measurable delay to those trips. To measure your own path, see [how to test latency to your server](https://jadekvm.com/help/test-latency-to-your-server/ "How do I test the latency to my JadeKVM server?").

## How do you delete or change a UFW rule?

**List the rules with numbers, then delete by number.** `sudo ufw status numbered` prints each rule with an index. `sudo ufw delete 3` removes the third one. IPv6 copies show as separate lines, so delete both.

To allow one address only, for example your office, use `sudo ufw allow from 198.51.100.7 to any port 22 proto tcp`. Then remove the general SSH rule.

## Does Docker ignore UFW rules?

**Yes: Docker writes its own iptables rules, so a published container port is open even if UFW denies it.** Docker's own documentation says so. Bind containers to 127.0.0.1 and put nginx in front, or filter in the DOCKER-USER chain. The guide on [how to install Docker on a VPS](https://jadekvm.com/guides/how-to-install-docker-on-a-vps/ "How do you install Docker on a VPS?") shows the safe pattern.

## What else should sit next to UFW?

**fail2ban for the SSH journal, and keys-only login.** UFW counts connections; fail2ban reads failed logins and bans their source. Together with the steps in [how to secure a Linux VPS](https://jadekvm.com/guides/how-to-secure-a-linux-vps-in-hong-kong-tokyo-or-singapore/ "How do you secure a Linux VPS in Hong Kong, Tokyo or Singapore?"), they cover the usual attacks. Outbound port 25 is closed on every server; send mail through a mail provider on port 465 or 587.

## Questions about UFW on a VPS

### Is UFW installed on Ubuntu 24.04?

Yes, but it is inactive until you run ufw enable. On Debian 13, install it with apt first.

### Does UFW work with IPv6?

Yes. IPV6=yes is the default in /etc/default/ufw, so each rule gets an IPv6 twin. JadeKVM servers have a routed IPv6 /64.

### Which is better: UFW or firewalld?

On Debian and Ubuntu, UFW is the native and simpler choice. Firewalld is the norm on AlmaLinux and Rocky Linux.

### Can UFW stop a DDoS attack?

No. A large flood saturates the link before UFW sees it. UFW closes ports; it does not absorb traffic.

### How do I turn UFW off for a test?

Run sudo ufw disable. The rules stay saved, and ufw enable brings them back.

**Verdict**

- **Allow OpenSSH, deny the rest, enable**: that order is the whole trick.
- Bind Docker ports to 127.0.0.1, because Docker skips UFW.

## Sources

1. [ufw(8) manual page, Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man8/ufw.8.html) — Canonical (read 2026-11-16)
2. [Firewalls, Ubuntu Server documentation](https://documentation.ubuntu.com/server/how-to/security/firewalls/) — Canonical (read 2026-11-16)
3. [Packet filtering and firewalls, Docker Engine documentation](https://docs.docker.com/engine/network/packet-filtering-firewalls/) — Docker (read 2026-11-16)
4. [WonderNetwork: pings from Singapore](https://wondernetwork.com/pings/Singapore) — WonderNetwork (read 2026-11-16)

**Put the firewall on a Singapore server** Bead in Singapore costs $3.75 a month on 12 months, Linux only. [Order Bead in Singapore](https://jadekvm.com/app/checkout/?plan=bead&term=12m&location=sg-sin&from=how-to-set-up-a-ufw-firewall-on-a-vps&lang=en) · [See every plan](https://jadekvm.com/pricing/ "How much does a JadeKVM server cost per month?")

---

JadeKVM: JadeKVM sells Linux KVM VPS in Hong Kong, Tokyo and Singapore, paid in crypto with no ID document, and publishes the measured latency from each city to the rest of Asia. 5 plans from 1 to 16 GB of RAM cost the same in all three cities and, on the 12-month term, sit at least 25% under a named comparable no-KYC crypto plan read on 16 November 2026. The operator is DomaineGoat LLC, Panama.
