Linux last 新手命令教程(8例)
如果您不熟悉系统管理,您有时需要访问或监控凭据。您可能会为此目的使用多种工具,其中一种是最后一种。在本教程中,我们将通过一些通俗易懂的例子来解释这个程序的基本功能。
在我们开始解释之前,我们应该提到这里的所有示例都已经在 Ubuntu 20.04 和 Ubuntu 22.04 LTS 系统上进行了测试,但它们在任何最新的 Linux 发行版上应该以完全相同的方式工作。
Linux 最后一个命令
last 命令显示最后登录的用户列表。以下是它的语法:
last [options] [username...] [tty...]
以下是手册页对这个工具的解释:
last searches back through the /var/log/wtmp file (or the file desig?
nated by the -f option) and displays a list of all users logged in (and
out) since that file was created. One or more usernames and/or ttys
can be given, in which case last will show only the entries matching
those arguments. Names of ttys can be abbreviated, thus last 0 is the
same as last tty0.
When catching a SIGINT signal (generated by the interrupt key, usually
control-C) or a SIGQUIT signal, last will show how far it has searched
through the file; in the case of the SIGINT signal last will then ter?
minate.
The pseudo user reboot logs in each time the system is rebooted. Thus
last reboot will show a log of all the reboots since the log file was
created.
以下是一些 Q&A 风格的示例,应该可以让您更好地了解 last 的工作原理。
Q1。如何使用最后一个命令?
基本用法非常简单。您所要做的就是运行没有任何选项的最后一个命令:
last

Q2。在非本地登录的情况下如何自定义输出?
默认情况下,对于非本地插件,last 命令以下列方式显示输出:

注意:在屏幕截图中,我们有意模糊了一些包含 IP 地址的部分。
现在,如果需要,您可以通过将与 IP 地址相关的列移至最右侧来稍微自定义此输出。这可以使用 -a 命令行选项来完成。

Q3.如何使上次读取不同的文件?
正如本教程开头所述,最后一个命令读取 /var/log/wtmp 文件以准备其输出。但是,如果需要,您可以让该工具读取一个完全不同的文件。您可以使用 -f 命令行选项执行此操作。当然,您必须将新文件名(及其路径)作为输入传递给此选项。
last -f [new-file-path-and-name]
Q4.如何使上次打印完整的日期和时间信息?
如果您希望 last 在输出中产生完整的日期和时间信息,请使用 -F 命令行选项。
last -F

Q5.如何让 last 只显示特定行数?
如果您想自定义最后一条命令在输出中显示的行数,您可以使用 -n 命令行选项来实现。当然,您必须将数字作为输入传递给此选项。
例如:
last -n 3

所以你可以看到输出只包含 3 行。
Q6.如何在输出中最后抑制主机名字段?
lasts 输出的第三列是主机名信息。但是,出于某种原因,如果您希望该工具禁止显示此信息,请使用 -R 命令行选项。
last -R

Q7.如何制作特定时间段的最后显示信息?
假设您希望 last 仅显示基于时间的输出 - 例如,仅显示昨天和今天的信息 - 那么您可以使用 -s 和 -t 命令行选项。
例如:
last -s yesterday -t today

Please note the options that take the time argument understand the following formats:
YYYYMMDDhhmmss
YYYY-MM-DD hh:mm:ss
YYYY-MM-DD hh:mm (seconds will be set to 00)
YYYY-MM-DD (time will be set to 00:00:00)
hh:mm:ss (date will be set to today)
hh:mm (date will be set to today, seconds to 00)
now
yesterday (time is set to 00:00:00)
today (time is set to 00:00:00)
tomorrow (time is set to 00:00:00)
+5min
-5days
Q8.如何使最后显示诸如运行级别更改之类的东西?
要显示运行级别更改和系统关闭条目等信息,请使用 -x 命令行选项。
last -x

结论
同意,last 不是您每天都会使用的那些命令之一,但可能会有几天此工具会提供很大帮助。我们在本教程中介绍了几个主要选项 - 应该足以让您入门。有关更多信息,请访问实用程序手册页。