# How do I set up a firewall on a VPS?

> How to set up a firewall on a VPS with ufw in 6 steps: default deny, allow SSH first, open web ports, enable, check status and add IPv6.

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

**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 OpenSSH` before `ufw enable`, or you lock yourself out.
- **Defaults**: deny incoming, allow outgoing.
- **IPv6**: covered when `IPV6=yes` in `/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.**

1. **Install ufw** — Ubuntu usually ships it. On Debian, run apt install ufw.
2. **Set the defaults** — Run ufw default deny incoming and ufw default allow outgoing.
3. **Allow SSH first** — Run ufw allow OpenSSH, or ufw allow 22/tcp. If SSH listens on another port, allow that number instead.
4. **Open service ports** — Add only what you run: ufw allow 80/tcp and ufw allow 443/tcp for a website, ufw allow 51820/udp for WireGuard.
5. **Enable the firewall** — Run ufw enable and answer y. Your current SSH session stays connected.
6. **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.**

*ufw rules for common services on a VPS*

| 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](https://saltvps.com/help/reboot-reinstall-delete/ "How do I reboot, reinstall or delete a SaltVPS server?") 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](https://saltvps.com/guides/how-to-install-docker-on-a-vps/ "How do I install Docker on a VPS?") 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](https://saltvps.com/guides/how-to-secure-a-linux-vps/ "How do I secure a Linux VPS?"), followed by [Fail2ban](https://saltvps.com/guides/how-to-install-fail2ban-on-a-vps/ "How do I install Fail2ban on a VPS?").

## 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

1. [ufw(8) manual page](https://manpages.ubuntu.com/manpages/noble/en/man8/ufw.8.html) — Ubuntu (2026-11-16)
2. [Ubuntu Server documentation: firewall](https://documentation.ubuntu.com/server/) — Canonical (2026-11-16)
3. [firewalld documentation](https://firewalld.org/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. [Order Grain for 12 months](https://saltvps.com/app/checkout/?plan=grain&term=12m&from=guides-how-to-set-up-a-ufw-firewall-on-a-vps&lang=en) · [All guides](https://saltvps.com/guides/ "Which VPS guides and Linux tutorials does SaltVPS publish?")

---

SaltVPS: SaltVPS sells Linux KVM VPS in New York, Dallas, Los Angeles, London, Amsterdam and Frankfurt, paid in crypto with no ID document. Seven plans from 2 to 32 GB of RAM cost the same in all six cities, and on the 12-month term each is priced at least 25% under a named comparable plan of a no-KYC crypto host, read on 16 November 2026. The operator is DomaineGoat LLC, Panama.
