Plink 在连接时不获取 bashrc 或 bash_profile
我尝试在 Windows 上使用 plink 作为 ssh 替代方案,但我发现当 plink 连接到远程 Linux 计算机时,它不会获取 .bash_profile 或 .bashrc。
我应该创建不同的点文件吗?或者还有其他选择吗?
例如,我的 bashrc 文件将一个目录添加到我的路径中。该目录包含我想要使用的额外程序,其中之一是 python。
这不起作用:
plink host python
当
plink host "source .bashrc;python"
我使用不带命令参数的 plink 时,它会获取 .bash_profile 并且一切正常,但看起来仅发送命令 plink 不会获取任何一个文件。
有解决方法吗?
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?
For example, my bashrc file adds a directory to my path. This directory contains extra programs that I want to use, one being 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.
Is there a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
接受的答案帮助我使用 plink 解决了同样的问题。
这里有一些更详细的信息,可能会对将来的人们有所帮助:
当使用 plink 建立 ssh 连接来运行单个命令时,bash 不会作为“交互式登录 shell”被调用,因此它不会运行 /etc/profile, ~/.bash_profile、~/.bash_login 或 ~/.profile(请参阅 bash 手册页)。
出于我的目的,我需要在 plink 命令行中传递命令之前运行 ~/.profile。
可以将强制命令添加到该密钥的authorized_keys 文件中(请参阅 sshd 手册页)。强制命令(例如运行 ~/.profile)会阻止它运行 plink 指定的命令,因此要让它同时执行这两项操作,强制命令应该是执行一个运行 .profile 的脚本,然后运行原始 plink 命令。后者存储在环境变量 $SSH_ORIGINAL_COMMAND 中,以便您的脚本可以执行此操作
,并且您可以在 ~/.ssh/authorized_keys 文件中指定脚本,如下所示,在同一行的密钥之前:
The accepted answer helped me solve the same problem using plink.
Here is a bit more detail that may help people in future:
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).
For my purposes, I needed ~/.profile to run prior to the command passed in the plink command line.
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
and you specify the script in the ~/.ssh/authorized_keys file as follows, before the key, on the same line:
如果您只是通过 ssh 或 plink 连接到远程主机,它将启动登录帐户的默认 shell。如果该 shell 是 bash,则 bash 将自动获取 .bash_profile。
如果您通过 ssh 或 plink 连接到远程主机并要求执行命令,则 ssh 将尝试仅执行该命令。
您想要实现的目标可以通过使用ForcedCommand来完成。另请参阅此处:
将强制命令设置为执行 2 件事的脚本:
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.
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.
What you want to achieve can be done by using a ForcedCommand. See also here:
Set the forced command to be a script that does 2 things: