SSH is very secure protocol to access and administrate remotely your servers. as result the ssh daemon must exposed to internet. Any service exposed to internet it’s unfortunately exposed also to security threats.

Brute force attacks can be run against your machine trough ssh. in Ubuntu /var/log/auth.log will record any login attempt to the server. In case you found the alerts below,then it’s time to take some actions.

What I m trying to do

Block unauthorized SSH login using Fail2Ban.

Enviroment

Prerequisite

Basic Steps

  1. Install Fail2Ban
  2. Configure Fail2Ban
  3. Test it

1. Install Fail2Ban

sudo apt-get update
sudo apt-get install fail2ban

2. Configure Fail2Ban

[DEFAULT]
....
ignoreip = 127.0.0.1/8 192.168.0.1
[DEFAULT]
....
bantime = 127.0.0.1/8 192.168.0.1
[DEFAULT]
....
findtime = 300
maxretry = 3
[DEFAULT]
....
action = %(action_mwl)s
[DEFAULT]
....
destemail = admin@issamben.com
....
sendername = Machine Name - SSH BAN
....
mta = sendmail
[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 3

3. Test Fail2Ban

sudo service fail2ban reload

Hi, The jail ssh has been started successfully. Regards, Fail2Ban

Example:

Fail2Ban-SSH-BAN