$ sudo apt-get update
$ sudo apt install mailutils
$ sudo systemctl restart postfix
Put the following in /etc/profile
:
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" adityaypi@yahoo.com
fi
How the script works
/etc/profile
is executed at every login (for bash shell users). The if statement will only return true if the user has logged in via ssh, which in turn will cause the indented code block to be run.
Next, we then build the text of the message:
$(date)
will be replaced by the output of thedate
command${USER}
will be replaced by the user’s login name$(hostname -f)
will be replaced by the full hostname of the system being logged into
The second TEXT
line adds to the first, giving the IP address of the system this user is logging in from. Finally, the generated text is sent in an email to your address.
$ sudo /etc/init.d/ssh restart
$ sudo service ssh restart
$ sudo restart ssh
$ sudo systemctl restart ssh