Skip to content

Instantly share code, notes, and snippets.

@dunderrrrrr
Created February 21, 2020 13:40
Show Gist options
  • Save dunderrrrrr/433a392a7086e08f8370668ed2718f4d to your computer and use it in GitHub Desktop.
Save dunderrrrrr/433a392a7086e08f8370668ed2718f4d to your computer and use it in GitHub Desktop.
Receive a notification every time a SSH-authentication is successful.

Receive a notification every time a SSH-authentication is successful.

First, install ssmtp.

Create the notify-script.

ssh-notify.sh

#!/bin/sh

# Change these two lines:
sender="[email protected]"
recepient="[email protected]"

if [ "$PAM_TYPE" != "close_session" ]; then
    host="`hostname`"
    subject="New SSH login on $host"
    echo "Subject: $subject\n\nUser: $PAM_USER\nFrom: $PAM_RHOST" | sendmail -v $recepient
fi

Enable script by adding the following line to /etc/pam.d/sshd.

session optional pam_exec.so seteuid /path/to/ssh-notify.sh

Restart ssh service.

$ sudo service ssh restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment