# 16.13.进程记账

进程记账是一种安全方法，管理员可以用来跟踪系统资源的使用情况及其在用户之间的分配，提供系统监控，并最小化地跟踪用户的命令。

进程记账有其优点和缺点。一个优点是，可以将入侵范围缩小到入侵的入口点。一个缺点是进程记账生成的日志数量及其可能占用的磁盘空间。本节将引导管理员了解进程记账的基础。

> **注意**
>
> 如果需要更细粒度的记账，请参考[安全事件审计](https://docs.freebsd.org/en/books/handbook/audit/#audit)。

## 16.13.1. 启用和使用进程记账

在使用进程记账之前，必须使用以下命令启用它：

```sh
# sysrc accounting_enable=yes
# service accounting start
```

记账信息存储在 **/var/account** 中的文件中，如果必要，第一次启动记账服务时会自动创建这些文件。这些文件包含敏感信息，包括所有用户发出的命令。对这些文件的写入权限仅限于 `root`，读取权限仅限于 `root` 和 `wheel` 组的成员。为了防止 `wheel` 组的成员读取这些文件，可以将 **/var/account** 目录的模式更改为仅允许 `root` 访问。

启用后，记账将开始跟踪信息，如 CPU 统计信息和执行的命令。所有记账日志都是不可读的格式，可以使用 [sa(8)](https://man.freebsd.org/cgi/man.cgi?query=sa\&sektion=8\&format=html) 查看。如果不带任何选项运行 [sa(8)](https://man.freebsd.org/cgi/man.cgi?query=sa\&sektion=8\&format=html)，则会打印与每个用户的调用数量、总的经过时间（分钟）、总的 CPU 时间和用户时间（分钟）以及平均 I/O 操作次数相关的信息。有关可用选项的列表，请参阅 [sa(8)](https://man.freebsd.org/cgi/man.cgi?query=sa\&sektion=8\&format=html)。

要显示用户发出的命令，可以使用 `lastcomm`。

例如，以下命令会打印出 `trhodes` 在 `ttyp1` 终端上使用 `ls` 的所有记录：

```sh
# lastcomm ls trhodes ttyp1
```

还有许多其他有用的选项，可以在 [lastcomm(1)](https://man.freebsd.org/cgi/man.cgi?query=lastcomm\&sektion=1\&format=html)、[acct(5)](https://man.freebsd.org/cgi/man.cgi?query=acct\&sektion=5\&format=html) 和 [sa(8)](https://man.freebsd.org/cgi/man.cgi?query=sa\&sektion=8\&format=html) 中找到解释。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/hanbook/di-16-zhang-an-quan/16.13.-jin-cheng-shen-ji.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
