Skip to primary content
Skip to secondary content

Angel's Place

A little from everything

Angel's Place

Main menu

  • Home
  • About
  • Microsoft Azure Blog

Tag Archives: Send email on login

How do I set up an email alert when a ssh login is successful?

Posted on January 28, 2019 by Angel
Reply

Original: https://askubuntu.com/questions/179889/how-do-i-set-up-an-email-alert-when-a-ssh-login-is-successful

Since the sshrc method doesn’t work if the user has their own ~/.ssh/rc file, I’ll explain how to do this with pam_exec as @adosaiguas suggested. The good thing is that this can also be easily adapted to login types other than ssh (such as local logins or even all logins) by hooking into a different file in /etc/pam.d/.

First you need to be able to send mail from the command line. There are other questions about this. On a mail server it’s probably easiest to install mailx (which is probably already installed anyway).

Then you need an executable script file login-notify.sh (I put it in /etc/ssh/ for example) with the following content. You can change the variables to change the subject and content of the e-mail notification. Don’t forget to execute chmod +x login-notify.sh to make it executable.

#!/bin/sh

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

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

Once you have that, you can add the following line to /etc/pam.d/sshd:

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

For testing purposes, the module is included as optional, so that you can still log in if the execution fails. After you made sure that it works, you can change optional to required. Then login won’t be possible unless the execution of your hook script is successful (if that is what you want).

For those of you in need of an explanation of what PAM is and how it works, here is a very good one.

Just make sure you have UsePAM set to yes in your sshd_config

And this is how to use Pushover API for alerts instead of email:

#!/bin/sh

# This script monitors /var/log/auth.log for SSH logins
# and sends a notification via Pushover when one is
# detected.

# Need to wait a bit before actually starting this script
sleep 90 # sleeps for 90 seconds

# Pushover
PUSHOVER_TITLE="FreeNAS SSH Login" # This is the title for each message.
APITOKEN=******
USERKEY=******

# Log
LOG=/mnt/Tank/sshNotify.log
/bin/date > $LOG
echo "PID: $$" >> $LOG

# Priority:   -2 to generate no notification/alert
#             -1 to always send as a quiet notification
#              0 to use the default priority
#              1 to display as high-priority and bypass the user's quiet hours
#              2 to also require confirmation from the user.
#                Note: For priority 2, the retry and expire parameters
#                      must also be supplied.

# Monitoring loop

echo "Script running." >> $LOG
/usr/bin/tail -Fn0 /var/log/auth.log | \
while read line ; do
    echo "$line" | grep -q " Accepted publickey for "
    if [ $? = 0 ] ; then
        TS=$(date +%s)
        USER=$(echo "$line" | cut -d':' -f4 | cut -d' ' -f5)
        IP=$(echo "$line" | cut -d':' -f4 | cut -d' ' -f7)
        /usr/local/bin/curl https://api.pushover.net/1/messages.json -d "token=${APITOKEN}&user=${USERKEY}&title=${PUSHOVER_TITLE}&message=User: ${USER}   IP: ${IP}&timestamp=${TS}&priority=-1"
    fi
done

echo "Script finished, will now exit." >> $LOG
/bin/date >> $LOG
exit 0

If using this with FreeNAS run the script by creating an entry under Tasks –> Init/Shutdown Scripts. The fields are:

