windows Plink 在连接时不提供 bashrc 或 bash_profile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3407386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:58:32  来源:igfitidea点击:

Plink does not source bashrc or bash_profile on connect

windowslinuxsshplink

提问by Charles

I am trying to use plink as an ssh alternative on windows, but I am finding that when plink connects to a remote linux machine, it does not source .bash_profile or .bashrc.
Is there a different dot file I should create? Or is there another option?

我试图在 Windows 上使用 plink 作为 ssh 替代方案,但我发现当 plink 连接到远程 linux 机器时,它不会提供 .bash_profile 或 .bashrc。
我应该创建一个不同的点文件吗?或者还有其他选择吗?

For example, my bashrc file adds a directory to my path. This directory contains extra programs that I want to use, one being python.

例如,我的 bashrc 文件在我的路径中添加了一个目录。这个目录包含我想使用的额外程序,一个是 python。

This will not work:

这将不起作用:

plink host python

Where as this will:

这将在哪里:

plink host "source .bashrc;python"

When I use plink without a command parameter, it sources .bash_profile and everything works fine, but it appear that by merely sending a command plink will not source either file.

当我在没有命令参数的情况下使用 plink 时,它会获取 .bash_profile 并且一切正常,但似乎仅通过发送命令 plink 不会获取任何文件。

Is there a workaround?

有解决方法吗?

采纳答案by Kurt Pfeifle

If you simply connect to a remote host via ssh or plink, it will start the login account's default shell. If that shell is bash, bash will automatically source .bash_profile.

如果您只是通过 ssh 或 plink 连接到远程主机,它将启动登录帐户的默认 shell。如果该 shell 是 bash,bash 将自动获取 .bash_profile。

If you connect to a remote host via ssh or plink asking for a command to be executed, ssh will try to execute just that command.

如果您通过 ssh 或 plink 连接到远程主机要求执行命令,ssh 将尝试仅执行该命令。

What you want to achieve can be done by using a ForcedCommand. See also here:

您可以通过使用ForcedCommand来实现您想要实现的目标。另见此处:

Set the forced command to be a script that does 2 things:

将强制命令设置为执行两件事的脚本:

  1. source the .bash_profile
  2. run original command (env vars $SSH_ORIGINAL_COMMAND or $SSH2_ORIGINAL_COMMAND)
  1. 来源 .bash_profile
  2. 运行原始命令(环境变量 $SSH_ORIGINAL_COMMAND 或 $SSH2_ORIGINAL_COMMAND)

回答by Spalteer

The accepted answer helped me solve the same problem using plink. Here is a bit more detail that may help people in future:

接受的答案帮助我使用 plink 解决了同样的问题。这里有更多细节可以帮助人们在未来:

When a ssh connection is made to run a single command using plink, bash is not invoked as an "interactive login shell", so it doesn't run /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile (see the bash manual pages).

当建立 ssh 连接以使用 plink 运行单个命令时,bash 不会作为“交互式登录 shell”调用,因此它不会运行 /etc/profile、~/.bash_profile、~/.bash_login 或 ~/ .profile(请参阅 bash 手册页)。

For my purposes, I needed ~/.profile to run prior to the command passed in the plink command line.

出于我的目的,我需要 ~/.profile 在 plink 命令行中传递的命令之前运行。

A forced command can be added to the authorized_keys file for that key (see the sshd manual pages). A forced command (e.g. to run ~/.profile) stops it running the command specified by plink, so to get it to do both, the forced command should be to execute a script that runs .profile then the original plink command. The latter is stored in an environment variable $SSH_ORIGINAL_COMMAND so your script can do

可以将强制命令添加到该密钥的 authorized_keys 文件中(请参阅 sshd 手册页)。强制命令(例如运行 ~/.profile)会停止它运行 plink 指定的命令,因此要使其同时执行这两项操作,强制命令应该是执行运行 .profile 的脚本,然后是原始 plink 命令。后者存储在环境变量 $SSH_ORIGINAL_COMMAND 中,因此您的脚本可以执行

source .profile
$SSH_ORIGINAL_COMMAND

and you specify the script in the ~/.ssh/authorized_keys file as follows, before the key, on the same line:

并在 ~/.ssh/authorized_keys 文件中指定脚本,如下所示,在密钥之前,在同一行:

command="source forced_command.script" ssh-rsa A3AABzaC1yc...