Intermediate 10 min

Configure a UFW firewall

Close unnecessary ports and allow only SSH, HTTP and HTTPS on your instance.

#UFW#Security#Firewall

Ready to start your service? Check offers and order online.

View offers and order

Introduction

UFW (Uncomplicated Firewall) is the default firewall on Ubuntu and available on Debian. It allows only the strictly necessary ports to be exposed and the rest of the incoming traffic to be blocked.

This guide starts from the principle that you are connected in SSH to your VPS with root or sudo rights.

Important Always allow the SSH (22) port before to activate UFW. Otherwise, you may cut off remote access.

Installation

On Debian, UFW is not always installed by default:

apt update apt install ufw -y

On Ubuntu, UFW is usually already present. Check with ufw --version.

Basic configuration

  1. Set default rules

    Refuse unauthorized incoming connections, allow outputs:

ufw default deny incoming ufw default allow outgoing
  1. Allow SSH
    ufw allow OpenSSH

    If you are using a custom SSH port (e.g. 2222): ufw allow 2222/tcp

  2. Allow HTTP and HTTPS
    ufw allow 'Nginx Full'

    Manual equivalent: ufw allow 80/tcp and ufw allow 443/tcp

  3. Enable firewall
    ufw enable

    Confirm with y. UFW will start automatically at boot.

Check Status

ufw status verbose

You must see the active rules, for example:

Status: active To Action From -- ------ ---- 22/tcp (OpenSSH) ALLOW Anywhere 80,443/tcp (Nginx Full) ALLOW Anywhere

Common use cases

Game server (Minecraft, FiveM...)

Open only the game port, in addition to SSH:

ufw allow 25565/tcp # Minecraft Java ufw allow 30120/udp # FiveM (exemple)

Restrict SSH to a fixed IP

If you still connect from the same address:

ufw delete allow OpenSSH ufw allow from VOTRE_IP_PUBLIQUE to any port 22

Allow an application port

ufw allow 8080/tcp comment 'API interne'

Delete or modify a rule

List the numbered rules:

ufw status numbered

Delete Rule 2:

ufw delete 2

Disable temporarily UFW

ufw disable

To be used only for diagnostic purposes. Reactivate as soon as possible with ufw enable.

Anti-DDoS network Anthony IT Solutions already filters upstream volumetric attacks. UFW completes this protection by limiting the exposure surface of your VPS to the system level.

Next steps