Shell script – Logged in mail
Introduction
This script helps you receive an email alert whenever a user logs into your Linux server. It is useful for monitoring access, especially for root logins.
Prerequisites
- SSH access to the server
- Mail service configured on the server
- Basic knowledge of editing files using vi or any editor
Implementation
Step 1: Log in to the server
Step 2: Open .bash_profile
vi .bash_profile
Step 3: Add the below script
echo ‘ALERT – Root Shell Access (root@IP ADDRESS) on:’ date who | mail -s “Alert: Root Access from who | cut -d”(” -f2 | cut -d”)” -f1” <mailaccount>
Step 4: Save and exit
Step 5: Verify
- Log out and log in to the server
- You will receive an email notification
Note:
- Replace <mailaccount> with your email ID
- Replace IP ADDRESS with your server IP
Conclusion
By adding this script to .bash_profile, you can monitor login activities and receive instant alerts whenever a user accesses your server.
