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.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top