How to Get Linux Server Sends Email Alert on Root Login

To improve the security of the server, especially web server which exposes to the Internet and possible worldwide hackers, it’s best to enable server to automatically send a notification email to predefined email address everytime someone logs in as root to the host. To configure the automatic email alert notification to a default email address on each incident of root log on on the server, use the following guide.

  1. Login to the server via SSH using as root ID.
  2. Ensure that you’re at home directory of root. The open up the .bash_profile for editing using pico or vi by typing one of the following commands at command shell line:

    pico .bash_profile
    vi .bash_profile

  3. Scroll down to the end of the file and add the following line:


    echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" user@example.com

    Replace user@email.com with the actual email account address that you want to the root access alert notification been sent to. Note that you can change the text contains in the email alert too. The text starting with first ALERT is written as email body, and you can add in other info such as host name or change the wordings. The second Alert is the email title which you can change to your own too.

Now logout and login again as root, you should receive an email alert at your inbox. The security trick should works on most popular flavor of Linux such as RedHat, CentOS, Ubuntu, FreeBSD and etc.

6 Responses to “How to Get Linux Server Sends Email Alert on Root Login”

  1. Chuck
    December 22nd, 2009 01:11
    6

    Also, it should be known that if anyone was attempting to get root access to your system, the “bash” shell probably wouldnt be loaded. They would probably go for the lower of “sh”.

  2. Chuck
    December 22nd, 2009 01:10
    5

    Hi,

    Before finding this article, I had already started to try to implement this myself. I did basic security testing to it and I found that you can very very easily bypass this by executing the “sh” command which bypasses the “bash” command to load the shell and therefore, .bash_profile and .bashrc both get bypassed.

    Use this at your own risk and don’t assume its a guarantee. Another way to possible implement such feature is to rewrite the code for openSSH and implement it directly into the code.

  3. dean
    November 21st, 2008 03:41
    4

    Great stuff,

    Is there a way to have the IP of the person who logs in show up?

  4. Emilio
    November 1st, 2008 03:06
    3

    This works for root login or su – from a user login.
    How can I implement it that will work also for “su” only? Right now since the path will change with su instead of su – it will not send an email if someone is not using the “-” after the su. Thanks.

  5. Jon
    May 3rd, 2008 05:43
    2

    ‘who -m’ makes the output much cleaner when using this on a system that has multiple users logged on…

    -m reports only hostname and user associated with stdin terminal

  6. Linux - wysyłanie maila o logowaniu na konto roota « Barni.LOG
    August 12th, 2007 03:10
    1

    [...] Linux – wysyłanie maila o logowaniu na konto roota Bardzo użyteczny skrypcik [...]

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to My Digital Life Comments RSS feed to receive notification of latest comments posted.

New Articles

Incoming Search Terms for the Article

linux email alerts - linux mail root - linux email login - linux send email on login - linux root email - linux mail login - linux root email address - linux alerts - linux mail bei login - linux send emails when users login - linux login notification - linux email after login - login notification linux - email on root login - linux email when user logs in - mail alert linux - root login notification - linux mail on login - linux email log - root email address - change root email - linux set root email - linux send email login - change root email address - linux email at login - linux alert - linux email alert - change root email address linux - root mail linux - linux root mail - send email at login linux - root login alert - root login email alert - linux send email when user logs in - get server alerts via email linux - login notifier + linux - login as root send email - get email everytime server access by root - linux email notification - get notified when someone logs in via ssh - linux mail change mail from address - linux send mail notification - root login notify - change linux root email - linux notify when user logs in - linux email when login - linux mail server - linux mail alert - linux how to change root email - how to send mail to root in linux -