How To Set Up a Firewall With UFW on Ubuntu
Goal
In this tutorial, you will install and configure UFW on Ubuntu, set a safe default-deny inbound policy, open only the ports your server needs, rate-limit SSH against brute-force attempts, restrict a service to a specific trusted IP, and verify the firewall is enforcing the rules correctly.
Prerequisites
- A server or VPS running Ubuntu 22.04 or 24.04
- A non-root user with sudo privileges
- An active SSH connection to the server
Let an AI agent do this for you
Copy a ready-made prompt for an AI coding assistant with terminal access to your server (Claude Code, Cursor, or similar) — it can carry out the steps below for you. Review what it plans to run before it executes anything.
UFW (Uncomplicated Firewall) is a front-end for iptables/nftables that ships with Ubuntu and makes it practical to manage firewall rules with short, readable commands instead of raw netfilter syntax. A freshly provisioned server has no firewall enforcing which ports are reachable, which means every listening service is exposed to the entire internet by default. This tutorial walks through installing UFW, setting a deny-by-default policy, opening only the ports you actually need, rate-limiting SSH against brute-force attempts, and verifying the rules work as expected.
Step 1 — Check that UFW is installed
UFW ships by default on Ubuntu, but confirm it's present before relying on it.
sudo ufw statusIf this returns "Status: inactive" or a rule list, UFW is installed and you can move on. If you instead get "command not found", install it from Ubuntu's repositories:
sudo apt update
sudo apt install -y ufwStep 2 — Set the default policy
Before opening any specific ports, set the baseline policy: deny all incoming connections, allow all outgoing ones. This means nothing is reachable until you explicitly allow it, while your server can still make outbound requests (updates, API calls, etc.) without restriction.
sudo ufw default deny incoming
sudo ufw default allow outgoingThese are stored as defaults and apply to any port that doesn't have a more specific rule, so you never end up with an implicitly open service.
Step 3 — Allow SSH before enabling UFW
“Do this step before running "ufw enable". If you enable UFW with the default-deny incoming policy active and no SSH rule in place, your existing SSH session will be cut off and you will be locked out of the server with no way back in except console/recovery access from your provider.”
Allow SSH using the registered OpenSSH application profile, which maps to port 22/tcp:
sudo ufw allow OpenSSHIf for some reason the OpenSSH profile isn't registered (for example, `openssh-server` was installed non-standard, or `ufw app list` doesn't show it), allow the port directly instead:
sudo ufw allow 22/tcpIf your SSH daemon listens on a non-default port, substitute that port number instead of 22 here — allowing 22/tcp won't help if sshd is actually bound to something else.
Tricknowtech VPS Hosting
Dedicated KVM resources and full root access — deployed in under 60 seconds, no ticket required.
Ready to try it yourself?
Create a free account and follow along.