Skip to content

Instantly share code, notes, and snippets.

@lonalore
Last active December 8, 2017 11:25
Show Gist options
  • Save lonalore/8704c1c590a01c456594acf88d3be8ea to your computer and use it in GitHub Desktop.
Save lonalore/8704c1c590a01c456594acf88d3be8ea to your computer and use it in GitHub Desktop.

1, Create /etc/ssh/login-notify.sh with the following contents:

#!/bin/sh

# Change these two lines:
sender="noreply@example.com"
recepient="notify-address@example.com"

if [ "$PAM_TYPE" != "close_session" ]; then
    host="`hostname`"
    subject="SSH Login: $PAM_USER from $PAM_RHOST on $host"
    # Message to send, e.g. the current environment variables.
    message="`env`"
    echo "$message" | mailx -r "$sender" -s "$subject" "$recepient"
fi

2, Make it executable with chmod +x login-notify.sh

3, Add the following line to /etc/pam.d/sshd:

session optional pam_exec.so seteuid /etc/ssh/login-notify.sh

Source: https://askubuntu.com/a/448602

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment