● acct.service - Kernel process accounting Loaded: loaded (/lib/systemd/system/acct.service; enabled; vendor preset: enabled) Active: active (exited) since Thu 2022-10-13 16:06:35 IST; 28s ago Docs: man:accton(8) Process: 3241 ExecStart=/usr/sbin/accton /var/log/account/pacct (code=exited, status=0/SUCCESS) Main PID: 3241 (code=exited, status=0/SUCCESS) CPU: 879us
Oct 13 16:06:35 ubuntu2204 systemd[1]: Starting Kernel process accounting... Oct 13 16:06:35 ubuntu2204 accton[3241]: Turning on process accounting, file set to '/var/log/account/pacct'. Oct 13 16:06:35 ubuntu2204 systemd[1]: Finished Kernel process accounting.
利用 psacct 或 acct 来监测 Linux 中的用户活动
psacct(进程记账)软件包包含以下用来监测用户和进程活动的工具:
ac - 提供用户登录时间的统计信息。
lastcomm - 展示先前执行过的命令的信息。
accton - 开启或关闭进程记账。
dump-acct - 把 accton 的输出文件转化为易读的格式。
dump-utmp - 以易读的方式打印 utmp 文件。
sa - 汇总信息,关于先前执行的命令。
现在就让我们一起了解如何通过每个工具来监控 Linux 用户的活动。
1、使用 ac 命令
ac 工具可以为你提供以小时为单位的连接时间报告,这样你就能知道用户或一组用户连接到系统的时长。
1.1、展示所有用户的总连接时间
1 2
$ ac
上述命令会显示所有用户的总连接时间(单位为小时)。
1 2
total 52.91
1.2、按日期排序显示所有用户的总连接时间
你可以通过使用 -d 参数,按日期排序显示所有用户的连接时间,操作如下:
1 2
$ ac -d
示例输出:
1 2 3 4 5 6 7 8 9 10 11 12 13
May 11 total 4.29 May 13 total 3.23 May 14 total 7.66 May 15 total 8.97 May 16 total 0.52 May 20 total 4.09 May 24 total 1.32 Jun 9 total 15.18 Jun 10 total 2.97 Jun 22 total 2.61 Jul 19 total 1.95 Today total 0.29
1.3、获取各个用户的总连接时间
使用 -p 参数,你可以查看每位用户各自在系统中总的连接时长。
1 2
$ ac -p
示例输出:
1 2 3 4
ostechnix 52.85 root 0.51 total 53.36
1.4、显示指定用户的总连接时间
你还可以显示特定用户的总登录时间。
1 2
$ ac ostechnix
示例输出:
1 2
total 52.95
1.5、显示特定用户各日期的总连接时间
要按日期查看某个用户的登录时间,可以运行:
1 2
$ ac -d ostechnix
示例输出:
1 2 3 4 5 6 7 8 9 10 11 12 13
May 11 total 4.29 May 13 total 3.23 May 14 total 7.66 May 15 total 8.97 May 16 total 0.01 May 20 total 4.09 May 24 total 1.32 Jun 9 total 15.18 Jun 10 total 2.97 Jun 22 total 2.61 Jul 19 total 1.95 Today total 0.68
如需更多详情,可参考手册页面。
1 2
$ man ac
2、lastcomm 命令使用示例
lastcomm 工具用于列出过去执行过的命令,它会按执行的最近程度将命令列在前面。
2.1、展示过去执行的命令
1 2
$ lastcomm
示例输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
systemd-hostnam S root __ 0.06 secs Thu Oct 13 17:21 systemd-localed S root __ 0.06 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 awk ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 uname ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 sed ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 grep ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 grep ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 bash F ostechni pts/1 0.00 secs Thu Oct 13 17:22 [...]
2.2、打印特定用户先前执行的命令
上述命令显示的是所有用户的命令。要显示特定用户以前执行的命令,可以使用下面的命令:
1 2
$ lastcomm ostechnix
示例输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
less ostechni pts/1 0.00 secs Thu Oct 13 17:26 lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:26 lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:26 lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:26 gdbus X ostechni __ 0.00 secs Thu Oct 13 17:24 lastcomm ostechni pts/1 0.00 secs Thu Oct 13 17:24 ac ostechni pts/1 0.00 secs Thu Oct 13 17:24 update-notifier F ostechni __ 0.00 secs Thu Oct 13 17:23 apport-checkrep ostechni __ 0.06 secs Thu Oct 13 17:23 apport-checkrep ostechni __ 0.05 secs Thu Oct 13 17:23 systemctl ostechni __ 0.00 secs Thu Oct 13 17:23 apt-check ostechni __ 0.81 secs Thu Oct 13 17:23 dpkg ostechni __ 0.00 secs Thu Oct 13 17:23 ischroot ostechni __ 0.00 secs Thu Oct 13 17:23 dpkg ostechni __ 0.00 secs Thu Oct 13 17:23 [...]
2.3、打印特定命令的执行次数
你还可以查看特定命令被执行的次数。
1 2
$ lastcomm apt
示例输出:
1 2 3 4
apt S root pts/2 0.70 secs Thu Oct 13 16:06 apt F root pts/2 0.00 secs Thu Oct 13 16:06 apt F root pts/2 0.00 secs Thu Oct 13 16:06
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 [...]
如需更多详细信息,请参考手册页:
1 2
$ man sa
4、dump-acct 和 dump-utmp 命令
dump-acct 实用工具将 accton 格式的输出文件显示为人类可读的格式。
1 2
$ dump-acct /var/account/pacct
dump-utmp 将 utmp 文件显示为人类可读的格式。
1 2
$ dump-utmp /var/run/utmp
如需了解更多详情,请参考手册页:
1 2
$ man dump-acct
1 2
$ man dump-utmp
5、accton 命令
accton 命令将允许你开启或关闭记账。
要开启进程记账,请运行:
1 2
$ accton on
要关闭它,运行:
1 2
$ accton off
如需了解更多详情,请参考手册页:
1 2
$ man accton
总结
每个 Linux 管理员都应该知道 GNU 记账实用程序,以便注意所有用户的行为。在故障排除时,这些实用程序会非常有帮助。