# How to set up a firewall with UFW?

> How to set up a firewall with UFW in 6 steps: deny incoming by default, allow SSH first, open 80 and 443, rate-limit 22, enable and check the rules.

- URL: https://ironkvm.com/guides/how-to-set-up-a-firewall-with-ufw/
- Last updated: 2026-11-16
- Publisher: IronKVM (https://ironkvm.com)

UFW is a short front end to the Linux packet filter. Six commands lock a VPS down without locking you out.

**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 limit` denies 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?

*Common UFW rules for a VPS, with syntax from the ufw manual and Ubuntu Server documentation*

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

1. **Install UFW** — Debian images may not include it. Install the ufw package with apt.
2. **Set the default policies** — Deny incoming and allow outgoing. These are the defaults, but setting them makes the intent clear.
3. **Allow SSH first** — Add ufw limit 22/tcp, or allow your custom SSH port, before turning the firewall on.
4. **Open the service ports** — Allow 80/tcp and 443/tcp for a website, 51820/udp for WireGuard, nothing more.
5. **Enable the firewall** — Run ufw enable. Under SSH it asks for confirmation; answer y.
6. **Check the rules** — Run ufw status verbose, and open a second SSH session to be sure you still get in.

*A web server firewall in six commands*

```bash
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 verbose
```

## How 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](https://ironkvm.com/guides/how-to-secure-an-offshore-vps/ "How to secure an offshore VPS?") 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](https://ironkvm.com/guides/how-to-install-fail2ban/ "How to install fail2ban?") 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](https://ironkvm.com/guides/how-to-run-docker-on-a-vps/ "How to run Docker on a VPS?") shows the syntax. WireGuard has no such issue; the [WireGuard guide](https://ironkvm.com/guides/how-to-set-up-wireguard-on-a-vps/ "How to set up WireGuard on a VPS?") 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

1. [ufw(8) manual, Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/en/man8/ufw.8.html) — Canonical (read 2026-11-16)
2. [Firewalls](https://ubuntu.com/server/docs/how-to/security/firewalls/) — Canonical (read 2026-11-16)
3. [Packet filtering and firewalls](https://docs.docker.com/engine/network/packet-filtering-firewalls/) — Docker (read 2026-11-16)
4. [IronKVM offshore VPS survey (CSV)](https://ironkvm.com/data/ironkvm-offshore-vps-survey-2026-11-16.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. [Order Girder](https://ironkvm.com/app/checkout/?plan=girder&term=12m&from=guide-ufw&lang=en) · [Compare the plans](https://ironkvm.com/pricing/ "IronKVM prices 2026: how much does an offshore KVM VPS cost?")

---

IronKVM: IronKVM is an offshore KVM VPS provider with servers in the Netherlands, Romania and Moldova that takes no action on DMCA takedown notices, for content that is legal in the server's country. It is paid in crypto with no ID upload and run by DomaineGoat LLC, a company registered in Panama. On the 12-month term each plan costs at least 25% less than a named comparable plan read on 16 November 2026.
