你可以通过使用 -d参数,按日期排序显示所有用户的连接时间,操作如下:
$ ac -d
示例输出:
May 11 total 4.29May 13 total 3.23May 14 total 7.66May 15 total 8.97May 16 total 0.52May 20 total 4.09May 24 total 1.32Jun 9 total 15.18Jun 10 total 2.97Jun 22 total 2.61Jul 19 total 1.95Today total 0.29
1.3、获取各个用户的总连接时间
使用 -p参数,你可以查看每位用户各自在系统中总的连接时长。
$ ac -p
示例输出:
ostechnix 52.85root 0.51total 53.36
1.4、显示指定用户的总连接时间
你还可以显示特定用户的总登录时间。
$ ac ostechnix
示例输出:
total 52.95
1.5、显示特定用户各日期的总连接时间
要按日期查看某个用户的登录时间,可以运行:
$ ac -d ostechnix
示例输出:
May 11 total 4.29May 13 total 3.23May 14 total 7.66May 15 total 8.97May 16 total 0.01May 20 total 4.09May 24 total 1.32Jun 9 total 15.18Jun 10 total 2.97Jun 22 total 2.61Jul 19 total 1.95Today total 0.68
如需更多详情,可参考手册页面。
$ man ac
2、lastcomm 命令使用示例
lastcomm工具用于列出过去执行过的命令,它会按执行的最近程度将命令列在前面。
2.1、展示过去执行的命令
$ lastcomm
示例输出:
systemd-hostnam S root __ 0.06 secs Thu Oct 13 17:21systemd-localed S root __ 0.06 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22awk ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22uname ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22sed ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22grep ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22grep ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22[...]
2.2、打印特定用户先前执行的命令
上述命令显示的是所有用户的命令。要显示特定用户以前执行的命令,可以使用下面的命令:
$ lastcomm ostechnix
示例输出:
less ostechni pts/1 0.00 secs Thu Oct 13 17:26lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:26lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:26lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:26gdbus X ostechni __ 0.00 secs Thu Oct 13 17:24lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:24ac ostechni pts/1 0.00 secs Thu Oct 13 17:24update-notifier F ostechni __ 0.00 secs Thu Oct 13 17:23apport-checkrep ostechni __ 0.06 secs Thu Oct 13 17:23apport-checkrep ostechni __ 0.05 secs Thu Oct 13 17:23systemctl ostechni __ 0.00 secs Thu Oct 13 17:23apt-check ostechni __ 0.81 secs Thu Oct 13 17:23dpkg ostechni __ 0.00 secs Thu Oct 13 17:23ischroot ostechni __ 0.00 secs Thu Oct 13 17:23dpkg ostechni __ 0.00 secs Thu Oct 13 17:23[...]
2.3、打印特定命令的执行次数
你还可以查看特定命令被执行的次数。
$ lastcomm apt
示例输出:
apt S root pts/2 0.70 secs Thu Oct 13 16:06apt F root pts/2 0.00 secs Thu Oct 13 16:06apt F root pts/2 0.00 secs Thu Oct 13 16:06
如上述输出所示,root 用户执行了 apt命令三次。
对于账户文件中的每个命令,使用 -u标志打印用户 ID 和命令名称。
$ sa -u
示例输出:
root 0.00 cpu 693k mem 0 io accton root 0.00 cpu 3668k mem 0 io systemd-tty-ask root 0.00 cpu 3260k mem 0 io systemctl root 0.01 cpu 3764k mem 0 io deb-systemd-inv root 0.00 cpu 722k mem 0 io acct.postinst root 0.00 cpu 704k mem 0 io rm root 0.00 cpu 939k mem 0 io cp root 0.00 cpu 704k mem 0 io rm root 0.00 cpu 951k mem 0 io find root 0.00 cpu 911k mem 0 io gzip root 0.00 cpu 722k mem 0 io sh root 0.00 cpu 748k mem 0 io install-info root 0.00 cpu 911k mem 0 io gzip [...]
如需更多详细信息,请参考手册页:
$ man sa
4、dump-acct 和 dump-utmp 命令
dump-acct实用工具将 accton 格式的输出文件显示为人类可读的格式。
$ dump-acct /var/account/pacct
dump-utmp将 utmp 文件显示为人类可读的格式。
$ dump-utmp /var/run/utmp
如需了解更多详情,请参考手册页:
$ man dump-acct$ man dump-utmp
5、accton 命令
accton命令将允许你开启或关闭记账。
要开启进程记账,请运行:
$ accton on
要关闭它,运行:
$ accton off
如需了解更多详情,请参考手册页:
$ man accton
总结
每个 Linux 管理员都应该知道 GNU 记账实用程序,以便注意所有用户的行为。在故障排除时,这些实用程序会非常有帮助。