I want to get notified if any person accesses my Debian server via ssh.

So I want to send an email whenever a user logs in on my server via ssh, so I added this line at the end of /etc/profile:

/usr/local/bin/shell-login.sh | mailx -s "SSH Login User $(whoami) on YOUR-HOSTNAME" your@email.org

/usr/local/bin/shell-login.sh contains:


echo "Login on $(hostname) at $(date +%Y-%m-%d +%H:%M)"
echo "User: "$(whoami)

This works too well: I get an email every minute now telling me that root is logging in, which seems to be caused by cron (see /var/log/auth.log)

How do I have to change this setup to send no emails on automated internal ssh-calls?

closed as unclear what you're asking by Anthon, Michael Homer, Networker, jimmij, Ramesh Feb 7 '15 at 6:51

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You can put a check in your bash script for external user by checking the output of w and do invert match for the external connection. your bash script should return fail and then do not execute the email alert. let me know if this make sense then I will propose detail answer – Raza Jan 26 '15 at 5:25
  • How do you make login in call this script? Do you included it in /etc/bash.bashrc? Something in /etc/ssh/sshd_config? Is someone/someprogram really ssh-ing into your machine every minute from the machine? – Anthon Jan 26 '15 at 7:06
  • What are the close-votes about? I edited my question to clarify the problem – rubo77 Jan 26 '15 at 10:36
  • Only some mysterious "external" access, or a number of specific ones? – 0xC0000022L Jan 26 '15 at 10:39
  • @0xC0000022L: I see, there seemed to be something missing at the end of my question. I hope now it is clearer – rubo77 Jan 26 '15 at 10:43
up vote 4 down vote accepted

To me the question is still unclear, so I try to answer the question contained in the first paragraph. How to log SSH logins?

I will also limit my answer to all *nix systems with PAM support. This is a relevant point, because you do not limit the scope of your question by giving a particular OS.

Okay, here's what I used in the past: sshrc. If you add a file named that in /etc/ssh (location may vary!), it will be executed by interactive (i.e. with shell) SSH connections.

Downside here is that you won't get informed about the stuff that is also relevant, such as SFTP (sftp-internal subsystem) connections.

However, we have an inroute here.

We can use PAM with pam_exec.so to our advantage and limit its effect to SSH by adding this to /etc/pam.d/sshd (for me it's the last non-comment line):

session    optional     pam_exec.so stdout /etc/your_email_script.sh

This will ensure that the script gets run as a privileged user (relevant if you prefer to call the sendmail binary to send off the mail) and that there is hardly anything the user can do to avoid this script being run. You can effectively limit access to that script to only root.

The part with optional you should adjust if needed. Relevant reading: man pam_exec, man pam.conf, man pam.d.

You may also want to play with how early on you want to execute your script.

What you see to miss is. You have so many other ways of locking down the server. For starters: don't allow passwords. Stick to key-only authentication. Make sure that people with only SFTP access do not have additional access:

Match group sftponly
        ChrootDirectory /home
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp
        PasswordAuthentication no

will let members of group sftponly only use SFTP and no port forwarding etc, and limit the scope to /home (file/folder permissions do the rest).

AllowGroups ssh-users

will only let members of a group ssh-users even log on via SSH. That is, you can limit SSH logon to a subset of your user base.

PermitRootLogin no

should be set and relevant users be made sudoers instead.

PasswordAuthentication no
PubkeyAuthentication yes

should ensure that password over which you have limited control cannot be used to log on.

AuthorizedKeysFile     /some/protected/folder/.ssh/authorized_keys

can ensure that users aren't allowed to manage their authorized_keys file, but requires you to do it on their behalf.

  • I tried a lot now with pam_exec.so but I always get notified that the user root logged in, instead of my username. I used the script in my question. I tried the option seteuid and quiet but still, always it sais it was root – rubo77 Jan 26 '15 at 11:39
  • @rubo77: Wait wait wait ... you did read the friendly manual of pam_exec, didn't you? Hint the following variables are available to the program run by pam_exec: PAM_RHOST, PAM_RUSER, PAM_SERVICE, PAM_TTY, PAM_USER and PAM_TYPE. – 0xC0000022L Jan 26 '15 at 11:43
  • So I can rewrite the script like this: unix.stackexchange.com/a/126573/20661 – rubo77 Jan 26 '15 at 11:57
  • @rubo77: that looks about right. – 0xC0000022L Jan 26 '15 at 12:00

You can do this by (carefully) editing /etc/pam.d/sshd and adding the pam_exec module into the stack. This module can be used to call an external program - such as your script - when someone has successfully started an ssh session.

Let me know if you need "how to" instructions and I'll update my answer to include them for a Debian-based system. (Other distributions have slightly different PAM stacks, so you would have to interpret my instructions rather than follow them blindly.)

Try this:


if [ ! $(whoami) == root ] ; then
echo "Login on $(hostname) at $(date +%Y-%m-%d +%H:%M)"
echo "User: "$(whoami)
  • 1
    As a workaround, this would work, but only, if you have root login disabled in your ssh config, otherwise, you would miss mails on a real root login – rubo77 Jan 26 '15 at 10:28
  • sorry, for that. I just want to help – DiogoSaraiva Jan 26 '15 at 10:30
  • It is a nice workaround, until i Find a solution to really track the real logins and send an email only then – rubo77 Jan 26 '15 at 10:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.