Skip to main content

Adding Notification to SSH Logon

What will this do

This will initiate a Gotify notification with the User Name and IP address of an SSH login.

Edit the sshd config file

First we must edit the fileĀ /etc/pam.d/sshd to add a line to execute during the login process.

sudo nano /etc/pam.d/sshd

At the end of the file add the following line of code to execute another script upon login

# at the end of the file
session optional pam_exec.so /usr/bin/gotify-ssh-login.sh

Add the Sauce

Create the file that is referenced in the above to execute the notification

sudo nano /usr/bin/gotify-ssh-login.sh

#!/bin/bash
PATH=/bin:/usr/bin
Token=ABYE8UmEU6knixq
Title="SSH Login $HOSTNAME"
Message="SSH Login: ${PAM_USER} from ${PAM_RHOST}"
gotifysrv="https://gotify.monkeyturf.net/message?token=$Token"

if [ "${PAM_TYPE}" = "open_session" ]; then
      curl "$gotifysrv" -F "title=$Title" -F "message=$Message" -F "priority=5"
fi

Make the file executable.

sudo chmod +x /usr/bin/gotify-ssh-login.sh