How to Install Fail2Ban on Ubuntu VPS to Prevent Brute Force Attacks (2026 Guide)

Fail2Ban is one of the most effective security tools for protecting Linux VPS servers against brute-force attacks. It automatically detects repeated failed login attempts and blocks malicious IP addresses using your firewall.

This guide explains how to install and configure Fail2Ban on Ubuntu 22.04 and Ubuntu 24.04.

Requirements

  • Ubuntu 22.04 or Ubuntu 24.04 VPS
  • Root access or a sudo user
  • UFW or iptables firewall

Step 1: Update Your VPS

sudo apt update
sudo apt upgrade -y

Step 2: Install Fail2Ban

sudo apt install fail2ban -y

Step 3: Enable the Service

sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Step 4: Verify Installation

sudo systemctl status fail2ban

The service should display as active (running).

Step 5: Create a Local Configuration

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Edit the configuration file:

sudo nano /etc/fail2ban/jail.local

Recommended SSH Configuration

[sshd]
enabled = true
port = ssh
maxretry = 5
findtime = 10m
bantime = 1h

Step 6: Restart Fail2Ban

sudo systemctl restart fail2ban

Step 7: Check Active Jails

sudo fail2ban-client status

Example output:

Status
|- Number of jail: 1
`- Jail list: sshd

View Banned IP Addresses

sudo fail2ban-client status sshd

Unban an IP Address

sudo fail2ban-client set sshd unbanip 192.168.1.100

Best Practices

  • Disable direct root SSH login.
  • Use SSH key authentication whenever possible.
  • Change the default SSH port.
  • Keep Ubuntu and Fail2Ban updated.
  • Enable a firewall such as UFW.
  • Review Fail2Ban logs regularly.

Why Install Fail2Ban?

  • Automatically blocks brute-force attacks.
  • Improves SSH security.
  • Reduces unauthorized login attempts.
  • Works with multiple services including SSH, Apache, Nginx, Postfix, and more.
  • Lightweight and easy to maintain.

Conclusion

Fail2Ban is an essential security tool for every Linux VPS. Combined with SSH keys, a firewall, and regular system updates, it significantly improves server protection against automated attacks.

If you're looking for a secure Linux VPS with full root access, NVMe storage, dedicated IPv4, and instant deployment, VPS For Life provides reliable VPS hosting suitable for production environments, developers, and businesses worldwide.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Set Up a Firewall on Your VPS for Better Security

Setting up a firewall on your VPS helps protect it from unauthorized access and attacks. Here’s...

Why You Should Disable Root Login on Your VPS

By default, the root user has full control over your VPS. Disabling direct root login helps...

How to Enable Automatic Security Updates on Your VPS

Automatic security updates ensure your VPS is always protected against the latest...

How to Back Up Your VPS to Prevent Data Loss

Backups are essential for protecting your VPS data from accidental loss or security incidents....

How to Install Fail2Ban on Ubuntu 24.04 VPS

Fail2Ban is one of the most effective security tools for Linux VPS servers. It monitors log files...