How to send root SSH login notification on Debian

You probably have thought about an additional step of security, namely the ability to send notifications when you log in to your root account.
This point will be considered in this article.

 

The simplest answer to sending one-line messages via gmail is to use ssmtp.

Firstly, login as root:

su - root

Then, install ssmtp with the following commands:

apt-get update
apt-get install ssmtp

Adjust ssmtp.conf configuration file:

nano /etc/ssmtp/ssmtp.conf

/etc/ssmtp/ssmtp.conf
root=postmaster
FromLineOverride=YES
mailhub=smtp.gmail.com:465
AuthUser=testEmailFrom@gmail.com
AuthPass=testpass
UseTLS=YES

Open the file ~/.bashrc in a text editor.

nano ~/.bashrc

And append the following lines:

IP="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
HOSTNAME=$(hostname)
NOW=$(date +"%e %b %Y, %a %r")

echo -e 'Subject: SSH Login Notification\n\nSomeone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | ssmtp testEmailTo@gmail.com

 

 

We will be happy to hear your thoughts

Leave a reply