How to set up Fail2ban?
Fail2ban is a security tool that can be used to protect servers from malicious login attempts and other types of attacks. It works by monitoring log files and banishing IP addresses that show suspicious activity, such as repeated failed login attempts. Here is a tutorial on how to set up Fail2ban on an Ubuntu system:
- Install Fail2ban: First, you will need to install Fail2ban on your system. You can do this by running the following command in a terminal window:
sudo apt install fail2ban
- Configure Fail2ban: Fail2ban is configured using configuration files located in the
/etc/fail2bandirectory. The main configuration file is
/etc/fail2ban/fail2ban.conf, which contains global settings for Fail2ban. You can edit this file to customize the behavior of Fail2ban, such as the ban time, the log file location, and the email notifications.
- Create a jail configuration: A jail is a set of rules that specifies what Fail2ban should look for in the log files and how it should react when it finds a match. You can create a jail configuration by creating a new file in the
/etc/fail2ban/jail.ddirectory. For example, to create a jail for SSH attacks, you can create a file named
ssh.confwith the following contents:
[ssh] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 86400
This configuration will enable the SSH jail and set the ban time to one day (86400 seconds) for IP addresses that have more than 3 failed login attempts within a 10-minute period.
- Restart Fail2ban: Once you have configured Fail2ban, you will need to restart the service to apply the changes. You can do this by running the following command:
sudo systemctl restart fail2ban
- Test Fail2ban: To test Fail2ban, you can try to log in to your server using an incorrect password multiple times. Fail2ban should detect the failed login attempts and ban the IP address. You can view the banned IP addresses by running the