Tuesday, 7 July 2015

How to Set SSH Login Email Alerts in CentOS/Redhat Enterprise Linux/Ubuntu/ Debian/Linux Mint /Fedora

On Debian/Ubuntu/Linux Mint
# apt-get install mailx

On RHEL/CentOS/Fedora
# yum install mailx

Set SSH Root Login Email Alerts
# cd /root
# vi .bashrc

Add the following whole line at the bottom of the file. Make sure to replace “ServerName” with a hostname of your Server and change “your@yourdomain.com” with a your email address.

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

Save and close the file and logout and log back in. Once you login via SSH, a .bashrc file by default executed and sends you an email address of the root login alert.

Set SSH Normal User Login Email Alerts
Login as normal user (amir) and go to user’s home directory by typing cd /home/amir/ command.

# cd /home/amir

Next, open .bashrc file and add the following line at end of the file. Make sure to replace values as shown above.

#vim .bashrc

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

Save and close the file and logout and login again. Once you login back again, a .bashrc file executed and sends you an email address of the user login alert.



No comments:

Post a Comment