LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How many times a user logged in (https://www.linuxquestions.org/questions/linux-newbie-8/how-many-times-a-user-logged-in-695767/)

mmahulo 01-08-2009 01:22 AM

How many times a user logged in
 
Hi all! Happy new year!

I want to write a script that can count how many times a user has logged in.
The following is what I tried and seemengly I miss something somewhere:
times=$(who | grep $* | wc -l)
echo "$* has logged in $times times"


Please help.

Didier Spaier 01-08-2009 01:31 AM

"who" does not give that kind of historical information. See "man who" or "info coreutils 'who invocation'".

divyashree 01-08-2009 01:36 AM

just try the command "last"

ZAMO 01-08-2009 02:25 AM

Yesterday I checked with my box and "last" failed to report my brothers login .
This is how I verified his login.


Code:

grep LOGIN /var/log/messages |grep username | tail -1
You can count them with

Code:

grep LOGIN /var/log/messages |grep username | wc -l

btmiller 01-08-2009 02:56 AM

The last command reads data from /var/log/wtmp by default, and most distros set things up so this file is rotated weekly or monthly (or when it gets big). If you want long term historical data, you'll have to modify your system not to do this (usually by modifying logrotate.conf), or by just backing up the wtmp file. Automatically backing up log files is not a bad idea for security auditing anyhow, as an attacker who manages to gain root access can falsify log entries.

mmahulo 01-08-2009 08:25 AM

I tried : grep LOGIN /var/log/messages |grep username | tail -1

but it seems like it does not apply to SuSE Linux as I am using one. Or else, it's maybe because I don't have root access. Another suggestion please...

john test 01-08-2009 11:06 AM

Did you try
Code:

sudo grep LOGIN /var/log/messages |grep username | tail -1
?


All times are GMT -5. The time now is 04:21 AM.