让 ssh-agent 与 git run 从 Windows 命令外壳一起工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3669001/
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
Getting ssh-agent to work with git run from windows command shell
提问by Hymano
I have msysgit installed, with OpenSSH. I am connecting to a gitosis repo. From the git bash, I have created a .profile
file that runs ssh-agent (if not already running) each time git bash is opened, using this script
我已经使用 OpenSSH 安装了 msysgit。我正在连接到 gitosis 存储库。从 git bash 中,我创建了一个.profile
文件,每次打开 git bash 时都会运行 ssh-agent(如果尚未运行),使用此脚本
SSH_ENV=$HOME/.ssh/environment
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
echo succeeded
chmod 600 ${SSH_ENV}
. ${SSH_ENV} > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. ${SSH_ENV} > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
I am also using git extensions, which runs the git command from the Windows command prompt, not git bash. So, ssh doesn't see the ssh-agent that is running. Is it possible to fix this?
我也在使用 git 扩展,它从 Windows 命令提示符运行 git 命令,而不是 git bash。因此,ssh 看不到正在运行的 ssh-agent。有没有可能解决这个问题?
回答by Fery Wardiyanto
I had the same problem as you, then I tried adding this code
我和你有同样的问题,然后我尝试添加此代码
#! /bin/bash
eval `ssh-agent -s`
ssh-add ~/.ssh/*_rsa
into file .bashrc
in my home directory. And it works!
进入.bashrc
我的主目录中的文件。它有效!
回答by Braiam
For msysgit you might have to modify a bit the solution offered by https://help.github.com/articles/working-with-ssh-key-passphrases
对于 msysgit,您可能需要稍微修改https://help.github.com/articles/working-with-ssh-key-passphrases提供的解决方案
declare -x SSH_ENV="$HOME/.ssh/environment"
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
echo succeeded
chmod 600 "$SSH_ENV"
. "$SSH_ENV" > /dev/null
ssh-add
}
# test for identities
function test_identities {
# test whether standard identities have been added to the agent already
ssh-add -l | grep "The agent has no identities" > /dev/null
if [ $? -eq 0 ]; then
ssh-add
# $SSH_AUTH_SOCK broken so we start a new proper agent
if [ $? -eq 2 ];then
start_agent
fi
fi
}
# check for running ssh-agent with proper $SSH_AGENT_PID
if [ -n "$SSH_AGENT_PID" ]; then
ps -f -u $USERNAME | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
fi
else
if [ -f "$SSH_ENV" ]; then
. "$SSH_ENV" > /dev/null
fi
ps -f -u $USERNAME | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
else
start_agent
fi
fi
As you may notice the only change I did was in the ps call, since msysgit don't use -U but -u
您可能会注意到我所做的唯一更改是在 ps 调用中,因为 msysgit 不使用 -U 而是 -u
回答by Cu7l4ss
Even though you've probably solved it... use the eval
command to make the ssh_agent
process stick:
即使你可能已经解决了它......使用eval
命令使ssh_agent
过程保持不变:
eval `ssh-agent.exe`
Then use ssh-add to add the keys you need.
然后使用 ssh-add 添加您需要的密钥。
回答by Thoran
On Windows 10 this worked for me
在 Windows 10 上,这对我有用
- run git bash
touch ~/.profile
start ~/.profile
to open.profile
- add the following to
.profile
- 运行 git bash
touch ~/.profile
start ~/.profile
打开.profile
- 将以下内容添加到
.profile
#! /bin/bash
eval `ssh-agent -s`
ssh-add ~/.ssh/*_rsa
This is based on this answer. The only difference is that .bashrc
did not work, instead .profile
worked.
这是基于这个答案。唯一的区别是.bashrc
没有奏效,而是.profile
奏效了。
回答by Alasdair
I found the smoothest way to achieve this was using Pageant as the SSH agent and plink.
我发现实现这一目标的最顺畅方法是使用 Pageant 作为 SSH 代理和 plink。
You need to have a putty session configured for the hostname that is used in your remote.
您需要为远程中使用的主机名配置一个腻子会话。
You will also need plink.exe which can be downloaded from the same site as putty.
您还需要 plink.exe,它可以从 putty 所在的站点下载。
And you need Pageant running with your key loaded. I have a shortcut to pageant in my startup folder that loads my SSH key when I log in.
并且您需要在加载密钥的情况下运行 Pageant。我的启动文件夹中有一个选美的快捷方式,可以在我登录时加载我的 SSH 密钥。
When you install git-scm you can then specify it to use tortoise/plink rather than OpenSSH.
当您安装 git-scm 时,您可以指定它使用 tortoise/plink 而不是 OpenSSH。
The net effect is you can open git-bash whenever you like and push/pull without being challenged for passphrases.
最终效果是您可以随时打开 git-bash 并推/拉,而不会受到密码短语的挑战。
Same applies with putty and WinSCP sessions when pageant has your key loaded. It makes life a hell of a lot easier (and secure).
Same applies with putty and WinSCP sessions when pageant has your key loaded. 它使生活变得更加轻松(和安全)。
回答by Andrew Aylett
You could wrap your git executable with a script that sources your .profile
, causing the ssh-agent
environment variables to be loaded.
您可以使用一个脚本来包装您的 git 可执行文件,该脚本是您的 .git 的来源.profile
,从而导致ssh-agent
加载环境变量。
Either put a script called git
in a directory earlier in your path than the real git, or configure the git extensions to call your wrapper in place of the real git.
要么git
在您的路径中比真正的 git 更早的目录中放置一个脚本,要么配置 git 扩展以调用您的包装器来代替真正的 git。
回答by oklas
Simple two string solutionfrom this answer:
来自这个答案的简单的两个字符串解决方案:
# ~/.profile
if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
. ~/.ssh-agent.sh