install fail2ban

fail2ban is a Python script that help you to protected against brute force

Install : apt-get install fail2ban

some config … before all you should create a copy from fail2ban config file
cd /etc/fail2ban
cp -r jail.conf jail.local
now we can change the jail.local

for sending email set destemail as your email .
change action to action = %(action_mwl)s
add below code at the end of file

[ssh]
enabled = true
port = ssh,some_port
filter = sshd
logpath = /var/log/auth.log
maxretry = 2
bantime = 10m

After that restart fail2ban service
service fail2ban stop
service fail2ban start

now you should check the inbox or span folder for an email from fail2ban.also, you can check the banned ip by
fail2ban-client status sshd
for more detail you can check this article

Linux send an email after login

It is a security concept that you know when a user (almost root user) loge in.

For this do like below

Add a shell script to send email

cd etc/ssh/

nano login-notify.sh and add below code

You should change sender and recepient

After that we should change file permission
chmod a+w login-notify.sh
and for testing sh login-notify.sh
Please check your email (inbox and spam folder).

nano /etc/pam.d/sshd And add this line at the end of file (use Alt and / to go to the end)

session optional pam_exec.so seteuid /path/to/login-notify.sh

All thing is ready but be careful, Please use another terminal to connect to your server and do not close your current connection.