让 ssh-agent 从 Windows 命令 shell 与 git run 一起使用
我安装了 msysgit 和 OpenSSH。我正在连接到 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
我还使用 git 扩展,它从 Windows 命令提示符运行 git 命令,而不是 git bash。因此,ssh 看不到正在运行的 ssh-agent。有可能解决这个问题吗?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我遇到了和你一样的问题,然后我尝试将此代码添加
到我的主目录中的文件
.bashrc
中。它有效!I had the same problem as you, then I tried adding this code
into file
.bashrc
in my home directory. And it works!对于 msysgit,您可能需要稍微修改 https:// 提供的解决方案help.github.com/articles/working-with-ssh-key-passphrases
正如您可能注意到的,我所做的唯一更改是在 ps 调用中,因为 msysgit 不使用 -U 而是 -u
For msysgit you might have to modify a bit the solution offered by https://help.github.com/articles/working-with-ssh-key-passphrases
As you may notice the only change I did was in the ps call, since msysgit don't use -U but -u
即使您可能已经解决了这个问题...使用
eval
命令使ssh_agent
进程保持不变:然后使用 ssh-add 添加您需要的密钥。
Even though you've probably solved it... use the
eval
command to make thessh_agent
process stick:Then use ssh-add to add the keys you need.
在 Windows 10 上,这对我有用,
touch ~/.profile
start ~/.profile
打开.profile
这是基于这个答案。唯一的区别是
.bashrc
不起作用,而.profile
起作用。On Windows 10 this worked for me
touch ~/.profile
start ~/.profile
to open.profile
.profile
This is based on this answer. The only difference is that
.bashrc
did not work, instead.profile
worked.我发现实现这一目标的最顺利方法是使用 Pageant 作为 SSH 代理和 plink。
您需要为远程中使用的主机名配置 putty 会话。
您还需要 plink.exe,它可以从与 putty 相同的站点下载。
并且您需要在加载密钥的情况下运行选美比赛。我的启动文件夹中有一个 pageant 的快捷方式,当我登录时,它会加载我的 SSH 密钥。
当您安装 git-scm 时,您可以指定它使用 tortoise/plink 而不是 OpenSSH。
最终效果是您可以随时打开 git-bash 并进行推送/拉取,而无需输入密码。
当选美比赛加载了您的密钥时,同样适用于 putty 和 WinSCP 会话。它让生活变得更加轻松(而且安全)。
I found the smoothest way to achieve this was using Pageant as the SSH agent and 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.
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.
When you install git-scm you can then specify it to use tortoise/plink rather than OpenSSH.
The net effect is you can open git-bash whenever you like and push/pull without being challenged for passphrases.
Same applies with putty and WinSCP sessions when pageant has your key loaded. It makes life a hell of a lot easier (and secure).
您可以使用源
.profile
的脚本包装 git 可执行文件,从而加载ssh-agent
环境变量。将名为
git
的脚本放在路径中比真实 git 更早的目录中,或者配置 git 扩展以调用包装器来代替真实 git。You could wrap your git executable with a script that sources your
.profile
, causing thessh-agent
environment variables to be loaded.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.来自这个答案的简单的两个字符串解决方案:
Simple two string solution from this answer: