How to achieve E-Mail Notification on root login?

Karl Vogel vogelke+unix at
Tue Feb 12 19:40:19 UTC 2013

>> Robert Huff <roberthuff at> writes:

R> The bigger question is how quickly do you need to know - instantly?
R> once an hour?  once a day?

>> On 12 Feb 2013 15:39:56 +0100, Frank Staals <frank at> said:

F> I don't think anything other than instantly makes sense. If it would be
F> a batch thing sent once an hour/day/<whatever> then an attacker could
F> simply prevent the mail being sent, and/or remove her entry from the
F> log.  Furthermore, one should realize that any setup would only be
F> guaranteed to report the first breach/login.

   Yup.  I can see two ways around this, and the first one is ugly.

   1.  Rename su and make it executable only by root, so you can't bypass
       the part that handles the email alert:

       # mv /usr/bin/su /usr/bin/sulocal
       # chmod 700 /usr/bin/sulocal

   2.  Create a script in a directory accessible only by root:

       # cat /root/bin/emailalert
       echo root login by `/usr/bin/id -un` | exec /path/to/sendmail -t
       exit 1

   3.  Replace /usr/bin/su with a small setuid C program to call the
       script and then run the real su, something like:

       main(int argc, char **argv, char **envp) {
           execve("/usr/bin/sulocal", argv, envp);

   The second (better) way is to have your logs immediately forwarded to
   another host specifically set up for intrusion detection, install a
   log-monitoring system there, and send the message from there.  This way,
   the original logs are more likely to be intact when you investigate.

Karl Vogel                      I don't speak for the USAF or my company

Some guy just yelled at me for texting and driving.  I told him to
get off my hood and mind his own business.

More information about the freebsd-questions mailing list