How to Install and Configure Fail2Ban on Your Dedicated Server
Fail2Ban is an essential tool for protecting your dedicated server from brute force attacks and suspicious login attempts. This step-by-step guide explains how to install and configure Fail2Ban, ensuring your server is better protected from potential threats.

Step 1: Update your server
Keeping your server up to date is the first step in securing it and installing Fail2Ban. Update the system packages using the commands below:

  • For Debian/Ubuntu, run:
    sudo apt update && sudo apt upgrade -y
    
  • For CentOS/RHEL, use:
    sudo yum update -y
    

Step 2: Install Fail2Ban
Fail2Ban can be installed via your server’s package manager:

  • For Debian/Ubuntu, run:
    sudo apt install fail2ban -y
    
  • For CentOS/RHEL, perform these two steps:
    • Enable the EPEL repository:
      sudo yum install epel-release -y
      
    • Install Fail2Ban:
      sudo yum install fail2ban -y
      

Step 3: Start and enable the Fail2Ban service
After installation, start Fail2Ban and ensure it runs automatically at boot. Use the following commands:

sudo systemctl start fail2ban  
sudo systemctl enable fail2ban  

To check its status, run:

sudo systemctl status fail2ban  

Step 4: Configure Fail2Ban
Create a custom configuration file to ensure changes are not overwritten during updates. Follow these steps:

  • Navigate to the Fail2Ban configuration directory:
    cd /etc/fail2ban  
    
  • Copy the default configuration file to create a local configuration file:
    sudo cp jail.conf jail.local  
    

Step 5: Edit and customize Fail2Ban settings
Open the jail.local file to set your preferences:

sudo nano /etc/fail2ban/jail.local  

In the [DEFAULT] section, configure the following settings:

  • Set the ban duration:
    bantime = 600  
    
  • Define the time window for monitoring:
    findtime = 600  
    
  • Specify the number of retries allowed before banning:
    maxretry = 5  
    

Step 6: Enable protection for SSH
Scroll to the [sshd] section in the configuration file and modify it as follows:

[sshd]  
enabled = true  
port = ssh  
logpath = /var/log/auth.log  
maxretry = 5  

Save the file and exit.

Step 7: Verify and restart Fail2Ban
Test the configuration file for syntax errors with this command:

sudo fail2ban-client -d  

If no errors are detected, restart the Fail2Ban service to apply changes:

sudo systemctl restart fail2ban  

Step 8: Monitor Fail2Ban activity
To check which IPs have been banned and review activity, use the following commands:

  • To list all active jails:
    sudo fail2ban-client status  
    
  • To check details of a specific jail (e.g., SSH):
    sudo fail2ban-client status sshd  
    
  • To view the logs:
    sudo tail -f /var/log/fail2ban.log  
    

Step 9: Unban an IP address (if necessary)
If a legitimate user’s IP is mistakenly banned, unban it using this command:

sudo fail2ban-client set <jail_name> unbanip <IP_address>  

For example, to unban an IP in the SSH jail:

sudo fail2ban-client set sshd unbanip 192.168.1.100  

Step 10: Keep your server secure
Fail2Ban enhances server security, but regular updates and monitoring are critical. Check logs frequently, adjust settings as needed, and ensure your server software is up to date.

Installing and configuring Fail2Ban is a proactive way to safeguard your dedicated server from brute force attacks and unauthorized login attempts. For more tips or assistance, contact QuickServers.net to help you secure your server today!

Was this answer helpful? 0 Users Found This Useful (0 Votes)