How to Enable SSH Login Email Alerts Notification
To enhance the security of the server, which opens to the Internet and possible worldwide hackers, it’s best to enable server to consequently send a warning email. To configure the automatic email alert notice to a default email address on every occurrence of root log on the server.
In this article, I will explain how to set up an email alert to all SSH login user.
Set SSH Root Login Email Alerts:
Add the following whole line at the bottom of the file (.bashrc). Make sure to replace “ServerName” with a hostname of your Server and change “your@yourdomain.com” with a your email address.
# vi /root/.bashrc
echo 'ALERT - Root Shell Access (ServerName) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com
Full Configuration file example :
# .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi echo 'ALERT - Root Shell Access (ServerName) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com
Set SSH All User Login Email Alerts:
Configure at alert from source global definitions (/etc/bashrc). This will enabled for root and normal users :
# vi /etc/bashrc
Add the following at the bottom of the files.
echo 'ALERT - Root Shell Access (ServerName) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com
Set SSH Normal User Login Email Alerts:
You can enable email alert for specify normal user also (e.g techoism ):
# vi /home/techoism/.bashrc
Add the following at the bottom of /home/techoism/.bashrc :
echo 'ALERT - Root Shell Access (ServerName) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com
Enjoy it!