Configure a UFW firewall
Close unnecessary ports and allow only SSH, HTTP and HTTPS on your instance.
Ready to start your service? Check offers and order online.
View offers and orderIntroduction
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.
Installation
On Debian, UFW is not always installed by default:
apt update
apt install ufw -yOn Ubuntu, UFW is usually already present. Check with ufw --version.
Basic configuration
-
Set default rules
Refuse unauthorized incoming connections, allow outputs:
ufw default deny incoming
ufw default allow outgoing-
Allow SSH
ufw allow OpenSSHIf you are using a custom SSH port (e.g. 2222):
ufw allow 2222/tcp -
Allow HTTP and HTTPS
ufw allow 'Nginx Full'Manual equivalent:
ufw allow 80/tcpandufw allow 443/tcp -
Enable firewall
ufw enableConfirm with
y. UFW will start automatically at boot.
Check Status
ufw status verboseYou must see the active rules, for example:
Status: active
To Action From
-- ------ ----
22/tcp (OpenSSH) ALLOW Anywhere
80,443/tcp (Nginx Full) ALLOW AnywhereCommon 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 22Allow an application port
ufw allow 8080/tcp comment 'API interne'Delete or modify a rule
List the numbered rules:
ufw status numberedDelete Rule 2:
ufw delete 2Disable temporarily UFW
ufw disableTo be used only for diagnostic purposes. Reactivate as soon as possible with ufw enable.
Next steps
- Deploy a site with Nginx
- First SSH connection