I need to be notified by email when my Ubuntu server is accessed via SSH.

If it is possible, how can I do it?


4 Answers 4


pam_script will run any program you want when a user logs in.

  • Just a note: if you're still on Lucid you'll have to compile pam_script yourself (it was included with Oneiric)
    – Joril
    Commented Apr 17, 2013 at 7:09

You should be able to do so with a rule in /etc/hosts.allow. Try something like:

sshd: ALL: (/usr/bin/echo "SSH connection from %h (%H)" | /usr/bin/mailx -s "SSH Alert" [email protected]) 

You can get more detail from a script run from /etc/profile.d, or included in /etc/profile. However, this will only work if the user logs in to an interactive session.

If you don't need immediate notification, the logcheck program can notify you hourly of any accesses in the last hour. You will need to add appropriate rules to the configuration.

EDIT: Ubuntu uses the incompatible hosts_options format to execute shell commands. The follow rule is what I implemented:

SSHD: ALL: spawn (/bin/echo "SSH connection to %H from %h[%a]" | \
     /usr/bin/mailx -s "SSH Alert" [email protected])

Notes: Backslash notation can be used to wrap lines as above. Substitution characters are documented in the hosts.allow man page.

  • I'm getting ssh_exchange_identification: Connection closed by remote host error after setting this. Commenting it out in /etc/hosts.allow makes it work again.
    – Mehdi
    Commented Aug 8, 2012 at 20:52
  • @MHK What happens if you run the commands in brackets at the command line?
    – BillThor
    Commented Aug 8, 2012 at 22:57
  • That sends a mail, I had to change /usr/bin/echo to /bin/echo though on my system. I've disabled root login and password based login via ssh, changed port, watching it with Fail2ban. Other than that, it's a typical Ubuntu 12.04 server edition.
    – Mehdi
    Commented Aug 9, 2012 at 9:11
  • You will need to change your command in /etc/hosts.allow accordingly.
    – BillThor
    Commented Aug 9, 2012 at 14:14
  • I did that before posting the error message here.
    – Mehdi
    Commented Aug 10, 2012 at 17:55

Your best bet is to set up a script to watch the log file.

I'm on my phone at the moment, but Check /var/log/access.log

  • I'm using swatch
    – Joril
    Commented Apr 17, 2013 at 7:10
  • would you care to complete your answer now that you have probably had access to your desktop environment?
    – tony gil
    Commented Jun 1, 2014 at 14:37

The log is on auth.log, you can do a

cat /var/log/auth.log | grep ssh

To actualy send the mail you could install SSMTP, edit it's config as follows:


[email protected] mailhub=smtp.gmail.com:465 rewriteDomain=gmail.com AuthUser=yourusername AuthPass=yourpassword FromLineOverride=YES UseTLS=YES

Create a text file with the message body as follows:

To: [email protected] From: [email protected] Subject: SSH warning or whatever


To add the content you could do:

tail /var/log/auth.log | grep ssh >> /tmp/mailcontents.txt

and then run

ssmtp [email protected] < /tmp/mailcontents.txt


Another OP said you might want a notification each time it happens, you coud do something LIKE this:

create an script with


tail /var/log/auth.log | grep ssh >> /tmp/alert&

while true; do
   change=$(inotifywait -e close_write,moved_to,create .)
   change=${change#./ * }
   if [ "$change" = "/tmp/alert" ]; then 
       tail -n 1 /tmp/alert >> /tmp/mailcontents.txt
       ssmtp [email protected] < mailcontents.txt; 

Mailcontents should include the addresses as stated before, the script code has not been checked to be valid, consider it pseudocode.

  • I think the OP wants to be notified when events occur, not create a custom report once.
    – raphink
    Commented Jun 20, 2012 at 22:53
  • well same principle, he'd just need to tail /var/log/auth.log permanently with the -f switch as in tail -f --follow=name /var/log/auth.log >> /tmp/alert.log, then he can use inotifywait to track changes in the alert file.
    – Radius
    Commented Jun 21, 2012 at 0:37
  • I failed to use ssmtp. It can connect to gmail server but said the authentication fails while using the same email & password I can log in. Do you have any idea why? The error is ssmtp: Authorization failed (535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 nh8sm37308607pbc.60)
    – Nam G VU
    Commented Jun 22, 2012 at 1:40
  • Did you try this? google.com/accounts/DisplayUnlockCaptcha
    – Radius
    Commented Jun 22, 2012 at 3:38
  • @Andres Yes I did. But still the authentication still fails
    – Nam G VU
    Commented Jun 22, 2012 at 4:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .