How to Set Up a Firewall with UFW on Ubuntu Server 24.04

Every new Ubuntu server that faces the internet gets probed within minutes of going online. Bots scan for open ports, try default SSH logins, and poke at database ports that should never be public. A firewall turns most of that noise into nothing: traffic you did not allow gets dropped at the door, and your services only answer on the ports you opened on purpose.

On Ubuntu, the friendly way to do this is UFW, the Uncomplicated Firewall. It sits in front of iptables (or nftables on newer systems) and lets you manage rules with short, readable commands. This guide walks through a complete UFW setup on Ubuntu Server 24.04, from first check to a working rule set for a typical web server. All commands assume you are logged in as a user with sudo privileges.

Step 1: Check UFW status and defaults

Start by seeing what you are working with. Run:

sudo ufw status verbose

On a fresh server you will usually see “Status: inactive”. Before enabling anything, set the default policies. The safe starting point is to block everything coming in and permit everything going out:

sudo ufw default deny incoming
sudo ufw default allow outgoing

This deny-by-default posture means a new service you install later will not accidentally open itself to the internet. You open exactly what you need, port by port.

Step 2: Allow SSH before you enable anything

This is the step people skip and then regret. The moment UFW activates with default-deny and no SSH rule, your current SSH session can get cut off and you may lose access to the server entirely. Always add the SSH rule first:

sudo ufw allow OpenSSH

The OpenSSH profile is a built-in application rule that covers port 22. If your SSH daemon runs on a different port, use the port directly instead:

sudo ufw allow 2222/tcp

While you are at it, add rate limiting on SSH. This throttles repeated login attempts and slows down brute-force scanners:

sudo ufw limit ssh

With limit, UFW blocks any IP that tries to open six or more SSH connections within thirty seconds. Legitimate logins are not affected.

Step 3: Enable UFW

With SSH covered, turn the firewall on:

sudo ufw enable

You will see a warning that the command may disrupt existing SSH connections. Since you already allowed SSH, confirm with y. Verify the result:

sudo ufw status verbose

The output now lists your default policies plus the SSH rules you added. Test that you can still open a fresh SSH session in a second terminal before you close the current one.

Step 4: Open ports for your services

UFW accepts port numbers, service names, and protocol suffixes. For a typical web server, HTTP and HTTPS are the essentials:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

If you run a control panel, monitoring agent, or an application on a custom port, open it the same way. Port ranges are allowed too, which is useful for passive FTP or application clusters:

sudo ufw allow 8080:8090/tcp

For services that should only answer on the local network, restrict the rule to a source address. This example opens MySQL (port 3306) to a single app server and nothing else:

sudo ufw allow from 203.0.113.25 to any port 3306

Replace 203.0.113.25 with the real IP of your other machine. A whole subnet works as well:

sudo ufw allow from 203.0.113.0/24 to any port 3306

Step 5: Block specific addresses and remove stale rules

When a log or fail2ban shows one IP hammering your server, drop it with a single command:

sudo ufw deny from 198.51.100.44

To review what is active, list the rules with numbers:

sudo ufw status numbered

Delete a rule by its number, not by retyping it:

sudo ufw delete 3

Numbered deletion avoids mistakes when a rule contains IP addresses or ranges. Re-run sudo ufw status numbered after every deletion to confirm the list looks right.

Step 6: Use application profiles

UFW ships with profiles for common software, kept in /etc/ufw/applications.d/. See what is available:

sudo ufw app list

Typical output on a server with a few packages installed includes OpenSSH, Nginx Full, Nginx HTTP, and similar entries. Applying a profile opens its known ports in one move:

sudo ufw allow “Nginx Full”

Check what a profile covers before applying it:

sudo ufw app info “Nginx Full”

Profiles save typing, but they open the same ports you would open manually. On a minimal server, adding ports by number is often clearer than pulling in profiles.

Step 7: Turn on logging

Firewall logs tell you who is knocking. Enable logging at a medium level, which records dropped and rejected packets without flooding your disk:

sudo ufw logging medium

Entries land in the system journal and in /var/log/ufw.log. Watch live traffic like this:

sudo tail -f /var/log/ufw.log

If the log gets noisy from constant scanning, drop the level to low, which logs only blocked packets that match no rule. You can switch it off with sudo ufw logging off.

Step 8: Check IPv6 and edge cases

Open /etc/default/ufw and confirm the line IPV6=yes is present. Ubuntu Server 24.04 ships with it enabled, and UFW applies your rules to both IPv4 and IPv6 stacks. If it were set to no, all your IPv6 traffic would bypass the firewall while IPv4 stayed protected, which is an easy hole to miss.

Two commands for troubleshooting: sudo ufw disable turns the firewall off without deleting your rules, and sudo ufw reset wipes every rule and policy back to defaults. Reset is the fastest way to start over if the rule list gets tangled, but it also removes your SSH rule, so only use it when you have console or VNC access as a backup.

Quick reference: a complete first-time setup

Here is the whole sequence for a fresh Ubuntu Server 24.04 web server in one place:

Goal Command
Default policies sudo ufw default deny incoming
sudo ufw default allow outgoing
Allow and rate-limit SSH sudo ufw allow OpenSSH
sudo ufw limit ssh
Activate the firewall sudo ufw enable
Open web ports sudo ufw allow 80,443/tcp
Restrict a database port sudo ufw allow from 203.0.113.25 to any port 3306
Block an abusive IP sudo ufw deny from 198.51.100.44
Review rules sudo ufw status numbered
Delete rule #3 sudo ufw delete 3
Enable logging sudo ufw logging medium

One command in the table deserves a second look: sudo ufw allow 80,443/tcp opens both web ports in a single call. It is equivalent to the two separate commands from Step 4.

Practical takeaways

  • Order matters. Set defaults, allow SSH, then enable. Locking yourself out of a remote server is the classic UFW mistake and it is entirely avoidable.
  • Deny by default, open by exception. Every port should earn its place. If you cannot name the service behind a port, leave it closed.
  • Restrict sensitive ports by source IP. Database and admin ports have no business answering the whole internet. Pin them to the machines that actually need them.
  • Review the rule list regularly. Run sudo ufw status numbered after each change and once a month as a habit. Stale rules from old projects pile up faster than you expect.
  • Keep logs on at a sensible level. Medium logging gives you enough signal to spot scans without drowning in noise.

A working firewall is one of the fastest wins on a new Ubuntu server. Ten minutes of UFW rules buys you a deny-by-default boundary, protected SSH, and a log that shows exactly who is probing your machine. Do it on every server you deploy, right after the first SSH login.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.