How to set up a firewall with UFW
Updated 11/29/2026
A firewall allows only the ports you need and blocks the rest. UFW (Uncomplicated Firewall) makes that simple on Ubuntu.
1. Install
apt-get update -y
apt-get install -y ufw
2. Allow SSH before enabling
Very important: allow SSH first, otherwise you lock yourself out:
ufw allow OpenSSH
3. Allow web services
ufw allow 80/tcp
ufw allow 443/tcp
4. Enable the firewall
ufw enable
ufw status verbose
5. Useful rules
# allow a port only from a specific IP
ufw allow from 1.2.3.4 to any port 3306
# delete a rule
ufw delete allow 80/tcp
The golden rule
Open only the strictly necessary ports. A database, for example, should not be publicly exposed, see Connect to a remote database. Continue with fail2ban protection.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
Single sign-on for your applications with authentik
One account for all your team's apps: first sign-in, users, an OpenID Connect application and two-factor authentication.
Updated 11/29/2026SecurityPasswordless sign-in with Pocket ID
Passkeys for your applications over OpenID Connect: first sign-in, colleagues and connecting an application.
Updated 11/29/2026SecurityHow to protect your server from brute-force attacks with fail2ban
fail2ban automatically bans IPs that repeatedly try wrong passwords. Here is how to install and configure it.
Updated 11/29/2026