2023-01-10 08:45:16 +08:00
|
|
|
|
[#]: subject: "who Command in Linux: Explanation with Examples"
|
|
|
|
|
[#]: via: "https://www.debugpoint.com/who-command-linux/"
|
|
|
|
|
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
|
|
|
|
[#]: collector: "lkxed"
|
|
|
|
|
[#]: translator: "geekpi"
|
2023-01-10 13:03:17 +08:00
|
|
|
|
[#]: reviewer: "wxy"
|
|
|
|
|
[#]: publisher: "wxy"
|
|
|
|
|
[#]: url: "https://linux.cn/article-15430-1.html"
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
who 命令的解释与示例
|
2023-01-10 08:45:16 +08:00
|
|
|
|
======
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
![][0]
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
> 这里是一个关于理解 Linux 中 who 命令的初学者指南,并带有几个例子。
|
|
|
|
|
|
|
|
|
|
这篇文章是 [Linux 命令][1]学习系列的一部分。
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
### who 命令
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
Linux 中的 `who` 命令用于显示当前登录到系统中的用户的信息。它显示用户的登录名,用户登录的终端,用户登录的时间,以及远程主机名(如果有)。
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
#### 语法
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
下面是 `who` 命令的基本语法:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
who [OPTION]... [ FILE | ARG1 ARG2 ]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 各种 who 命令和开关的例子
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
默认情况下,`who` 读取文件 `/var/run/utmp`,其中包含当前登录的用户的信息。如果没有指定选项,它会显示每个用户的登录名、终端和登录时间。
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
who
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
它给出了以下输出。你可以看到它显示了登录名是 `debugpoint`,终端 ID `tty2` 和登录的日期和时间。
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
debugpoint tty2 2023-01-01 11:22 (tty2)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![who 命令 - 默认示例][2]
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
然而,如果你在虚拟机中运行上述命令,你应该看到同样的情况,但终端 ID 将是 x11 服务器的显示名称,即 `:0`。
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
2023-01-10 13:03:17 +08:00
|
|
|
|
❯ who
|
|
|
|
|
debugpoint :0 2023-01-01 23:36 (:0)
|
2023-01-10 08:45:16 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
要显示当前用户的用户名和信息,使用下面的方法:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
whoami
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
使用 `-b` 选项查看最后一次系统启动时间:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
2023-01-10 13:03:17 +08:00
|
|
|
|
❯ who -b
|
|
|
|
|
system boot 2023-01-01 23:36
|
2023-01-10 08:45:16 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
显示当前系统中登录的用户数:
|
|
|
|
|
|
|
|
|
|
```
|
2023-01-10 13:03:17 +08:00
|
|
|
|
❯ who -q
|
|
|
|
|
debugpoint
|
|
|
|
|
users=1
|
2023-01-10 08:45:16 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
所有上述命令与 `-H` 选项配对时,你会有一个更好的含标题行的信息,如下所示:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
who -H
|
|
|
|
|
|
|
|
|
|
NAME LINE TIME COMMENT
|
|
|
|
|
debugpoint tty2 2023-01-01 11:22 (tty2)
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
如果你想在 Linux 中显示与 `who` 命令有关的所有信息,请使用选项 `-a`:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
who -aH
|
|
|
|
|
|
|
|
|
|
NAME LINE TIME IDLE PID COMMENT EXIT
|
|
|
|
|
system boot 2023-01-01 11:19
|
|
|
|
|
run-level 5 2023-01-01 11:19
|
|
|
|
|
debugpoint + tty2 2023-01-01 11:22 13:26 2042 (tty2)
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
像往常一样,你可以使用下面的重定向将 `who` 命令的输出保存到任何文件:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
who > user_details.txt
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### who 命令选项的例子总结
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
下面是一些 `who` 命令的例子和它们的解释:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
下面是一些可以与 `who` 命令一起使用的选项:
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
- `-a`: 显示每个用户的主机名、登录时间和进程
|
|
|
|
|
- `-b`: 显示上次系统启动的时间
|
|
|
|
|
- `-d`: 显示死进程(已终止但未从 utmp 文件中删除的进程)
|
|
|
|
|
- `-H`: 显示标题行
|
|
|
|
|
- `-l`: 显示长格式的登录进程
|
|
|
|
|
- `-m`: 只显示在 `ARG1 ARG2` 指定的终端上登录的用户的名字和行。
|
|
|
|
|
- `-q`: 显示已登录用户的数量
|
2023-01-10 13:03:17 +08:00
|
|
|
|
- `-u`: 显示拥有未脱离进程的用户的信息
|
2023-01-10 08:45:16 +08:00
|
|
|
|
- `-w`: 显示已经登录的用户信息,格式与 utmp 文件相同
|
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
### 总结
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
2023-01-10 13:03:17 +08:00
|
|
|
|
我希望这篇文章能够帮助你了解 `who` 命令及其基本原理。你也可以阅读 [who 手册页][3]来了解更多。如果你有任何问题,请告诉我。
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://www.debugpoint.com/who-command-linux/
|
|
|
|
|
|
|
|
|
|
作者:[Arindam][a]
|
|
|
|
|
选题:[lkxed][b]
|
2023-01-10 13:03:17 +08:00
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2023-01-10 08:45:16 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://www.debugpoint.com/author/admin1/
|
|
|
|
|
[b]: https://github.com/lkxed
|
|
|
|
|
[1]: https://www.debugpoint.com/category/linux-commands
|
|
|
|
|
[2]: https://www.debugpoint.com/wp-content/uploads/2023/01/who-command-default-example.jpg
|
|
|
|
|
[3]: https://man7.org/linux/man-pages/man1/who.1.html
|
2023-01-10 13:03:17 +08:00
|
|
|
|
[0]: https://img.linux.net.cn/data/attachment/album/202301/10/130213zb6odhv8gl8cvxvo.jpg
|