1, Create /etc/ssh/login-notify.sh
with the following contents:
#!/bin/sh
# Change these two lines:
sender="[email protected]"
recepient="[email protected]"
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