Installing Fail2Ban
Fail2Ban monitors your log files and automatically bans IP addresses that show malicious behavior, such as repeated failed login attempts.
Install Fail2Ban
apt install fail2ban -y
Create a Local Configuration File
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Configure SSH Protection
Edit /etc/fail2ban/jail.local and add or update the [sshd] section:
[sshd]
enabled = true
port = ssh
maxretry = 5
findtime = 300
bantime = 3600
This configuration bans any IP that fails SSH login 5 times within 5 minutes, for 1 hour.
Start and Enable Fail2Ban
systemctl enable fail2ban
systemctl start fail2ban
Check Status
fail2ban-client status
fail2ban-client status sshd
Manually Unban an IP
fail2ban-client set sshd unbanip 203.0.113.100
View the Fail2Ban Log
tail -f /var/log/fail2ban.log