Learn Enable SSH Login Notification on Linux

June 7, 2019

Table of Contents

Is your Linux server accessed by multiple users? If so, consider adding an SSH login notification whenever someone logs in.

https://www.itweb.services/tutorials/linux-guides/setup-exim-to-send-email-using-gmail-in-debian”>Exim with Gmail if your IT Web Services account is not verified to send mail.

Replace YOUR_EMAIL_ADDRESS with the email address that you want to receive login notifications.

CentOS

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

Append the following lines:

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

echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mail -s 'SSH Login Notification' YOUR_EMAIL_ADDRESS

Ubuntu/Debian

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

Append the following lines:

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

echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mail -s 'SSH Login Notification' YOUR_EMAIL_ADDRESS

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!