Get Linux Server Sends Email Alert on Root Login

This guide is to improve the security of the server, which is exposed to the Internet and possible to get hacked worldwide, it’s best to enable server to automatically send a notification email to predefined email address every time 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 vi by typing one of the following commands at command shell line: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`"[email protected]

    Replace [email protected] 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.


  1. Heya i’m for the first time here. I found this board and I find It truly useful & it helped
    me out a lot. I hope to give something back and help others like
    you aided me.

  2. I do not even know how I ended up here, but I thought this post was good.
    I don’t know who you are but definitely you’re going to a famous blogger if you
    aren’t already 😉 Cheers!

    Battle Islands Hack
  3. My partner and I stumbled over here from a different web address
    and thought I might check things out. I like what I see so
    now i am following you. Look forward to looking into
    your web page for a second time.

Leave a Comment