Skip to content

Basic Firewall Setup with UFW

A firewall controls which network ports can be reached from the internet. On Ubuntu and Debian servers, UFW is the simplest way to configure basic firewall rules.

Terminal window
apt update
apt install ufw -y

If your SSH port is 22, run:

Terminal window
ufw allow 22/tcp

If you changed SSH to another port, allow that port instead:

Terminal window
ufw allow <your-ssh-port>/tcp

Only open ports you actually use.

ServiceCommand
HTTP websiteufw allow 80/tcp
HTTPS websiteufw allow 443/tcp
Minecraft Javaufw allow 25565/tcp
Minecraft Bedrockufw allow 19132/udp
Pterodactyl Wings SFTPufw allow 2022/tcp

After allowing SSH and any required service ports, enable UFW:

Terminal window
ufw enable

Confirm with y when asked.

Check the current rules:

Terminal window
ufw status verbose

List rules with numbers:

Terminal window
ufw status numbered

Delete the rule by number:

Terminal window
ufw delete <number>

For most servers, use these defaults:

Terminal window
ufw default deny incoming
ufw default allow outgoing

Then explicitly allow the ports you need.

If you accidentally block yourself out, use the server console or rescue tools in your hosting panel if available, then disable UFW:

Terminal window
ufw disable

After regaining access, fix the rules and enable it again.