> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/last.1.md).

# last.1

`last` — 显示用户和 tty 的最近登录

## 名称

`last`

## 概要

`last [--libxo] [-swy] [-d [[CC]YY] [MM DD] hh mm [.SS]] [-f file] [-h host] [-n maxrec] [-t tty] [user ...]`

## 描述

`last` 实用程序将按时间倒序列出指定 `users`、`ttys` 和 `hosts` 的会话，或列出在指定日期和时间登录的用户。每行输出包含用户名、会话所在的 tty、任何主机名、会话的开始和结束时间以及会话的持续时间。如果会话仍在进行或因崩溃或关机而中断，`last` 会相应地指示。

以下选项可用：

**`--libxo`** 通过 libxo(3) 以多种人类和机器可读格式生成输出。有关命令行参数的详细信息，请参见 xo\_options(7)。

**`-d`** `date` 指定快照日期和时间。将报告在快照日期和时间登录的所有用户。这可以与 `-f` 选项一起使用，从存储的 `utx.log` 文件中派生结果。提供此参数时，除 `-f` 和 `-n` 之外的所有其他选项都将被忽略。参数应采用 `[[CC]YY] [MM DD] hh mm [.SS]` 的形式，其中每对字母表示以下内容：

**`CC`** 年的前两位数字（世纪）。

**`YY`** 年的后两位数字。如果指定了 `YY` 但未指定 `CC`，且 `YY` 的值在 69 到 99 之间，则 `CC` 值为 19。否则，使用 `CC` 值 20。

**`MM`** 月份，从 1 到 12。

**`DD`** 月中的日期，从 1 到 31。

**`hh`** 小时，从 0 到 23。

**`mm`** 分钟，从 0 到 59。

**`SS`** 秒，从 0 到 60。

如果未指定 `CC` 和 `YY` 字母对，则值默认为当前年份。如果未指定 `SS` 字母对，则值默认为 0。

**`-f`** `file` 读取文件 `file` 而非默认的 **/var/log/utx.log**。

**`-h`** `host` `Host` 名称可以是名称或互联网号码。

**`-n`** `maxrec` 将报告限制为 `maxrec` 行。

**`-s`** 以秒为单位报告登录会话的持续时间，而非默认的天、小时和分钟。

**`-t`** `tty` 指定 `tty`。Tty 名称可以完整或缩写给出，例如，"`last -t 03`" 等同于 "`last -t tty03`"。

**`-w`** 扩展持续时间字段以显示秒，以及默认的天、小时和分钟。

**`-y`** 在会话开始时间中报告年份。

如果给出了多个参数，且未指定快照时间，则将打印适用于任何参数的信息，例如，"`last root -t console`" 将列出 "`root`" 的所有会话以及控制台终端上的所有会话。如果未指定用户、主机名或终端，`last` 将打印所有登录和注销的记录。

伪用户 `reboot` 在系统重启时登录，因此 "`last reboot`" 将给出重启间平均时间的指示。

如果 `last` 被中断，它会指示搜索已进行到哪个日期。如果被退出信号中断，`last` 会指示搜索已进行到哪里，然后继续。

## 文件

**/var/log/utx.log** 登录数据库

## 实例

显示 pts/14 中的登录，持续时间以秒为单位，并将报告限制为两行：

```sh
$ last -n2 -s -t pts/14
bob    pts/1   Wed Dec  9 11:08   still logged in
bob    pts/2   Mon Dec  7 20:10 - 20:23  (     776)
```

显示当前年份 12 月 7 日 20:23 时的活动登录：

```sh
$ last -d 12072023
bob    pts/1    Mon Dec  7 20:10 - 20:23  (00:12)
bob    pts/6    Mon Dec  7 19:24 - 22:27  (03:03)
alice  ttyv0    Mon Dec  7 19:18 - 22:27  (03:09)
```

## 参见

[lastcomm(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/lastcomm.1.md), getutxent(3), libxo(3), xo\_options(7), ac(8), [lastlogin(8)](/man/man8/lastlogin.8.md)

## 历史

`last` 实用程序首次出现于 1BSD。

## 作者

原始版本由 Howard P. Katseff 编写；Keith Bostic 在 1986/87 年重写了它以增加功能并提高代码质量。Philip Paeps 在 2018 年 8 月添加了 libxo(3) 支持。

## 缺陷

如果登录 shell 因某种原因异常终止，注销记录可能不会被写入 `utx.log` 文件。在这种情况下，`last` 会将注销时间指示为 "shutdown"。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/last.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
