This is a way to get email notifications when logging in and out via SSH on a Linux based system.
Email notificaitons are sent via mail
that are gpg encrypted.
This has been tested on a RaspberryPi and an Ubuntu system.
Download the Script
wget https://raw.githubusercontent.com/vanderblugen/SSH-Email-Notifications/main/notify-ssh-login.sh
nano notify-ssh-login.sh
Change recipient_email@address
to the appropriate email address.
Replace
cat $THISFILENAME | gpg -ear "recipient_email@address" | mail -a "Subject: Alert - Remote SSH $LOGWHAT notification" -a "X-Custom-Header: yes" "recipient_email@address.com"
With
cat $THISFILENAME | mail -a "Subject: Alert - Remote SSH $LOGWHAT notification" -a "X-Custom-Header: yes" "recipient_email@address.com"
Press <Ctrl+X>
and then <Enter>
when finished to exit
sudo chown root:root notify-ssh-login.sh
sudo chmod a=rx notify-ssh-login.sh
sudo mv notify-ssh-login.sh /etc
sudo nano /etc/pam.d/sshd
Add in the following lines to the end of the file
# Notify on successful log in / log out.
session optional pam_exec.so /etc/notify-ssh-login.sh
Press <Ctrl+X>
and then <Enter>
when finished to exit
Log out of the system and back in to verify functionality.
An email should be sent after logging in and out of the system via SSH now.