ac 命令可以提供非常有用的摘要,说明用户登录 Linux 系统所花费的时间。它从 wtmp 文件中获取数据。
虽然不是一个众所周知的 Linux 命令,但ac可以提供非常有用的用户登录时间统计信息。以最简单的形式,它会显示用户在wtmp文件涵盖的时间段内在系统上花费了多少时间。您只需键入“ac”即可获得显示所有用户的总体登录时间的数字。
$ ac total 8360.60
上图表示用户在系统上的总使用时长为 8,360.6 小时。使用who命令查看wtmp文件,我们可以看到保存的登录从 6 月 6 日开始——比 6 个月早一点。
$ who /var/log/wtmp | head -2 shs tty2 2022-06-06 16:00 (tty2) shs pts/1 2022-06-06 16:23 (192.168.0.12)
要按用户查看时间,请添加-p(人员)参数。
$ ac -p popeye 1081.10 nemo 2301.70 shark 801.00 shs 4176.80 total 8360.6
要查看每天的登录总数,您可以使用ac -d(天)命令。如果您只想查看一个月的登录时间,请添加grep命令。
$ ac -d | grep Oct | head -11 Oct 1 total 46.21 Oct 2 total 55.09 Oct 3 total 34.50 Oct 4 total 69.48 Oct 5 total 50.73 Oct 6 total 44.85 Oct 7 total 25.05 Oct 8 total 74.49 Oct 9 total 44.88 Oct 10 total 34.28 Oct 11 total 38.36
将ac -d命令的输出通过管道传输到head命令是查看wtmp文件回溯多远的另一种方法。
$ ac -d | head -1 Jun 6 total 82.92
您还可以将ac输出通过管道传输到tail命令以查看最近几天。这提供了当月的当天和早些时候的日期。
$ ac -d | tail -10 Nov 1 total 121.79 Nov 2 total 24.86 Nov 3 total 25.75 Nov 4 total 24.63 Nov 5 total 25.03 Nov 7 total 52.97 Nov 8 total 26.86 Nov 9 total 27.66 Nov 10 total 24.56 Today total 14.91
如果您将来自其他时间段或系统的wtmp数据保存在文件中,您可以像这样对该文件运行ac命令:
$ ac -f /var/log/Jupiter total 5434.7
ac -h (帮助)命令将提供该命令的许多选项的摘要。
$ ac -h Usage: ac [OPTION] ... OPTIONS: -d, --daily-totals Print totals for each day -p, --individual-totals Print time totals for each user -f, --file Read from --complain Print errors for whatever problem --reboots Count the time between login and reboot --supplants Count the time between logins on the terminal --timewarps Count the time between login and time warp --compatibility Shortcut for --reboots --supplants --timewarps -a, --all-days Do not skip days without login activity --tw-leniency Set the time warp leniency in seconds --tw-suspicious Set the time warp suspicious in seconds --print-year Print year when displaying dates --print-zeros Don't suppress zeros in category totals --debug Print verbose internal information -V, --version Show version and exit -h, --help Show help and exit
系统默认的登录记账文件为/var/log/wtmp。虽然ac命令的摘要通常更有用,但如果您认为需要,您可以查看该文件中的每个条目。使用who /var/log/wtmp命令,如下所示。将其输出通过管道传输到more命令将使您可以慢慢浏览细节并了解特定系统的使用量和使用频率。
$ who /var/log/wtmp | more shs pts/1 2022-06-07 06:40 (192.168.0.6) shs pts/2 2022-06-07 08:26 (192.168.0.12) shark pts/3 2022-06-07 09:00 (192.168.0.12) nemo pts/2 2022-06-07 10:09 (192.168.0.11) nemo pts/2 2022-06-07 11:05 (192.168.0.11) nemo pts/2 2022-06-07 12:12 (192.168.0.6) shs pts/2 2022-06-07 13:50 (192.168.0.12) nemo pts/2 2022-06-07 14:15 (192.168.0.11) shs pts/1 2022-06-06 16:23 (192.168.0.12) shs tty2 2022-06-06 16:00 (tty2) shs pts/1 2022-06-06 17:32 (192.168.0.12) shs tty2 2022-06-06 17:17 (tty2) shs tty2 2022-06-06 17:21 (tty2) shs pts/1 2022-06-06 18:35 (192.168.0.6) shark pts/2 2022-06-07 18:26 (192.168.0.6)
总结
ac命令使存储在wtmp文件中的信息更易于访问。如果您想知道用户登录系统所花费的时间或登录频率,这正是您所需要的。ac命令可能代表“会计”,允许您以几种非常有用的方式查看wtmp文件中的数据。
本文由 @小喵 发布于弱电智能网 。
题图来自Unsplash,基于CC0协议
内容观点仅代表作者本人,弱电智能网平台仅提供信息存储空间服务。
如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
文章名称:《Linux上使用 ac 命令查看用户登录时间》
文章链接:https://www.ruodian360.com/tech/sytemops/37443.html
添加微信ydian188免费入群,记得备注“弱电智能网”。