Fail2ban (fail2ban.service)

Install:

sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Config: /etc/fail2ban/jail.local

[DEFAULT]
bantime = 1h
bantime.increment = true     ; repeat offenders get longer bans
bantime.maxtime = 1w         ; ...up to 1 week
findtime = 1h
maxretry = 5
ignoreip = 127.0.0.1/8 ::1 <LAN_SUBNET>   ; never ban localhost / home LAN
 
[sshd]
enabled = true
port = <SSH_PORT>
logpath = /var/log/auth.log
backend = systemd            ; reads the journal (logpath is ignored with this backend)
 
[recidive]
enabled = true
bantime = 1w
findtime = 1d

What the jails do

  • sshd: 5 failed logins within 1 hour gets a 1-hour ban. The ban grows with each repeat offense, up to 1 week.
  • recidive: watches fail2ban’s own log. An IP that keeps getting banned within a day is banned for 1 week.

Commands

sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip <IP>
sudo systemctl restart fail2ban   # after editing jail.local

Related: 03 - SSH · Back: 00 - Overview