Fail2Ban is a log-parsing security tool that helps protect your server against brute-force attacks by banning malicious IPs dynamically.
sudo apt update && sudo apt upgrade -y
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
sudo systemctl status fail2ban
/etc/fail2ban/jail.conf/etc/fail2ban/jail.localImportant: Never edit jail.conf directly. Create a local override:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Open jail.local:
sudo nano /etc/fail2ban/jail.local
Example:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5
ignoreip = 127.0.0.1/8
Make sure the SSH jail is enabled:
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
sudo systemctl restart fail2ban
Check Status:
sudo fail2ban-client status
sudo fail2ban-client status sshd
Unban IP:
sudo fail2ban-client set sshd unbanip <IP_ADDRESS>
Restart/Stop/Start:
sudo systemctl restart fail2ban
sudo systemctl stop fail2ban
sudo systemctl start fail2ban
jail.local before editing./var/log/fail2ban.log.End of Guide