Bash Script to Capture Login Activity Logs

Introduction

Monitoring login activities on a server is essential for maintaining security and tracking user access. A Bash script for login details logs helps system administrators capture and review information such as user login time, IP address, and session details. This allows quick detection of unauthorized access and improves overall system auditing.

Prerequisites

Before creating or using a Bash script for login logs, ensure the following:

  • Access to a Linux server or system
  • Basic knowledge of Bash scripting
  • Required permissions to read system log files (e.g., /var/log/auth.log or /var/log/secure)
  • Proper user privileges (root or sudo access) to fetch login details

IMPLEMENTATION

STEP: 1

Create a file using vi ‘filename’ e.g., vi bash_logindetails

STEP: 2

Paste this script in the file, save it and exit.

#!/bin/bash
logfile=~/homework.txt
echo `date` > $logfile
echo `who` >> $logfile
echo `uptime` >> $logfile
exit

STEP: 3

Then change it to executable mode chmod +x eg: chmod +x bash_logindetails

STEP: 4

Then run the file using ./filename eg: ./bash_logindetails

That’s it your shell script has completed.

To check the output of your script just cat the file in the same directory.

[root@vps ~]# cat filename.txt
Sat May 28 20:40:00 MSD 2011
root pts/0 2011-05-28 20:17 (wing432)
20:40:00 up 4 days, 6:14, 1 user, load average: 0.00, 0.00, 0.00

Conclusion

Using a Bash script to monitor login details is an effective way to enhance server security and maintain audit records. It helps administrators quickly identify suspicious activities and take necessary actions. Regular monitoring and logging of login data ensure better control and protection of the system.

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 Reply

Scroll to Top