ACLClouds
ACLClouds

How do I set up the UFW firewall on an Ubuntu or Debian VPS?

Install and enable UFW without losing SSH, allow ports, ranges and IP addresses, rate-limit SSH, delete rules, read the logs and avoid the Docker pitfalls.

UFW (Uncomplicated Firewall) is the simplest tool to filter the incoming traffic of a Linux VPS. It writes the iptables rules for you, for IPv4 and IPv6 at the same time. This guide was checked with UFW 0.36.2 on Ubuntu 24.04 and Debian 12: run every command as root, or with sudo in front.

Step-by-step procedure

  1. Install UFW

    UFW is often already there but inactive on Ubuntu; on Debian 12, install it with apt install ufw. ufw status should answer Status: inactive: no rule is applied yet.

  2. Set the default policies

    ufw default deny incoming refuses everything that arrives without a rule, ufw default allow outgoing lets the VPS reach the Internet for updates, DNS and APIs.

  3. Allow SSH first

    Run ufw limit OpenSSH, or ufw limit 2222/tcp if SSH listens on another port. limit accepts the connection but blocks an address that opens 6 of them within 30 seconds. Check the list with ufw show added before enabling.

  4. Enable the firewall

    ufw enable applies the rules right away and at every boot. Keep the SSH session open and open a second one: if it connects, access is safe.

  5. Open the ports of your services

    Add one rule per service: ufw allow 80,443/tcp for a website, ufw allow 25565/tcp for Minecraft Java. Without a protocol, ufw allow 27015 opens the port in TCP and UDP. Add comment and a text to find the rule again later.

  6. Check from outside

    ufw status verbose sums up the policies and the rules. From another machine, nc -vz VPS_IP 443 confirms that a TCP port answers. The ACLClouds panel firewall filters before the VM: it must let the same ports through.

Basic setup without losing SSH

apt update && apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw limit OpenSSH
ufw show added
ufw enable
ufw status verbose

Common rules

ufw allow 80,443/tcp comment 'Site web'
ufw allow 25565/tcp comment 'Minecraft Java'
ufw allow 19132/udp
ufw allow 30120
ufw allow 30000:30100/udp
ufw allow from 203.0.113.10 to any port 3306 proto tcp
ufw allow from 203.0.113.0/24 to any port 22 proto tcp
ufw insert 1 deny from 198.51.100.7

List, delete and reset

ufw status numbered
ufw delete allow 25565/tcp
ufw delete 3
ufw app list
ufw logging low
ufw disable
ufw --force reset

Before you start

A badly set firewall cuts your own access: note the real SSH port with ss -tlnp before you start. If you lock yourself out, the noVNC console in the panel works without SSH: log in and run ufw disable.

  • UFW and the panel firewall are two layers: the panel filters before the VM, UFW inside it. A port must be open in both.
  • IPv6 is handled by default (IPV6=yes in /etc/default/ufw): every rule is also created as (v6).
  • Use only one tool: if nftables or firewalld already holds rules, disable it or stay with it rather than mixing.

Rule order matters

UFW reads the rules from top to bottom and stops at the first one that matches. If ufw allow OpenSSH comes before ufw limit 22/tcp, the limit never applies: use limit instead of allow for SSH. To block an address that is already attacking, put the rule at the top with ufw insert 1 deny from followed by the address.

  • ufw allow from 203.0.113.10 to any port 3306 proto tcp keeps a database for a single address; a /24 notation covers a whole network.
  • A range is written with a colon and a mandatory protocol: ufw allow 30000:30100/udp.
  • ufw app list shows the profiles provided by the installed packages, such as OpenSSH or Nginx Full (80 and 443/TCP).

Delete a rule and read the logs

The safest form repeats the rule: ufw delete allow 25565/tcp removes the IPv4 and the IPv6 versions. By number, ufw delete 3 removes only one line of ufw status numbered; the (v6) line stays and the numbers change after each deletion, so list again each time.

ufw logging low records the blocked packets with the [UFW BLOCK] prefix: in /var/log/ufw.log if rsyslog is installed, otherwise with journalctl -k | grep UFW. ufw --force reset disables the firewall and wipes all the rules, after keeping a copy of the old files in /etc/ufw.

UFW and Docker

Docker writes its own iptables rules for published ports, and they come before UFW: a container started with -p 8080:80 stays reachable even if UFW does not allow 8080. Publish on 127.0.0.1:8080:80 what must stay local behind a proxy, or filter the port with the panel firewall, which acts before the VM.

Which VPS for a server protected by UFW?

UFW runs on every ACLClouds KVM Linux VPS, with Ubuntu or Debian to choose at order time. On 17 November 2026, the VPS Mini (2 vCPU, 8 GB of RAM, 40 GB of storage) costs €4.99 per month without commitment, or €3.99 per month with a one-year commitment, and the VPS Essentiel (4 vCPU, 16 GB of RAM, 80 GB) €9.99 per month. The network goes up to 1 Gbit/s, and the panel firewall comes on top of UFW at no extra cost.

Quick check

  • ufw status verbose shows Status: active and deny (incoming).
  • A new SSH session opens after enabling.
  • Each open port matches a running service.
  • The same ports are allowed in the panel firewall.

Frequently asked questions

Is UFW installed by default on Ubuntu and Debian?

On Ubuntu, UFW is usually there but disabled: ufw status answers Status: inactive. Debian 12 does not install it: apt install ufw is enough. In both cases, nothing is filtered until you run ufw enable.

I enabled UFW and SSH no longer answers, what should I do?

The SSH port was not allowed, or SSH listens on a port other than 22. Open the noVNC console from the panel, log in with the local account, then run ufw allow 22/tcp (or your port) or ufw disable while you fix it.

The port is allowed in UFW but still unreachable, why?

Three causes come up: the panel firewall blocks this port, the application only listens on 127.0.0.1 (check with ss -tlnp), or the protocol is wrong, for example TCP opened for a game that uses UDP. A deny rule placed higher in ufw status numbered also wins.

Is UFW enough to protect a VPS?

It closes the unused ports, which greatly reduces the attack surface, but it does not protect a service you open. Add key-based SSH login, Fail2ban against password guessing and automatic security updates.