为什么 bash 在通过 ssh 工作时以非交互模式获取 .bashrc?

eci*_*iii 6 shell bash ssh openssh

我有一个 CentOS 虚拟机,我使用 SSH 登录。我将以下行附加到.bashrc

echo "from ~/.bashrc: (pid $$)"
Run Code Online (Sandbox Code Playgroud)

和以下行.bash_profile

echo "from ~/.bash_profile"
Run Code Online (Sandbox Code Playgroud)

当我登录虚拟机并运行时,ps我得到以下输出

user@laptop:~ $ ssh vmcentos
Last login: Sun May 17 04:48:24 2020 from 192.168.122.1
from ~/.bashrc: (pid 1821)
from ~/.bash_profile
[admin@localhost ~]$ ps -H -o pid,command
  PID COMMAND
 1821 -bash
 1844   ps -H -o pid,command
[admin@localhost ~]$
Run Code Online (Sandbox Code Playgroud)

此输出是我所期望的,因为我登录的 shell 是交互式登录 shell,因此该.bash_profile文件是来源的,而该文件又是该.bashrc文件的来源。

现在我从虚拟机注销并执行以下命令

user@laptop:~ $ ssh vmcentos 'sleep 60; echo $-'
from ~/.bashrc: (pid 1901)
hBc
user@laptop:~ $
Run Code Online (Sandbox Code Playgroud)

然后我登录到ssh虚拟机上的另一个会话并检查进程表

[admin@localhost ~]$ ps -eH -o pid,command
  PID COMMAND
(... more output here...)
 1900       sshd: admin@notty
 1901         bash -c sleep 60; echo $-
 1914           sleep 60
(... more output here...)
Run Code Online (Sandbox Code Playgroud)

据我了解,ssh执行的 shell(进程 1901)是非交互式的(因为选项-c,也因为$-变量不包含i字符)和非登录(因为ARGV0不是-bash并且没有--login提供选项)。因此,既不.bashrc也不.bash_profile应该采购。然而该命令的输出清楚地显示了它的.bashrc来源。为什么?

我使用标准 CentOS 安装和标准openssh配置。

ilk*_*chu 9

根据手册,它应该这样做

Bash 尝试确定它何时使用连接到网络连接的标准输入运行,例如何时由远程 shell 守护程序(通常为 )rshd或安全 shell 守护程序执行sshd。如果 Bash 确定它正在以这种方式运行,那么它会从 读取并执行命令 ~/.bashrc(如果该文件存在并且可读)。

Bash 的启动文件很奇怪。