Technology and troubleshooting.

Friday, January 29, 2016

How to set up an email alert when a ssh login is successful


How to set up an email alert when a ssh login is successful


If your like  to know who is logging into your linux servers, hopefully this blog entry will help you. So the scenario goes someone has logged into your server through means not legal or ethical at some where and wants to do who knows what. It would be great to get a email alert automatically to each successful login attempts via SSH server alert through email to your phone via Email and wake up to take care of business.


Demonstrative Purpose I used Centos
Distributor ID: CentOS
Description:    CentOS release 6.7 (Final)
Release:        6.7
Codename:       Final


 The script below should help:

 Paste the below script in the .bash_profile at the end of the line

[root@techreno.blogspot.com ~]# vi .bash_profile



if [ -n "$SSH_CLIENT" ]; then
TEXT="$(date): ssh login to ${USER}@$(hostname)"
TEXT="$TEXT from $(echo $SSH_CLIENT|awk '{print $1}')"
echo $TEXT|mail -s "ALERT: Host Techreno.blogspot.com Accessed" Name@yourcompany.com
fi

Replace Name@yourcompany.com with the email address that you want to receive login notifications.


techreno.blogspot.com
.bash_profile



Result/Output

techreno.blogspot.com
 SSH Login Notification

 

I hope this information is useful for you. Please forgive any typos or incomplete sentences.

 

 

Share:

Contributors

Popular Posts