I would like my server to send a mail to me when someone connects remotely over ssh to my server.

who only gives me back the Username, TerminalID and Date. I cannot use only that, I need to check for the IP someone uses to connect to me. So the triggering part would be an external IP.

How can I achieve that?

EDIT: who -h gives back the IP adresses of the ssh sessions. Thanks to Archemar

  • 1
    who -u give you more usefull information (like IP). – Archemar Sep 15 '14 at 7:42
  • Ok, who -u gives back the IP's thats great. Do you also know who I can execute a script when a SSH Login happens? – Harrys Kavan Sep 15 '14 at 7:45
  • A generic solution using PAM is described in this blog entry. – jofel Sep 15 '14 at 8:49

You can add some shell scripting to /etc/bashrc or /etc/bash.bashrc maybe bepending on your linux distribution. Those are executed when user logs in remotely via SSH. Just test if there is $SSH_CLENT variable to distinguish the ssh login.

There will be other usefull variables for your needs, like:

SSH_CLIENT=' 57353 2217' ← ip address
SSH_CONNECTION=' 57353 2217'

EDIT: Of course, if the user is using GNU/bash. Other shells uses other files. Check them in related manuals.

HTH, Cheers

  • Thank you, I'll go through all this at the evening when I have some more time. – Harrys Kavan Sep 15 '14 at 11:56
  • It worked pretty well Scyld, I just have make some more tweaks and I'll have what I want. Thanks to you! – Harrys Kavan Sep 16 '14 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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