Categories

  • articles

Tags

  • linux

This quick tutorial will show you how to get email alerts when any user logs into your Linux box.

Sending emails from command line

This will allow you to send emails from command line.

Install ssmtp

sudo apt-get install ssmtp mailutils

Edit the config file to set your usr/pwd/server/etc

sudo nano /etc/ssmtp/ssmtp.conf

Example for how gmail setup would be

root=username@gmail.com
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password
FromLineOverride=YES
UseTLS=YES

Test that everythings is working. This should send email to the provided email and a body of test

mail -s "Subject here" youremailhere@gmail.com <<< test

Setup the auto send

There are a few places you can add the command below to send a mail on user login. I just use /etc/profile. If you have better location please leave comment.

sudo nano /etc/profile

Add the following to bottom of file

if [ -n "$SSH_CLIENT" ]; then
    TEXT="$(date): ssh login to ${USER}@$(hostname -f)"
    TEXT="$TEXT from $(echo $SSH_CLIENT|awk '{print $1}')"
    echo $TEXT|mail -s "SSH login to $(hostname -f)" you@your.domain
fi

Bonus

If you have crontab jobs they will now be sending emails. Add the following line to the crontab

Edit crontab

crontab -e

Add to the top of the file

MAILTO=youremailhere@gmail.com