Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email on ssh login

Tags:

bash

email

ssh

I want to make sure everytime someone is login into my server I receive an email saying:

ALERT Shell Access on: Tue Jun 16 11:04:10 CDT 2009 user123 pts/0 2009-06-16 11:04

So I put this code:

echo 'ALERT Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" [email protected]

in the .bashrc.

Why I don't receive any emails?

Thank you for your help, J

like image 897
Tech4Wilco Avatar asked Sep 13 '11 16:09

Tech4Wilco


People also ask

How to create an email alert for SSH logins?

How to create an SSH login alert. Where EMAIL is the email address you want the alerts sent to. Save and close the file. Log out and log back into the system with SSH and you should eventually see an email alert to the destination email address of the login.


2 Answers

.bashrc is executed when bash is run as a interactive non-login shell, which is not the case when you ssh. Add the same code in .bash_profile which should be run when the shell is a login shell

Note: Many distributions source .bashrc from .bash_profile, I may be wrong and the issue may not be about .bashrc / .bash_profile

like image 184
Sorin Avatar answered Sep 21 '22 07:09

Sorin


Add into /etc/ssh/sshrc

ip=`echo $SSH_CONNECTION | cut -d " " -f 1`

logger -t ssh-wrapper $USER login from $ip
msg="User $USER just logged in from $ip"
echo $msg|mail -s $msg root
like image 21
Pipo Avatar answered Sep 19 '22 07:09

Pipo