为什么 SSH 远程命令获得的环境变量比手动运行时少? [关闭]

2024-05-09

如果我 ssh 到计算机并运行它,我有一个命令可以正常运行,但当我尝试使用远程 ssh 命令运行它时会失败,例如:

ssh user@IP <command>

使用两种方法比较“env”的输出会导致不同的环境。当我手动登录到机器并运行 env 时,我得到的环境变量比运行时多得多:

ssh user@IP "env"

知道为什么吗?


有不同类型的贝壳。 SSH 命令执行 shell 是非交互式 shell,而普通 shell 是登录 shell 或交互式 shell。描述如下,来自 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.

  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 SSH 远程命令获得的环境变量比手动运行时少? [关闭] 的相关文章

  • 如何为函数启用 bash 自动完成功能?

    我已经阅读了关于 bash 自动完成的教程bash补全简介 http www debian administration org article 316 An introduction to bash completion part 1我正
  • SQLPLUS 保存到文件

    我必须为我的数据库类使用 SQLPLUS 我们的第一个作业是简单的保存 我按照说明进行操作 我正在使用 PuTTY 访问 sqlplus 在本练习中使用以下 SQL 命令 并尝试使用 SAVE 和 SPOOL 命令将 SQL 命令和输出保存
  • Jenkins Git 参数插件无法获取标签

    詹金斯版本 1 593 Git 参数插件 0 4 0 GIT 客户端插件 1 16 1 我使用私有 git 存储库 可以通过 ssh 访问 我的构建是参数化的 git参数是TAG TO BUILD 要构建的分支是refs tags TAG
  • 无法验证主机真实性

    我第一次做git pull 在git中提示无法确定主机的真实性 与我生成的私钥相比 RSA密钥指纹不正确 我只是想知道如何更新它以便它读取正确的文件以及为什么它首先读取错误的指纹 我遇到了类似的问题 这意味着远程主机未知 不受信任 修复方法
  • 如何配置 Ansible 跳过两个堡垒主机?

    我想编写一个 Ansible 剧本 使用 Ansible 2 7 5 它将在到达目标服务器之前跳过两个主机来执行诸如安装 docker 和 python 等操作 我能够让 Ansible 通过一台主机跳转到server1通过将其添加到我的h
  • 尝试 SSH 时设备的 ioctl 不合适

    我正在尝试通过 SSH 连接几台服务器并尝试获取sudo l每个服务器的输出 下面是我正在执行的脚本 bin bash serverlist tmp servers while IFS read r server netgroup user
  • envsubst 不能进行就地替换吗?

    我有一个配置文件 其中包含一些 ENV VARIABLE 样式的变量 This is my file It might contain EXAMPLES of text 现在我希望将该变量替换为保存在实际环境变量中的值 所以我正在尝试这个
  • gitlab runner ssh私钥644文件权限错误

    运行 gitlab ci cd 管道时 ssh 给出 0644 badpermission 错误 变量作为文件类型存储在 gitlab 的 settings gt variable 部分中 gitlab ci yml 文件如下所示 stag
  • PHP shell_exec 使用 ssh 运行 shell 脚本

    我有一个 shell 脚本 使用 ssh 和密钥连接到另一台机器 因此它不需要用户名和密码 当我从命令行运行这个脚本时 它工作正常 但是当我从 php shell exec 运行这个脚本时 它不起作用 如果我与 PHP 建立 ssh 连接并
  • 是否可以使用环境变量来为基于 log4net 的日志系统指定所需的日志级别?

    我们使用 log4net 进行日志记录 我想知道在指定日志级别时是否可以使用环境变量 动机很简单 我们的 QA 人员不必编辑配置文件 他们只需设置一次特定的环境变量 简而言之 我们不想将配置文件中的日志级别设置为某个恒定值 因为相同的构建由
  • 我应该在 Heroku 上的哪里存储敏感文件?

    这个问题可能有一个明显的答案 只是我没有想到 我知道敏感数据 例如安全凭证 最好通过 Heroku 的 CLI 使用环境变量存储在 Heroku 等服务上heroku config add 但敏感的怎么办files 比如证书 具体来说 我想
  • 如何在 Windows 10 中将文件夹添加到“Path”环境变量(带有屏幕截图)

    在 StackOverflow 和整个网络上 关于如何将特定文件夹添加到 Windows 10 的指南已经过时且很少Path用户的环境变量 我认为针对新开发人员的完整指南 包含分步说明和屏幕截图 对于帮助他们从命令提示符 https upl
  • GoLang ssh:尽管将其设置为 nil,但仍出现“必须指定 HosKeyCallback”错误

    我正在尝试使用 GoLang 连接到远程服务器 在客户端配置中 除了用户和密码之外 我将 HostKeyCallback 设置为 nil 以便它接受每个主机 config ssh ClientConfig User user HostKey
  • launch.json 中不允许使用属性 env [VSCode]

    我所做的就是在 VS 中初始化一个模板 Azure Functions 项目 当我尝试通过设置运行配置环境变量时launch json VS直接警告我这是不 允许的 此外 即使当我尝试运行我的 ps1无论如何 对于 env 来说 它不起作用
  • 如何在 Jenkins 声明式管道中设置 PATH

    在 Jenkins 脚本化管道中 您可以像这样设置 PATH 环境变量 node git url https github com jglick simple maven project with tests git withEnv PAT
  • 使用 paramiko 运行 Sudo 命令

    我正在尝试执行sudo使用 python paramiko 在远程计算机上运行命令 我尝试了这段代码 import paramiko ssh paramiko SSHClient ssh set missing host key polic
  • Ansible bitbucket 克隆存储库配置 ssh 错误

    我之前发布过这个问题 但那里的答案不再有效 总之 当使用 Ansible 配置我的 vagrant box 时 在尝试使用 ssh 克隆我的 bitbucket 私有存储库时 我遇到了一个神秘的错误 该错误指出 权限被拒绝 公钥 然而 如果
  • Linux 中的动态环境变量?

    Linux 中是否可以通过某种方式拥有动态环境变量 我有一个网络服务器 网站遵循以下布局 site qa production 我想要一个环境变量 例如 APPLICATION ENV 当我在 qa 目录中时设置为 qa 当我在生产目录中时
  • ssh远程变量赋值?

    以下内容对我不起作用 ssh email protected cdn cgi l email protection k 5 echo k 它只是返回一个空行 如何在远程会话 ssh 上分配变量 Note 我的问题是not关于如何将本地变量传
  • 如何在 Eclipse .classpath 文件中使用环境变量?

    在 Windows 中 我有一个环境变量 EXTERNAL LIB ROOT 指向 C Program Files MyExternalLibRoot 在另一台计算机上 它可能指向 C ExternalLibs 在 ant build xm

随机推荐