Getting the number of UNIX login attempts
If you want count
how many times a user is logged in then it cannot be done with command-line
IMO .... especially because there is an option sudo
to pretend to be another user, which can break your logic - if you have
Also, how many times
must have a timeline ( from when
before when
) .. so you need to have an upper timeline from which you want to track the account. It would all be a mess perfect to maintaincount
If I need to keep track of it, I would rather have script
and maintain db-table
keeping track of count
users
that register!
source to share
you can refer to: Latest unix command
and then you can manipulate the result for counting or whatever you do.
source to share
You can use last
:
last | grep ^username| grep 'logged in' | wc -l
For example:
$ last | grep ^igor | grep 'still logged'
igor pts/9 astaro Thu Aug 28 09:55 still logged in
It takes information about user logins from wtmp
-database ( /var/log/wtmp
). This means that only those records that are in this database are displayed. You should keep in mind that tt it is possible that if a user has been logged in for a long time, there is no information about that login in it (due to the turn).
Also, I should note that the concept of "how many times a user has logged in" is a bit vague. The user can have processes running but not logged in (or at least not logged into wtmp
), the user can use su
, etc.
Using this method, you can count the number of open user terminal sessions:
$ ps aux | grep ^igor | fgrep S+ | wc -l
12
source to share