Secure your Server - Send Slack Alert on SSH Login

In my line of work, besides architecting and building infrastructure, part of the scope includes building safeguards to ensure that systems that were setup are properly secured.

Here’s a quick trick for folks looking for an easy first-line defence that they can implement easily across their servers right now.

In order to quickly respond to a server compromise, it is a good idea to setup some form of IDS to alert you when a potential headache is about to happen. Simplest way to do that is to set up an alerting system to alert you or your team when a server has been accessed.


  • Prepare your Slack’s incoming webhook tokens

Lets Go

First, login to your server as root and open up /etc/pam.d/sshd

Add the following at the bottom of the file

session optional /etc/

Bonus: To find out more about what else pam_exec can do, checkout man pam_exec, man pam.conf

Don’t worry, I’ll explain it afterwards!

Now, still as root, create /etc/

$ vim /etc/


[ "$PAM_TYPE" = "open_session" ] || exit 0
curl -X POST --data-urlencode "payload={'channel': '#builds', 'text': \"[login-alert] Login attempt by $PAM_RHOST as $PAM_USER @ $(hostname)\"}" \

Remember to chmod the /etc/

$ chmod +x /etc/

Breaking it down

So what just happened?


Defining session optional /etc/, what we’re telling the server to do is to execute the file /etc/ whenever a pam login event happens. If you noticed, we’ve edited the file /etc/pam.d/sshd which describe the ssh login service. If we wanted to, we can also add a similar executable line whenever someone makes sudo for example. How? We just modify /etc/pam.d/sudo of course!

Moving on.

So now we need to create the actual script that will be triggered.

You might notice that I’ve defined a conditional to check for: $PAM_TYPE = "open_session". In this case, we want to ensure that only logins are tracked else, even ssh logouts trigger as an event and will fire off the script.

In our case, our script consists of a quick and dirty incoming slack webhook which will send a message to a channel that we defined. This way your team will be quickly informed at minute 0 of someone connecting to your servers.

If you want to also track for logouts, just remove the conditionals.

Simple enough!

And you’re done!

Slack SSH Alert