Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-08-2009, 02:22 AM   #1
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
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.
Old 01-08-2009, 02:31 AM   #2
Didier Spaier
LQ Addict
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64- on Lenovo Thinkpad W520
Posts: 9,215

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

Last edited by Didier Spaier; 01-08-2009 at 02:32 AM. Reason: Typo
Old 01-08-2009, 02:36 AM   #3
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Posts: 1,373

Rep: Reputation: 135Reputation: 135
just try the command "last"
Old 01-08-2009, 03:25 AM   #4
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

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

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

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

Last edited by ZAMO; 01-08-2009 at 03:29 AM.
Old 01-08-2009, 03:56 AM   #5
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,289

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
Old 01-08-2009, 09:25 AM   #6
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
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...
Old 01-08-2009, 12:06 PM   #7
john test
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
Did you try
 sudo grep LOGIN /var/log/messages |grep username | tail -1


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Gnome: Auto mounting external devices as first user logged in, not current user yitzle Linux - Software 4 04-01-2008 10:43 AM
What is the command for finding the last 3 times the user had logged into the system? Linux_interest Linux - Newbie 3 08-27-2004 06:02 AM
What is the command for finding the last 3 times the user had logged into the system? Linux_interest Linux - General 1 08-27-2004 04:14 AM
iptables and limiting the number of times an event gets logged drexel Linux - Security 3 02-09-2004 09:59 AM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 09:34 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration