"who am i" 在 ansible 和 ssh 上什么都不输出

"who am i" in ansible and over ssh outputs nothing

我在ansible中有以下测试代码:

- shell: who am i
  register: w

- debug:
    msg:
      - "{{ w }}" 

产生如下所示的空标准输出:

ok: [10.20.30.40] => {
    "msg": [
        {
            "changed": true,
            "cmd": "who am i",
            "delta": "0:00:00.003400",
            "end": "2021-02-02 21:19:50.454729",
            "failed": false,
            "rc": 0,
            "start": "2021-02-02 21:19:50.451329",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "",
            "stdout_lines": []
        }
    ]
}

该命令在某些服务器上有效,但在其他服务器上无效。我不知道有什么区别。 我只能猜测它与终端信息有关。当我以交互方式键入“我是谁”命令时,我得到 符合我的预期。

我还注意到 ssh 上的“who”命令中的其他奇怪行为:

$ ssh myhost who       # who outputs nothing
$ ssh myhost who am i  # who am i outputs nothing
$ ssh myhost whoami    # whoami works
j.smith

也许他们是相关的。

我需要在 sudo 命令中使用 who am i 来查找发出 sudo 的用户。如果有别的办法 查找信息,我很乐意使用它,但我仍然想知道 who am i 发生了什么。 非常感谢。

whoami显示当前有效用户的用户名

who am i 又名 who -m 显示任何标准输入的 utmp 记录。

utmp记录一般由login和各种终端模拟器添加。如果您不创建登录会话,who am i 将为空白:

$ ssh localhost who am i
(nothing)

$ ssh -t localhost who am i
me    pts/31       Feb  2 14:27 (127.0.0.1)

如果你运行 screendeflogin off,那么 who am i 将永远为空。这就是为什么你永远不应该使用 who am i 来试图找出谁 运行 sudo(而是使用 $SUDO_USER)的原因之一。