bash 获取用户登录 UNIX 的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25542669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:15:15  来源:igfitidea点击:

Getting number of times user logged in UNIX

bashshellunixksh

提问by pulse

Is there a way to know that how many times an user logged into the system( UNIX), with the help of whoor some other method?

有没有办法知道用户在who或其他方法的帮助下登录系统(UNIX)多少次?

采纳答案by NoobEditor

If you want countof how many times a user logged in,then its not possible through command-lineIMO....specifically because there is sudooption to pretend other user which can fail your logic - if you have any

如果你想要count一个用户登录多少次,那么它不可能通过command-lineIMO ......特别是因为可以sudo选择假装其他用户可能会失败你的逻辑 -如果你有任何

Also, how many timesneeds to have a time bracket (from whentill when)..so you need to have an upper time bracket as well from which you want to track down the count.All this would be a mess ideally just to maintain a count

此外,how many times需要有一个时间段(from when直到when)..所以你还需要有一个时间段上限,你想从中追踪计数。所有这一切在理想情况下都是一团糟,只是为了维持一个count

If i have to keep track of it, my best guess would be to have a scriptand maintain a db-tablekeeping the countof userswho are logging-in!

如果我要跟踪它,我最好的猜测是有一个script并保持db-table保持countusers谁是登录,在!

回答by sukhi

you may refer : Last command of unix

你可以参考: unix 的最后一条命令

and then you can manipulate the output to count or whatever you waana do.

然后你可以操纵输出来计数或你做的任何事情。

回答by Igor Chubin

You can use last:

您可以使用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 users' logins from the wtmp-database (/var/log/wtmp). That means, that only that entries that are in this database are displayed. You must bear in mind, that tt is possible, that if a user was logged in for a long time ago, there is no information about this login in it (because of a rotation).

它从wtmp-database ( /var/log/wtmp)获取有关用户登录的信息。这意味着,仅显示此数据库中的条目。您必须记住,tt 是可能的,如果用户很久以前登录,则其中没有有关此登录的信息(由于轮换)。

Also, I must note, that the notion of "how many times user is logged in" is a bit vague. User can have running processes, but not be logged in (or at least not registered in wtmp), user can use suand so on.

另外,我必须指出,“用户登录了多少次”的概念有点含糊。用户可以有正在运行的进程,但不能登录(或至少没有注册wtmp),用户可以使用su等等。

Using this way you can count number of open terminal sessions of a user:

使用这种方式,您可以计算用户打开的终端会话数:

$ ps aux | grep ^igor | fgrep S+ | wc -l
12

回答by Walter A

You can add logic to /etc/profile. When you append a line to a logfile, do not forget housekeeping. You might want to make a logfile-per-day and delete files older than 10 days.

您可以向 /etc/profile 添加逻辑。当您将一行附加到日志文件时,不要忘记整理。您可能希望每天创建一个日志文件并删除超过 10 天的文件。