Fail2Ban on VPS: Complete Guide to Securing Your Server Against Brute-Force Attacks

Fail2Ban on VPS: Complete Guide to Securing Your Server Against Brute-Force Attacks

untitled-design-21.png

Securing a VPS is one of the most important tasks for any server owner, especially when hosting production applications, websites, or databases. One of the biggest risks every VPS faces is repeated brute-force login attempts—attackers scanning the internet for open services and trying thousands of password combinations. While firewalls and strong passwords help, an automated intrusion-prevention system is essential to fully protect your server. Fail2Ban is one of the most effective and lightweight tools for this purpose.

Fail2Ban monitors system logs and automatically bans IP addresses that show suspicious behavior, such as failing multiple login attempts within a short timeframe. Instead of manually reviewing logs or blocking IPs yourself, Fail2Ban handles the entire process based on flexible rules and filters. Whether you’re using an AVIOX Cloud VPS for web hosting, development, or Docker deployments, installing Fail2Ban is one of the first security steps you should take.

How Fail2Ban Works

Fail2Ban operates by reading log files generated by various services such as SSH, Apache, NGINX, Postfix, vsftpd, and more. Each “jail” watches a specific service. When Fail2Ban detects repeated failed login attempts or known malicious patterns, it automatically blocks the IP using firewall rules—typically through iptables, nftables, or firewalld. The block lasts for a configurable amount of time, preventing attackers from continuing their attempts.

For example, if someone tries to log in to your server via SSH and fails five times, Fail2Ban bans that IP for a duration you define—often 10 minutes to several hours. This significantly slows down automated bots and brute-force tools.

Installing Fail2Ban on Ubuntu/Debian VPS

Fail2Ban is available in default package repositories for most Linux distributions. Installation on an AVIOX Cloud Ubuntu or Debian VPS is very straightforward.

 
sudo apt update sudo apt install fail2ban -y

Once installed, the Fail2Ban service starts automatically. You can verify this with:

 
sudo systemctl status fail2ban

If you’re using CentOS, AlmaLinux, or Rocky Linux:

 
sudo yum install epel-release -y sudo yum install fail2ban -y

Fail2Ban is now installed, but not yet fully configured.

Configuring Fail2Ban for SSH Protection

SSH is the most common target for brute-force attacks. Enabling Fail2Ban for SSH drastically reduces unauthorized attempts.

Fail2Ban configuration files are located in:

 
/etc/fail2ban/

You should never edit the default jail.conf. Instead, create a local override file:

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

Open the file:

 
sudo nano /etc/fail2ban/jail.local

Under [sshd], enable the jail:

 
[sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 5 bantime = 10m findtime = 10m

This configuration means:

  • maxretry 5 → fail 5 times = IP banned

  • findtime 10 minutes → failures counted within 10 minutes

  • bantime 10 minutes → attacker blocked for 10 minutes

You can adjust these values to be stricter, such as banning for hours or even permanently.

Restart Fail2Ban to apply changes:

 
sudo systemctl restart fail2ban

Check active jails:

 
sudo fail2ban-client status 

You should see the SSH jail listed and running.

Customizing Ban Settings

Fail2Ban is extremely customizable. Some useful settings include:

Permanent bans for persistent offenders

 
bantime = -1 

Longer ban times

 
bantime = 1h findtime = 30m

Email notifications

You can configure Fail2Ban to notify you when a ban happens by updating the action parameter:

 
action = %(action_mwl)s

Protecting NGINX & Apache With Fail2Ban

Fail2Ban can also protect your web server. It blocks attackers attempting directory scans, SQL injections, or repeated 404 requests.

For NGINX:

Enable the jail:

 
[nginx-http-auth] enabled = true filter = nginx-http-auth logpath = /var/log/nginx/error.log

For Apache:

 
[apache-auth] enabled = true logpath = /var/log/apache2/error.log

Restart Fail2Ban after enabling web server jails.

Viewing Banned IPs

You can check banned IPs at any time:

 
sudo fail2ban-client status sshd

To unban an IP:

 
sudo fail2ban-client set sshd unbanip x.x.x.x

Fail2Ban + Firewall = Strong Security

On AVIOX Cloud VPS, Fail2Ban works beautifully with existing firewalls like:

  • UFW

  • iptables

  • nftables

  • CSF Firewall

It automatically inserts rules to block attackers without affecting your existing configuration.

Why Fail2Ban Is Essential on AVIOX Cloud VPS

Fail2Ban solves the most common server threat—automated brute-force attacks. Without it, bots will continuously try SSH passwords, potentially gaining access through weak credentials. With Fail2Ban, these attempts are blocked early and automatically.

Fail2Ban is lightweight, stable, and reliable. For any AVIOX Cloud server—whether hosting WordPress, Laravel, APIs, or private apps—it is one of the best security layers you can add.


Share:


Your experience on this site will be improved by allowing cookies Cookie Policy