Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save santiagosony/8d25c53b7e2f95729f36fc3ddd1496a0 to your computer and use it in GitHub Desktop.

Select an option

Save santiagosony/8d25c53b7e2f95729f36fc3ddd1496a0 to your computer and use it in GitHub Desktop.
Linux - New SSH login notification by email
#!/bin/sh
# Script Location
# /usr/local/bin/sshd-login-notification.sh
# Make root owner, 700, +x
# Add the following line to: /etc/pam.d/sshd
# session optional pam_exec.so /usr/local/bin/sshd-login-notification.sh
# Your Email Information: Recipient (To:), Subject and Body
RECIPIENT="<COMPLETE>"
SENDER="<COMPLETE>"
SUBJECT="`hostname` ALERT | New SSH login detected"
BODY="
A SSH login was successful, so here are some information for security:
User: $PAM_USER
User IP Host: $PAM_RHOST
Service: $PAM_SERVICE
TTY: $PAM_TTY
Date: `date`
Server: `hostname`
"
if [ ${PAM_TYPE} = "open_session" ]; then
echo "To:${RECIPIENT}
Subject:${SUBJECT}
From:${SENDER}
${BODY}" | /usr/sbin/sendmail ${RECIPIENT}
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment