Created
March 14, 2026 19:48
-
-
Save santiagosony/8d25c53b7e2f95729f36fc3ddd1496a0 to your computer and use it in GitHub Desktop.
Linux - New SSH login notification by email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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