How to configure VPS to an email alert when a SSH login is successful?

FerdieQO

Well-known member
Joined
Jul 15, 2016
Messages
213
Points
28
As tittle mentions, I want to receive emails when someone logins via my SSH? how to configure my VPS to do that?
 

steitieh

Well-known member
Joined
Apr 23, 2014
Messages
113
Points
18
Your VPS has many root users? And you want your VPS notify you whenever a user logged into the VPS via emails. Or simply, you would like to receive email notifications when VPS is logged into by someone via SSH. Ok let do these tips

1. On Centos VPS:

You use vim, nano or any other editing software to open the file .bash_profile, its address is /root/.bash_profile.

This file is a hidden file, located in your home folder

~/.bash_profile

(the ~ expands to your home directory. If your username is user, the result would be: /home/user/.bash_profile).

Open file .bash_profile

Code:
nano  /root/.bash_profile
Then you add content below and save.

Code:
IP="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
HOSTNAME=$(hostname)
NOW=$(date +"%e %b %Y, %a %r")
 
echo 'Someone has the '$IP' logged with the '$HOSTNAME' on '$NOW'.' | mail -s 'Email notification SSH' YOUR_EMAIL_ADDRESS
2. On VPS Ubuntu/Debian:

Edit file: .bashrc /root/.bashrc address. Add content below:

Code:
IP="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
HOSTNAME=$(hostname)
NOW=$(date +"%e %b %Y, %a %r")
 
echo 'Someone has the '$IP' logged with the '$HOSTNAME' on '$NOW'.' | mail -s 'Email notification SSH' YOUR_EMAIL_ADDRESS
You need to replace your email address with YOUR_EMAIL_ADDRESS which want to be notified.

So from now on, when someone logins to the VPS, you will receive an email notice from your VPS

Good luck!
 
Recommended Threads
Replies
14
Views
3,322
Replies
1
Views
1,221
Replies
3
Views
3,485

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top