Home UbuntuUFW Firewall on Ubuntu 24.04: Secure Your Server Fast

UFW Firewall on Ubuntu 24.04: Secure Your Server Fast

by Knowledge Base Linux
UFW Firewall on Ubuntu 24.04 — UFW Firewall on Ubuntu 24.04: Secure Your Server Fast

Most Ubuntu servers are exposed to the internet within seconds of booting, and every open port is an invitation for automated scanners to knock. A UFW firewall Ubuntu setup is the simplest effective answer: UFW (Uncomplicated Firewall) is a friendly front end for iptables and nftables that lets you allow only the services you actually run and block everything else by default.

UFW firewall Ubuntu rules filtering allowed and blocked ports on an Ubuntu server

This guide walks through a complete UFW firewall Ubuntu configuration on Ubuntu 24.04: installing UFW, allowing SSH before you switch it on, opening web ports, rate-limiting SSH, and checking and maintaining your rules. If you run Nginx, see also our guide to setting up an Nginx reverse proxy on Ubuntu 24.04, and for log investigation after a blocked attempt, our journalctl log filtering guide.

Prerequisites – UFW Firewall on Ubuntu 24.04

  • A server or VM running Ubuntu 24.04 LTS with sudo access.
  • SSH access that you can test in a second terminal window before enabling the firewall.
  • Knowledge of which ports your services need: SSH is 22, HTTP is 80, HTTPS is 443 by default.

UFW Firewall Ubuntu Basics: Install and Check Status – UFW Firewall on Ubuntu 24.04

UFW is usually installed by default on Ubuntu, but it is inactive until you enable it. Start by checking the current state and the numbered rules list, which is empty on a fresh system:

sudo apt update
sudo apt install ufw -y
sudo ufw status verbose
sudo ufw status numbered

The verbose output shows the default policies. Before you enable anything, set the defaults explicitly: deny all incoming traffic and allow all outgoing traffic. Outgoing allowance matters because your server still needs to fetch updates and talk to package mirrors, while incoming denial closes every port you have not deliberately opened.

sudo ufw default deny incoming
sudo ufw default allow outgoing

Allowing SSH Before You Enable UFW Firewall Ubuntu

This is the step people get wrong, and it can lock you out of a remote server. Always allow SSH before enabling UFW, and keep a second SSH session open while you test. If you changed the SSH port, allow that port instead of 22.

sudo ufw allow ssh
sudo ufw allow 22/tcp
sudo ufw allow 2222/tcp

The first command is a shortcut for the ssh service profile, which maps to port 22. If you use a custom port, the third command is the one that matters; run only the commands that match your setup. Once SSH is allowed, you can enable the firewall:

sudo ufw enable
sudo ufw status verbose

UFW will warn that enabling it may disrupt existing SSH connections. Type y to confirm, then test a brand-new SSH connection from your second terminal. Do not close your original session until the new connection works.

Opening Web and Application Ports with UFW Firewall Ubuntu

For a web server, allow HTTP and HTTPS. UFW understands common service names, so you can use the readable form or the explicit port form:

sudo ufw allow http
sudo ufw allow https
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

For other services, allow the specific port and protocol. A database should normally not be exposed to the whole internet at all; if you must allow it temporarily, restrict it to a single trusted IP instead of opening it globally:

sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp
sudo ufw allow from 203.0.113.0/24 to any port 5432 proto tcp

Replace the example addresses with your own trusted IP. The from form is the single most valuable UFW habit for admin panels, databases, and monitoring dashboards: the port is open only to the address that needs it.

Rate-Limiting SSH with UFW Firewall Ubuntu

Brute-force bots hammer port 22 constantly. UFW has a built-in limit rule that blocks an IP after six connection attempts within 30 seconds, which stops most noisy scanners without affecting legitimate logins:

sudo ufw limit ssh
sudo ufw limit 22/tcp

Use only one of these two commands; they do the same thing for the default port. If you run SSH on a custom port, use limit with that port number. Verify the rule appears in the numbered list, and remember that rate limiting is a supplement to key-based SSH login and disabled password authentication, not a replacement for them.

Managing, Deleting, and Resetting UFW Rules

Rules accumulate over time, so audit them regularly. The numbered list makes deletion precise, and you should always re-check status after a change:

sudo ufw status numbered
sudo ufw delete 3
sudo ufw delete allow 8080/tcp
sudo ufw reload

Deleting by number removes the rule at that position, so list the rules again before and after. The delete allow form removes a rule by its description and is safer when you are not sure about numbering. If a server’s rules become a mess, reset UFW completely and rebuild from SSH upward:

sudo ufw reset
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable

Never run ufw reset on a remote server unless you have console or out-of-band access, because it disables the firewall and removes all rules in one step.

Troubleshooting UFW Firewall Ubuntu Problems

Locked out after enabling UFW. Use your hosting provider’s web console or rescue mode, log in locally, and run sudo ufw allow ssh followed by sudo ufw enable again. Then fix the missing rule before closing the console session.

A service is unreachable even though its port is allowed. Check three layers: the UFW rule with sudo ufw status verbose, whether the service is actually listening with sudo ss -tlnp, and whether a cloud security group or hosting firewall is also filtering traffic. UFW cannot open a port that your cloud provider blocks upstream.

Rules show as active but traffic still passes. Confirm UFW is enabled and not just configured: status must say active. Also check for Docker, which can insert its own iptables rules that bypass UFW for published ports; for Docker hosts, restrict published ports to localhost or manage that interaction explicitly.

Logs are noisy. UFW logging is off by default. Enable low-volume logging only while debugging, then turn it off again, because full logging on a public server fills disks quickly:

sudo ufw logging on
sudo ufw logging off
sudo grep UFW /var/log/syslog | tail -n 50

UFW Firewall Ubuntu Maintenance Checklist

Revisit your rules whenever you add or remove a service. A short quarterly routine keeps the firewall honest: list numbered rules, delete anything you no longer recognise, confirm SSH is rate-limited, and confirm the default incoming policy is still deny. Pair that with automatic security updates so the services behind the firewall stay patched, and document any IP-restricted rules so a teammate can tell intentional access apart from a forgotten experiment.

A UFW firewall Ubuntu configuration does not need to be complicated to be effective. Deny by default, allow SSH first, open only the ports your services need, restrict sensitive ports to trusted IPs, and rate-limit SSH. That short set of habits closes the most common path into a fresh Ubuntu server, and it takes less than ten minutes to put in place. For the official reference, see the Ubuntu UFW documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.