问题描述
如果我 ssh 到一台机器并运行它,我有一个运行良好的命令,但是当我尝试使用远程 ssh 命令运行它时失败:
ssh user@IP
比较env"在不同环境下使用这两种方法的输出结果.当我手动登录到机器并运行 env 时,我得到的环境变量比运行时多得多:
ssh user@IP "env"
知道为什么吗?
shell 有多种类型.SSH 命令执行 shell 是一个非交互式 shell,而您的普通 shell 是登录 shell 或交互式 shell.描述如下,来自 man bash:
登录 shell 是其参数的第一个字符零是一个 - 或一个以 --login 选项开头的.交互式 shell 是一个没有非选项的启动参数并且没有 -c 选项的标准输入和错误都连接到终端(如确定通过 isatty(3)),或者以 -i 选项开头.PS1是set 和 $- 包括 i 如果 bash 是交互式的,则允许shell 脚本或启动文件来测试此状态.以下段落描述了 bash 如何执行它的启动文件.如果任何文件存在但不能读取,bash报错.波浪号在文件中展开名称如下面的波浪号扩展中所述扩展部分.当 bash 作为交互式登录 shell 调用时,或作为带有 --login 选项的非交互式 shell,它首先从文件/etc/profile 读取并执行命令,如果该文件存在.读取该文件后,它会查找~/.bash_profile, ~/.bash_login, and ~/.profile, 在那order,并从第一个读取和执行命令存在且可读.--noprofile 选项可能在 shell 启动时使用以抑制这种行为或.当登录 shell 退出时,bash 读取并执行命令从文件 ~/.bash_logout,如果它存在.当不是登录 shell 的交互式 shell 是开始,bash 从 ~/.bashrc 读取并执行命令,如果该文件存在.这可以通过使用--norc 选项.--rcfile 文件选项将强制使用 bash从文件中读取和执行命令而不是~/.bashrc.当 bash 以非交互方式启动时,运行一个 shell例如,它在脚本中查找变量 BASH_ENV环境,如果它出现在那里,它的价值就会扩大,并使用扩展值作为要读取的文件名并执行.Bash 的行为就像下面的命令被处决:如果 [ -n "$BASH_ENV" ];然后 ."$BASH_ENV";菲但 PATH 变量的值不用于搜索为文件名.I have a command that runs fine if I ssh to a machine and run it, but fails when I try to run it using a remote ssh command like :
ssh user@IP <command>
Comparing the output of "env" using both methods resutls in different environments. When I manually login to the machine and run env, I get much more environment variables then when I run :
ssh user@IP "env"
Any idea why ?
There are different types of shells. The SSH command execution shell is a non-interactive shell, whereas your normal shell is either a login shell or an interactive shell. Description follows, from man bash:
A login shell is one whose first character of argument zero is a -, or one started with the --login option. An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state. The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section. When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behav ior. When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists. When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc. When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file name.
这篇关于为什么 SSH 远程命令在手动运行时获得的环境变量更少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!