Install and configuration Fail2ban on Ubuntu servers 22.04 20.04 18.04 and 16.04 ...

Frst ubdate pakage and instal fail2ban ...

sudo apt update
sudo apt upgrade
sudo apt install fail2ban

To enable fail2ban run command ...

sudo systemctl enable --now fail2ban

Copy config file ...

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Edit config file

sudo nano /etc/fail2ban/jail.local

and add ...

ignoreip = 127.0.0.1/8 192.168.1.0/24
action = %(action_mw)s
destemail = This email address is being protected from spambots. You need JavaScript enabled to view it.
sender = This email address is being protected from spambots. You need JavaScript enabled to view it.
bantime  = 1d
findtime  = 10m
maxretry = 5

For the UFW firewall you need to change the banaction option to ufw as below.

banaction = ufw

Enable sshd edit ...

[sshd]

enabled   = true
maxretry  = 3
findtime  = 1d
bantime   = 1w

port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

Restart fail2ban with comand ...

sudo systemctl restart fail2ban

Status fail2ban with comand ...

sudo systemctl status fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd

Ban and unban command ...

sudo fail2ban-client set sshd banip ipadrese
sudo fail2ban-client set sshd unbanip ipadrese

...