Type: Command
Command: /bin/sh /mnt/Tank/sshNotify.sh &
When: postinit
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to email a link to a friend (Opens in new window)
  • Click to print (Opens in new window)
  • More
  • Click to share on Reddit (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
Posted in Howto, Ubuntu | Tagged Send email on login, SSH | Leave a reply

Categories

  • Android (9)
  • Art (1)
  • Azure (39)
  • Car maintenance (3)
    • Subaru Impreza (2)
    • Volvo XC90 (1)
  • CodeProject (9)
  • Cooking (40)
  • Digital Photography (4)
  • Docker (11)
  • FreeNAS (19)
  • Gardening (4)
  • GIMP (3)
  • Hardware (11)
  • Howto (171)
  • Internet (17)
  • Learning (6)
  • Linode (7)
  • Music (4)
  • OpenMediaVault (2)
  • Sitecore (14)
  • Smart Home (3)
  • Software Development (62)
    • ASP.NET (9)
    • ASP.NET Core (11)
    • AWS (2)
    • Azure (6)
    • Git (3)
    • Sitecore (6)
    • SQL Server (5)
    • VMware (6)
  • Synology (6)
  • Travel (5)
  • Ubuntu (40)
    • Ubuntu on a stick (4)
  • Uncategorized (53)
  • WordPress (2)
  • YouTube (1)

Archives

Categories

  • Android (9)
  • Art (1)
  • Azure (39)
  • Car maintenance (3)
    • Subaru Impreza (2)
    • Volvo XC90 (1)
  • CodeProject (9)
  • Cooking (40)
  • Digital Photography (4)
  • Docker (11)
  • FreeNAS (19)
  • Gardening (4)
  • GIMP (3)
  • Hardware (11)
  • Howto (171)
  • Internet (17)
  • Learning (6)
  • Linode (7)
  • Music (4)
  • OpenMediaVault (2)
  • Sitecore (14)
  • Smart Home (3)
  • Software Development (62)
    • ASP.NET (9)
    • ASP.NET Core (11)
    • AWS (2)
    • Azure (6)
    • Git (3)
    • Sitecore (6)
    • SQL Server (5)
    • VMware (6)
  • Synology (6)
  • Travel (5)
  • Ubuntu (40)
    • Ubuntu on a stick (4)
  • Uncategorized (53)
  • WordPress (2)
  • YouTube (1)

Tags

  • .Net
  • .NET Core
  • asp.net core
  • Azure
  • Azure DevOps
  • Backup
  • container
  • Cooking
  • dd-wrt
  • docker
  • Entware
  • Fire TV
  • freenas
  • gimp
  • Git
  • Grafana
  • IIS
  • IoT
  • iptables
  • Kubernetes
  • Linode
  • linux
  • OpenVPN
  • Pi-hole
  • Plex
  • Portainer
  • Proxmox
  • Raspberry Pi
  • RDP
  • site-to-site VPN
  • sitecore
  • SQL Server
  • SSH
  • SSL
  • Strong Swan
  • Synology
  • Terraform
  • TrueNAS
  • Ubuntu
  • VMWare ESXi
  • vpn
  • WordPress
  • Youtube
  • ZFS
  • ZoneMinder

Archives

  • January 2023 (6)
  • December 2022 (1)
  • November 2022 (2)
  • October 2022 (2)
  • September 2022 (5)
  • August 2022 (2)
  • July 2022 (5)
  • May 2022 (4)
  • April 2022 (6)
  • March 2022 (3)
  • February 2022 (4)
  • January 2022 (16)
  • December 2021 (6)
  • November 2021 (20)
  • October 2021 (3)
  • September 2021 (4)
  • August 2021 (12)
  • July 2021 (5)
  • June 2021 (5)
  • April 2021 (2)
  • March 2021 (2)
  • February 2021 (1)
  • January 2021 (1)
  • December 2020 (12)
  • November 2020 (9)
  • October 2020 (5)
  • September 2020 (1)
  • August 2020 (7)
  • July 2020 (7)
  • June 2020 (5)
  • April 2020 (3)
  • February 2020 (3)
  • January 2020 (8)
  • December 2019 (4)
  • November 2019 (4)
  • October 2019 (5)
  • September 2019 (4)
  • August 2019 (5)
  • July 2019 (3)
  • June 2019 (1)
  • May 2019 (3)
  • April 2019 (2)
  • March 2019 (4)
  • February 2019 (4)
  • January 2019 (14)
  • December 2018 (7)
  • November 2018 (7)
  • October 2018 (10)
  • September 2018 (5)
  • August 2018 (2)
  • July 2018 (16)
  • June 2018 (3)
  • May 2018 (9)
  • April 2018 (2)
  • March 2018 (4)
  • February 2018 (12)
  • January 2018 (12)
  • December 2017 (9)
  • October 2017 (1)
  • May 2017 (2)
  • April 2017 (1)
  • March 2017 (2)
  • February 2017 (1)
  • January 2017 (4)
  • December 2016 (8)
  • November 2016 (11)
  • October 2016 (1)
  • January 2015 (1)
  • December 2014 (5)
  • November 2014 (3)
  • October 2014 (4)
  • September 2014 (2)
  • June 2014 (1)
  • March 2014 (1)
  • February 2014 (4)
  • November 2013 (2)
  • October 2013 (1)
  • September 2013 (1)
  • August 2013 (1)
  • October 2012 (1)
  • May 2012 (5)
  • January 2012 (1)
  • December 2011 (5)
  • May 2011 (1)
  • April 2011 (1)
  • November 2010 (4)
  • September 2010 (6)

RSS feed RSS - Posts

RSS feed RSS - Comments

Categories

  • Android (9)
  • Art (1)
  • ASP.NET (9)
  • ASP.NET Core (11)
  • AWS (2)
  • Azure (39)
  • Azure (6)
  • Car maintenance (2)
  • CodeProject (9)
  • Cooking (40)
  • Digital Photography (4)
  • Docker (11)
  • FreeNAS (19)
  • Gardening (4)
  • GIMP (3)
  • Git (3)
  • Hardware (11)
  • Howto (171)
  • Internet (17)
  • Learning (6)
  • Linode (7)
  • Music (4)
  • OpenMediaVault (2)
  • Sitecore (14)
  • Sitecore (6)
  • Smart Home (3)
  • Software Development (32)
  • SQL Server (5)
  • Subaru Impreza (2)
  • Synology (6)
  • Travel (5)
  • Ubuntu (38)
  • Ubuntu on a stick (4)
  • Uncategorized (53)
  • VMware (6)
  • Volvo XC90 (1)
  • WordPress (2)
  • YouTube (1)
January 2023
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« Dec    

Categories

  • Android
  • Art
  • ASP.NET
  • ASP.NET Core
  • AWS
  • Azure
  • Azure
  • Car maintenance
  • CodeProject
  • Cooking
  • Digital Photography
  • Docker
  • FreeNAS
  • Gardening
  • GIMP
  • Git
  • Hardware
  • Howto
  • Internet
  • Learning
  • Linode
  • Music
  • OpenMediaVault
  • Sitecore
  • Sitecore
  • Smart Home
  • Software Development
  • SQL Server
  • Subaru Impreza
  • Synology
  • Travel
  • Ubuntu
  • Ubuntu on a stick
  • Uncategorized
  • VMware
  • Volvo XC90
  • WordPress
  • YouTube

Categories

  • Android
  • Art
  • ASP.NET
  • ASP.NET Core
  • AWS
  • Azure
  • Azure
  • Car maintenance
  • CodeProject
  • Cooking
  • Digital Photography
  • Docker
  • FreeNAS
  • Gardening
  • GIMP
  • Git
  • Hardware
  • Howto
  • Internet
  • Learning
  • Linode
  • Music
  • OpenMediaVault
  • Sitecore
  • Sitecore
  • Smart Home
  • Software Development
  • SQL Server
  • Subaru Impreza
  • Synology
  • Travel
  • Ubuntu
  • Ubuntu on a stick
  • Uncategorized
  • VMware
  • Volvo XC90
  • WordPress
  • YouTube
Proudly powered by WordPress