订阅内容

There are lots of monitoring apps for servers, and they’ve done a lot to help adoption of Linux with sysadmins who aren’t used to the Terminal or who have a real need for graphical representations of data. However, Linux has been a multi-user system since the beginning, and UNIX long before that, so there are built-in tools that go back 40 years to help you monitor who’s logged into your server, who’s using resources, and for what.

You don’t have to be paranoid or even nosy to justify keeping a close watch on a server that’s been made your responsibility. The psacct package contains several commands to gather detailed reports about user status and activity.

accton

Not all commands in the acct utilities require that you activate accounting, but many do. If you intend to use acct commands, you should enable accounting with the accton command.

To activate:

$ sudo accton on

By default, accounting records are stored in /var/account/pacct. This file could feasibly become quite large, so use logrotate or a similar tool to ensure proper log management.

To deactivate accounting:

$ sudo accton off

ac

The ac command prints statistics about connection times. If you need to get an overview of how active users have been on a system, the --individual-totals option provides that. It generates its report in hours based on logins and logouts recorded in the /var/log/wtmp file.

The accounting file wtmp is maintained by init(8) and login(1) but neither ac or login actually creates the file. If wtmp doesn’t exist, then no report is generated, but you can point ac to an alternate location using the --file option. If a wtmp file doesn’t exist on your system, you can create an empty wtmp file to enable reporting on your system.

To get a report on login times for individual users:

$ ac --individual-totals
seth     20.16
larry    43.60
curly    10.32
moe      35.11

You can also get daily totals:

$ ac --daily-totals
Jan 20  total       22.61
Jan 21  total       73.60
Jan 22  total       84.00
Jan 23  total       100.69
Jan 24  total       18.24
Jan 25  total        2.43
Jan 27  total       35.36
Today   total       62.13

lastcomm

The lastcomm command displays the last commands issued on the system for a given user. If no user is specified, then a report on the current user is generated.

$ sudo lastcomm --strict-match --user curly --tty pts/2
basename               curly   pts/2      0.00 secs Tue Jan 28 15:41
ps                     curly   pts/2      0.00 secs Tue Jan 28 15:41
bash              F    curly   pts/2      0.00 secs Tue Jan 28 15:41
manpath                curly   pts/2      0.00 secs Tue Jan 28 15:41
bash              F    curly   pts/2      0.00 secs Tue Jan 28 15:41
tclsh                  curly   pts/2      0.00 secs Tue Jan 28 15:41
bash              F    curly   pts/2      0.00 secs Tue Jan 28 15:41
bash              F    curly   pts/2      0.00 secs Tue Jan 28 15:41
sed                    curly   pts/2      0.00 secs Tue Jan 28 15:41

The commands listed by lastcomm aren’t necessarily commands a user launched interactively. For instance, simply by logging in, a user spawns nearly 40 items in the output of lastcomm, so it can be overwhelming. Coupled with grep, though, it’s an easy way to get a sense of a user’s session history.

sa

The sa command summarizes accounting information derived from the /var/account/pacct file. If you’re auditing the activities of users, then the --print-users option prints the user name before each command:

$ sudo sa --print-users
root    0.00 cpu      579k mem      0 io accton
root    0.03 cpu    64064k mem      0 io sudo 
seth    0.00 cpu    56752k mem      0 io bash         *
seth    0.00 cpu    54080k mem      0 io sed 
seth    0.00 cpu    56752k mem      0 io bash         *
larry   0.00 cpu    56752k mem      0 io bash         *
curly   0.00 cpu    56752k mem      0 io bash         *
moe     0.00 cpu    56752k mem      0 io bash         *
seth    0.00 cpu    54080k mem      0 io ls 

Alternatively, you can get just a summary for each user:

$ sudo sa --user-summary
                                     1065    2169.59re       0.97cp         0avio     49373k
seth                                  812    1117.11re       0.83cp         0avio     58163k
root                                  199    1052.42re       0.14cp         0avio     21314k
larry                                  41       0.00re       0.00cp         0avio     19403k
curly                                   1       0.06re       0.00cp         0avio      6706k
moe                                    12       0.00re       0.00cp         0avio     25888k
[...]

The columns displayed, in addition to user names, report on CPU (real time and CPU time), I/O operations per command (average and total), and so on. They can be configured using options such as --sort-tio for total I/O, --sort-cpu-avmem to sort CPU time by average memory usage, and so on. All sorting filters can be reversed with the --reverse-sort option.

User profiling

Combined with tools like who, w, and ps, you can get a sense for how users are spending system resources, which commands may be problematic, and what server upgrades could be useful in the future. Because the acct suite is terminal-based, it can be scripted and utilized by other tools, allowing you to create a customized reporting mechanism.

[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]


关于作者

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Original series icon

原创节目

关于企业技术领域的创客和领导者们有趣的故事