How to Set Up UFW and Fail2ban on Ubuntu: A Practical Guide

A fresh Ubuntu server is reachable from the whole internet within minutes of booting, and automated login attempts start almost as fast. Two tools handle most of that exposure: UFW, the friendly front end for the Linux firewall, and Fail2ban, which watches logs and blocks addresses that keep failing to log in. This ufw fail2ban ubuntu setup covers both, in the order you should apply them.

Work through the steps in order and do not lock yourself out. Keep one SSH session open while you test firewall rules in a second session, so a mistake costs you a terminal tab, not server access.

UFW Fail2ban Ubuntu Setup: Start with the Firewall

UFW ships with Ubuntu but is not enabled by default. Before enabling it, add the rules you need. The most common self-lockout is enabling UFW before allowing SSH, so make SSH the first rule:

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose

That single rule keeps port 22 open. Then tighten the rest. Deny incoming traffic by default, allow outgoing traffic, and add only the services you actually run. A web server needs ports 80 and 443:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Good ubuntu firewall rules follow a simple principle: every open port should map to a service you can name. Run sudo ss -tlnp and compare the listening ports with your UFW rules. Any port listening without a rule you added on purpose deserves a second look.

Lock Down SSH Before You Rely on the Firewall

A firewall controls which ports answer. SSH hardening controls who can get in through the open port. Edit /etc/ssh/sshd_config and set:

PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes

Then restart SSH with sudo systemctl restart ssh. Key-only login removes the entire password-guessing attack surface, which is the single biggest win on this page. If you have not set up keys yet, do that first and confirm you can log in with the key before disabling passwords. For the full hardening picture, our 15-step fresh Ubuntu server hardening guide covers users, updates, and services beyond the firewall.

Consider moving SSH off port 22 as well. It does not make you secure on its own, but it cuts the volume of background noise in your logs, which makes real incidents easier to spot. If you move it, update the UFW rule to match and test from a second session before closing the first.

Install and Configure Fail2ban

Fail2ban reads log files and temporarily bans IP addresses after repeated failures. Install it and the firewall integration:

sudo apt update
sudo apt install fail2ban
sudo systemctl enable --now fail2ban

Never edit jail.conf directly. Copy settings into /etc/fail2ban/jail.local so updates do not overwrite your work. A solid starting point for fail2ban jail configuration looks like this:

[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
banaction = ufw

[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s

These values mean an address that fails 5 times within 10 minutes is banned for an hour, and the ban is applied through UFW. Check status with sudo fail2ban-client status sshd, which shows banned addresses and failure counts. Test from another machine or phone hotspot: fail a few logins on purpose and watch the ban appear.

For ssh brute force protection to stay useful, tune the numbers to your traffic. A bantime of one hour stops casual scanners; persistent attackers rotate addresses, so longer bans add little. The real defense is key-only login plus Fail2ban as a noise filter, not Fail2ban alone.

Extend Protection Beyond SSH

Once SSH is covered, look at your other public services. If you run a web app with a login page, add a jail for its logs or put it behind a web application firewall. Fail2ban ships with filters for Nginx and Apache; enable the ones matching your stack and point them at the right log paths.

Keep an eye on the logs after setup. sudo fail2ban-client status lists active jails, and sudo ufw status numbered shows the rules Fail2ban added. If a legitimate user gets banned, unban with sudo fail2ban-client set sshd unbanip <IP> and consider whitelisting trusted office or home addresses with ignoreip in jail.local.

Our Ubuntu 24.04 production security checklist is a good next read once this setup is running, since it covers unattended upgrades and audit logging that pair well with a firewall and a ban system.

Maintenance: Keep Both Tools Updated and Tested

Security tools rot quietly. Review UFW rules after every new service install, since installers sometimes open ports you did not ask for. Re-check Fail2ban after every major upgrade, since log formats change and a filter that stops matching is a filter that stops protecting. A monthly five-minute check beats discovering a dead jail during an incident.

Finally, remember that UFW and Fail2ban protect one server. If you run a pipeline that deploys to that server, the deployment path needs the same care. Our guide to securing a CI/CD pipeline covers secrets handling and runner hardening for the other half of the problem.

Quick Checklist

  • UFW enabled with SSH allowed before anything else
  • Default deny incoming, only named services allowed
  • SSH set to key-only login, root login disabled
  • Fail2ban running with an sshd jail and UFW as the ban action
  • Ban tested from a second connection, unban command known

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.