SSH密钥要求在启动代理后输入密码

时间:2016-07-12 01:05:33

标签: git bash ssh ssh-keys

我在Bitbucket上有2个不同帐户的2个ssh密钥。

以下是我的bachrc文件:

# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.

env=$HOME/.ssh/agent.env

# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).

agent_is_running() {
    if [ "$SSH_AUTH_SOCK" ]; then
        # ssh-add returns:
        #   0 = agent running, has keys
        #   1 = agent running, no keys
        #   2 = agent not running
        ssh-add ~/.ssh/id_rsa ~/.ssh/id_rsa_2 -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
        false
    fi
}

agent_has_keys() {
    ssh-add -l >/dev/null 2>&1
}

agent_load_env() {
    . "$env" >/dev/null
}

agent_start() {
    (umask 077; ssh-agent >"$env")
    . "$env" >/dev/null
}

if ! agent_is_running; then
    agent_load_env
fi

# if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
    agent_start
    ssh-add ~/.ssh/id_rsa ~/.ssh/id_rsa_2
elif ! agent_has_keys; then
    ssh-add ~/.ssh/id_rsa ~/.ssh/id_rsa_2
fi

unset env

agent_stop() {
    if [ ${SSH_AGENT_PID+1} == 1 ]; then
        ssh-add -D
        ssh-agent -k > /dev/null 2>&1
        unset SSH_AGENT_PID
        unset SSH_AUTH_SOCK
    fi
}

agent_stop

为什么我要调用agent_stop?

用于测试的。我发现当调用agent_stop时,所有ssh密钥都从代理中删除,然后当再次打开git bash时,代理会自动添加ssh密钥。

但只有id_rsa不会提示密码,每次都会提示id_rsa_2。

P / S:id_rsa_2的公钥被添加到Bitbucket上。

我对此缺少什么?

2 个答案:

答案 0 :(得分:1)

注意:如果生成没有密码短语的ssh密钥

cd
ssh-keygen -t rsa -f ".ssh/mykey" -C "key for xxx acess" -q -P ""

然后你根本不需要ssh-agent

答案 1 :(得分:0)

  

当您运行ssh-keygen时,只需一步就可以询问您是否需要   密码,你可以点击输入跳过或输入密码,如果我的话   记忆正确地为我服务

来自gzh