Skip to content

How to Install Fail2Ban on Your Linux VPS

Fail2Ban protects your server from brute-force attacks by automatically banning IP addresses after multiple failed login attempts.

Install Fail2Ban

  1. Update system
    First, update the package lists:

    bash
    sudo apt update
    sudo apt update
  2. Install Fail2Ban
    Install Fail2Ban with the following command:

    bash
    sudo apt install fail2ban -y
    sudo apt install fail2ban -y
  3. Enable service
    Make sure Fail2Ban starts automatically on boot:

    bash
    sudo systemctl enable fail2ban
    sudo systemctl enable fail2ban

Configure Fail2Ban

  1. Create configuration file
    Create a local configuration file so your settings are not overwritten during updates:

    bash
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  2. Edit configuration
    Open the file:

    bash
    sudo nano /etc/fail2ban/jail.local
    sudo nano /etc/fail2ban/jail.local
  3. Enable SSH protection
    Find the [sshd] section and adjust it:

    ini
    [sshd]
    enabled = true
    port = ssh
    maxretry = 5
    bantime = 3600
    findtime = 600
    [sshd]
    enabled = true
    port = ssh
    maxretry = 5
    bantime = 3600
    findtime = 600
    SettingDescription
    enabledEnables SSH protection
    portThe SSH port (change this if you have modified your SSH port)
    maxretryMaximum failed attempts before an IP is banned
    bantimeBan duration in seconds (3600 = 1 hour)
    findtimeTime window in seconds in which failed attempts are counted
  4. Restart Fail2Ban
    Save with Ctrl + O, close with Ctrl + X and restart Fail2Ban:

    bash
    sudo systemctl restart fail2ban
    sudo systemctl restart fail2ban

Check status

Check if Fail2Ban is running:

bash
sudo systemctl status fail2ban
sudo systemctl status fail2ban

Show the status of SSH protection:

bash
sudo fail2ban-client status sshd
sudo fail2ban-client status sshd

Tip

You can unban a blocked IP address with sudo fail2ban-client set sshd unbanip IP_ADDRESS.