[Linux] How to Send E-MAIL (or SMS) Whenever a User Logs In

In the past, I’ve written about some of the con artist masquerading as consultants, which I’ve run into during my travels as a technical mercenary.

At one gig, a younger and inexperienced team lead was conflicted about canning a developer that wasn’t even showing up for work, but who claimed to be working remotely.

Of course, I checked the logs and he never logged in.

The team lead wanted more data, so I suggested that whenever the developer logs in, the lead would get an email.

“You can do that?”  the lead asked. 

Easy.  The solution is to add a few lines to the shell init script in the user’s home directory.  In a few minutes, it was done.

This is also a nice way to shoot yourself an SMS message via your cell phone companies email-SMS gateway when someone logs into one of your cloud instances.  It will give you an immediate notification if someone compromises a system.

In any event, the solution is extremely easy.

Put something similar to the following in the user’s .profile (csh):

mail user@yourdomain.com << EOF
From: Linux System
To: youremail@address.com
subject: user login
user $LOGNAME has logged into `hostname`