Get Root User SSH Login Email Alerts

1.Create a file in /etc directory and write

########### By vijay To send Mail when root user logs in ###########

currentlogin=`last -i| grep -i still | sort -r | head -n 1`
set $currentlogin

echo -e "ALERT -$1 Access To `hostname`(`hostname -i`) on: `date` From $3 \n\n`last -i | grep -i still | sort -r ` " | mail -r "" -s "Alert: $1 Access from $3 To `hostname`(`hostname -i`) "

2.Now  call this file in /root/.bash_profile by adding below lines in /root/.bash_profile file

vim /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
 . ~/.bashrc

# User specific environment and startup programs


export PATH


save the file and from now every time root users login you will get mail in below format

with subject

Alert: root Access from To

and with mail body shown below

ALERT -root Access To on: Sat Oct 29 09:53:57 IST 2016 From 

root     pts/0    Sat Oct 29 09:53   still logged in


This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s