【问题标题】:How to prevent that the password to decrypt the private key has to be entered every time when using Git Bash on Windows?如何防止每次在 Windows 上使用 Git Bash 时都必须输入解密私钥的密码?
【发布时间】:2011-08-09 07:28:58
【问题描述】:

我有一个从 git 私有存储库下载的自动构建服务。 问题是当它试图克隆存储库时,它需要提供密码,因为它不会被记住;所以因为没有人工交互,所以它永远等待密码。 如何强制它从 id_rsa.pub 中记住?

【问题讨论】:

    标签: windows git passwords ssh-keys


    【解决方案1】:

    对于 Windows 用户,请注意,这是我设置 Git Bash 环境以在启动时登录一次的方式。我编辑我的~/.bashrc 文件:

    eval `ssh-agent`
    ssh-add
    

    所以当我启动 Git Bash 时,它看起来像:

    Welcome to Git (version 1.7.8-preview20111206)
    (etc)
    Agent pid 3376
    Enter passphrase for /c/Users/starmonkey/.ssh/id_dsa:
    Identity added: /c/Users/starmonkey/.ssh/id_dsa (/c/Users/starmonkey/.ssh/id_dsa)
    

    现在我可以 ssh 到其他服务器,而无需每次都登录。

    【讨论】:

    • 如果你没有 ~/.bashrc 文件,那么只需创建一个新的文本文件(记事本或其他编辑器)并添加上面提到的两行 starmonkey。
    • '~' 指的是您的“主目录”。在 Windows 中,您可以通过打开命令外壳 (cmd) 并键入“echo %USERPROFILE%”来找到它。
    • 对我来说,这种语法不起作用。我不得不写eval $(ssh-agent)
    • 我的问题是我需要指定要使用的 ssh-add 文件。可能是因为我有多个并且没有使用默认名称。示例ssh-add ~/.ssh/myfile_rsa
    • copy > ~/.bashrc 在 git bash 中在 windows 中创建 bashrc 文件,忽略错误
    【解决方案2】:

    此答案说明了如何获取 GitHub 用户名和密码以永久存储,而不是 SSH 密钥密码。

    在 Windows 中,只需运行

    $ git config --global credential.helper wincred
    

    这意味着下次推送时,您将照常输入用户名和密码,但它们将保存在 Windows 凭据中。之后,您无需再次输入它们。

    如,Push to GitHub without entering username and password every time (Git Bash on Windows)

    【讨论】:

    • 在这个命令之后你需要输入你的用户名和密码。
    • 这与 SSH 密钥有关,还是仅与 HTTPS(用户名和密码)身份验证有关?问题似乎与使用 SSH 密钥有关。
    • 非常适合我,我想我正在使用 HTTPS(用户名和密码)身份验证。
    • 运行它根本没有输出。
    • @user9993 它不会。这是一个配置更改,因此虽然不会产生任何输出,但git 将根据配置指令集更改其行为。
    【解决方案3】:

    我不希望在打开新终端时不必输入我的 SSH 密码;不幸的是,starmonkey's solution 需要为每个会话输入密码。相反,我在我的.bash_profile 文件中有这个:

    # Note: ~/.ssh/environment should not be used, as it
    #       already has a different purpose in SSH.
    
    env=~/.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 -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
    elif ! agent_has_keys; then
        ssh-add
    fi
    
    unset env
    

    这也会记住我用于新终端会话的密码;我只需要在启动后打开第一个终端时输入一次。

    我想记下我是从哪里得到这个的;这是对别人作品的修改,但我不记得它来自哪里。感谢匿名作者!

    2019-07-01 更新:我认为这一切都没有必要。我现在通过确保我的.bash_profile 文件运行 ssh-agent 来始终如一地工作:

    eval $(ssh-agent)
    

    然后我像这样设置一个ssh 配置文件:

    touch ~/.ssh/config
    chmod 600 ~/.ssh/config
    echo 'AddKeysToAgent yes' >> ~/.ssh/config
    

    【讨论】:

    • 每次我仍然被要求输入密码。
    • @Ryan 希望我刚刚添加的更新能够解决您的问题。我在conan.is/blogging/clojure-on-windows.html 的博客文章中更新了信息,我自己也在stackoverflow.com/questions/52423626/… 提出了同样的问题
    • @Conan 更新的解决方案在每个 bash 会话中工作。在我关闭正在运行的 bash 会话并打开一个新会话后,系统再次提示我输入密码。
    【解决方案4】:

    如果我正确理解了这个问题,您已经在构建服务中使用了授权的 SSH 密钥,但您希望避免为每个克隆都输入密码?

    我可以想到两种方法:

    1. 如果您的构建服务以交互方式启动:在您启动构建服务之前,启动 ssh-agent 并设置足够长的超时时间(-t 选项)。然后使用ssh-add(msysGit 应该有这些)在启动构建服务之前添加您需要的所有私钥。您仍然需要输入所有密码,但每次服务启动只需输入一次。

    2. 如果您想完全避免输入密码,您可以随时从 SSH 密钥中删除密码,如 https://serverfault.com/questions/50775/how-do-i-change-my-private-key-passphrase 中所述,方法是设置一个空的 new密码。这应该完全取消密码提示,但它甚至比前一个选项更不安全。

    【讨论】:

    • 感谢第二个选项,我需要它。
    【解决方案5】:

    当我尝试推送我的代码时,出现以下错误:

    $ git push origin dev
    
    remote: Too many invalid password attempts. Try logging in through the website with your password.
    fatal: unable to access 'https://naushadqamar-1@bitbucket.org/xxxx/xxxx-api.git/': The requested URL returned error: 403
    

    经过几个小时的研究,我发现我需要使用以下命令:

    $ git config --global credential.helper cache
    

    执行上述命令后,我得到了输入我的 GitHub 用户名和密码的提示。提供正确的凭据后,我就可以推送我的代码了。

    【讨论】:

    • 这只是生成了“git: 'credential-cache' is not a git command。”然而,“git config --global credential.helper store”起作用了——这可能不是最好的,但我不得不使用 HTTPS 而不是我喜欢的 SSH,只是希望它能够工作。
    【解决方案6】:

    正确的解决方案是:

    1. 运行 Windows 默认终端 - cmd 并获取主配置文件的目录

      echo %USERPROFILE%
      
    2. 在上面的目录中运行 Git Bash 并使用命令创建.bashrc 文件

      echo "" > .bashrc
      
    3. 使用您喜欢的文本编辑器打开.bashrc 文件并将code from GitHub Help 粘贴到该文件中:

      env=~/.ssh/agent.env
      ...
      COPY WHOLE CODE FROM URL - I can't add it to Stack Overflow because it breaks layout... OMG!
      
    4. 重新启动 Git Bash,它会询问您的密码(仅第一次)并完成。再也不用密码了。

    【讨论】:

      【解决方案7】:

      您需要在您要连接到存储库服务器的用户的.ssh 文件夹下创建authorized_keys 文件。例如,假设您在 repo.server 上使用用户名 buildservice,您可以运行:

      cd ~buidservice
      mkdir ./ssh
      cat id_rsa.pub >> .ssh/authorized_keys
      

      那你要检查以下几点:

      1. 对应的id_rsa私钥在builservice@build.server:~/.shh/id_rsa中显示。

      2. repo.server 的指纹存储在buildservice@build.server:~/.ssh/known_hosts 文件中。通常这将在ssh 第一次尝试连接到repo.server 之后完成。

      【讨论】:

      • 我忘了说。在 Windows 下,您的主目录可能是 C:\Users\Username
      • 嗯.. Git bash 拥有一切,请仔细阅读问题的标题。同样从问题中我假设已经生成了密钥对(因为它被问到如何强制 repo 服务器记住 id_rsa.pub。)抱歉格式化。
      • 好的,以前从未听说过“git bash”。对不起'回合
      • 我假设这是使用 msysgit。我正在添加标签。
      • 警告!使用cat id_rsa.pub > .ssh/authorized_keys 将覆盖任何现有的授权密钥。使用>> 添加而不是覆盖。
      猜你喜欢
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2021-07-02
      • 2022-11-02
      • 1970-01-01
      • 2021-11-29
      相关资源
      最近更新 更多