Email alert for SSH logins

It is good idea to get notified when someone logs into your Linux server.

First be sure that your server is able to send email (at least Postfix + valid ptr)

Open /etc/profile and at the bottom add the following:

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 $(hostname -f)" EMAIL@DOMAIN.TLD
fi

Replace EMAIL@DOMAIN.TLD with your email

Save file and logout/login again. Check your inbox

Leave a Reply

Your email address will not be published. Required fields are marked *