who 和 whoami 命令的区别

nar*_*ary 6 command-line who whoami

who命令的手册页描述是 who - show who is logged on

但是存在一个类似的命令whoami。的手册页描述whoami

whoami - print effective userid

谁能解释一下这些命令的作用?它们有何不同?

Ram*_*esh 8

我在我的 shell 中以 root 身份登录并输入who,这是输出。

who
root     tty1         2014-08-25 14:01 (:0)
root     pts/0        2014-09-05 10:22 (:0.0)
root     pts/3        2014-09-19 10:08 (xxx.xxx.edu)
Run Code Online (Sandbox Code Playgroud)

它有效地显示了所有已建立连接的用户。

ssh ramesh@hostname
Run Code Online (Sandbox Code Playgroud)

who再次运行将导致用户 ramesh 的另一个条目。

who
root     tty1         2014-08-25 14:01 (:0)
root     pts/0        2014-09-05 10:22 (:0.0)
root     pts/3        2014-09-19 10:08 (xxx.xxx.edu)
ramesh   pts/4        2014-09-19 12:11 (xxx.xxx.edu)
Run Code Online (Sandbox Code Playgroud)

rootshell 中,我只是执行su ramesh然后运行whoami. 它将为我提供当前用户 ramesh 作为输出。

有效地,who给出当前登录到机器上的所有用户的列表,并且whoami你可以知道当前在 shell 中的用户。


Per*_*ulf 5

who:打印当前登录用户的信息。

whoami: 打印运行用户的有效用户名whoami

例如:

mohsen@debian:~$ who ## list logged in usernames
mohsen   :0           2014-09-19 16:31 (:0)
mohsen   pts/0        2014-09-19 16:32 (:0)
mohsen   pts/1        2014-09-19 19:42 (:0)

mohsen@debian:~$ whoami 
mohsen                        ##### print my username (mohsen)
mohsen@debian:~$ 
Run Code Online (Sandbox Code Playgroud)

我还推荐一个比who,命令更好的w命令。它的输出是:

mohsen@debian:~$ w
 21:45:45 up  5:16,  3 users,  load average: 0.68, 0.54, 0.46
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
mohsen   :0       :0               16:31   ?xdm?   1:40m  0.27s gdm-session-wor
mohsen   pts/0    :0               16:32    1.00s  0.15s  0.01s w
mohsen   pts/1    :0               19:42    2:03m  0.13s 14.06s /usr/bin/python
Run Code Online (Sandbox Code Playgroud)

也可以看看 :

last命令/var/log/btmp/var/log/wtmp文件。