How to Get Linux Server Sends Email Alert on Root Login

Another way to improve the security of your linux server is to enable server to automatically send a notification email to predefined email address everytime someone logs in as root to the host.

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 nano or vi by typing one of the following commands at command shell line:

[root@myserver ~]# nano .bash_profile
[root@myserver ~]# 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`"