Snippets

Łukasz Wójcik Debian: email me every time someone logs in via SSH

You are viewing an old version of this snippet. View the current version.
Revised by aaa aaa 353cab0
  1. Add in /etc/pam.d/sshd:
session optional pam_exec.so seteuid /etc/ssh/login-notify.sh
  1. Add the file:
$ sudo touch /etc/ssh/login-notify.sh
$ chmod +x /etc/ssh/login-notify.sh
  1. Edit the content:
#!/bin/sh

# Change these two lines:
sender="ssh@server.com"
recepient="root@anotherserver.com"

if [ "$PAM_TYPE" != "close_session" ]; then
        host="`hostname`"
        subject="SSH Login: $PAM_USER from $PAM_RHOST on $host"
        message="`env`"
        echo "$message" | mailx -r "$sender" -s "$subject" "$recepient"
fi
  1. Done
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.