让 ssh-agent 从 Windows 命令 shell 与 git run 一起使用

发布于 2024-09-18 10:15:05 字数 930 浏览 11 评论 0原文

我安装了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安稳善良 2024-09-25 10:15:05

我遇到了和你一样的问题,然后我尝试将此代码添加

#! /bin/bash 
eval `ssh-agent -s` 
ssh-add ~/.ssh/*_rsa

到我的主目录中的文件 .bashrc 中。它有效!

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!

笔芯 2024-09-25 10:15:05

对于 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

正如您可能注意到的,我所做的唯一更改是在 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

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

愿得七秒忆 2024-09-25 10:15:05

即使您可能已经解决了这个问题...使用 eval 命令使 ssh_agent 进程保持不变:

eval `ssh-agent.exe`

然后使用 ssh-add 添加您需要的密钥。

Even though you've probably solved it... use the eval command to make the ssh_agent process stick:

eval `ssh-agent.exe`

Then use ssh-add to add the keys you need.

梦中的蝴蝶 2024-09-25 10:15:05

在 Windows 10 上,这对我有用,

  1. 运行 git bash
  2. touch ~/.profile
  3. start ~/.profile 打开 .profile
  4. 将以下内容添加到 < code>.profile
#! /bin/bash 
eval `ssh-agent -s` 
ssh-add ~/.ssh/*_rsa

这是基于这个答案。唯一的区别是 .bashrc 不起作用,而 .profile 起作用。

On Windows 10 this worked for me

  1. run git bash
  2. touch ~/.profile
  3. start ~/.profile to open .profile
  4. add the following to .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.

○愚か者の日 2024-09-25 10:15:05

我发现实现这一目标的最顺利方法是使用 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).

流年里的时光 2024-09-25 10:15:05

您可以使用源 .profile 的脚本包装 git 可执行文件,从而加载 ssh-agent 环境变量。

将名为 git 的脚本放在路径中比真实 git 更早的目录中,或者配置 git 扩展以调用包装器来代替真实 git。

You could wrap your git executable with a script that sources your .profile, causing the ssh-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.

梦回旧景 2024-09-25 10:15:05

来自这个答案简单的两个字符串解决方案

# ~/.profile
if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
. ~/.ssh-agent.sh

Simple two string solution from this answer:

# ~/.profile
if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
. ~/.ssh-agent.sh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文