简体   繁体   English

如何防止每次在Windows上使用Git Bash时都要输入密码来解密私钥?

[英]How to prevent that the password to decrypt the private key has to be entered every time when using Git Bash on Windows?

I've an automatic building service which download from a git private repository. 我有一个自动构建服务,可以从git私有存储库下载。 The problem is that when it tries to clone repository it need to provide the password, because it is not remembered; 问题是,当它试图克隆存储库时,它需要提供密码,因为它不会被记住; so because there is no human interaction, it waits forever the password. 因为没有人工交互,它会永远等待密码。 How can I force it to remember from id_rsa.pub? 如何从id_rsa.pub强制记住它?

For Windows users, just a note that this is how I set up the Git Bash environment to log me in once when I start it up. 对于Windows用户,只需注意这是我设置Git Bash环境以便在启动时登录一次的方法 I edit my ~/.bashrc file: 我编辑~/.bashrc文件:

eval `ssh-agent`
ssh-add

So when I start Git Bash, it looks like: 所以当我启动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)

And now I can ssh to other servers without logging in every time. 现在我可以ssh到其他服务器而无需每次都登录。

This answer explains how to get the GitHub username and password to be stored permanently, not the SSH key passphrase. 这个答案解释了如何永久存储GitHub用户名和密码,而不是SSH密钥密码。

In Windows, just run 在Windows中,只需运行

$ git config --global credential.helper wincred

This means that the next time you push, you'll enter your username and password as usual, but they'll be saved in Windows credentials. 这意味着下次推送时,您将像往常一样输入用户名和密码,但它们将保存在Windows凭据中。 You won't have to enter them again, after that. 之后你不必再输入它们了。

As in, Push to GitHub without entering username and password every time (Git Bash on Windows) . 如同在每次推送到GitHub而不输入用户名和密码(Windows上的Git Bash)

I prefer not to have to type my SSH passphrase when opening new terminals; 打开新终端时,我不想输入我的SSH密码; unfortunately starmonkey's solution requires the password to be typed in for every session. 不幸的是, starmonkey的解决方案需要为每个会话输入密码。 Instead, I have this in my .bash_profile file: 相反,我在.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

This will remember my passphrase for new terminal sessions as well; 这将记住我的新终端会话的密码; I only have to type it in once when I open my first terminal after booting. 当我在启动后打开第一个终端时,我只需要输入一次。

I'd like to credit where I got this; 我想相信我得到了这个; it's a modification of somebody else's work, but I can't remember where it came from. 这是对其他人工作的修改,但我不记得它来自哪里。 Thanks anonymous author! 谢谢匿名作者!

Update 2019-07-01: I don't think all this is necessary. 更新2019-07-01:我不认为这一切都是必要的。 I now consistently have this working by ensuring my .bash_profile file runs the ssh-agent: 我现在一直通过确保我的.bash_profile文件运行ssh-agent来实现这一点:

eval $(ssh-agent)

Then I set up an ssh configuration file like this: 然后我设置一个像这样的ssh配置文件:

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

If I understand the question correctly, you're already using an authorized SSH key in the build service, but you want to avoid having to type the passphrase for every clone? 如果我正确理解了这个问题,那么您已经在构建服务中使用了授权的SSH密钥,但是您希望避免为每个克隆键入密码?

I can think of two ways of doing this: 我可以想到两种方法:

  1. If your build service is being started interactively: Before you start the build service, start ssh-agent with a sufficiently long timeout ( -t option). 如果以交互方式启动构建服务:在启动构建服务之前,请使用足够长的超时( -t选项)启动ssh-agent Then use ssh-add (msysGit should have those) to add all the private keys you need before you start your build service. 然后使用ssh-add (msysGit应该有这些)在开始构建服务之前添加所需的所有私钥。 You'd still have to type out all the passphrases, but only once per service launch. 您仍然需要键入所有密码,但每次服务启动只需输入一次。

  2. If you want to avoid having to type the passphrases out at all, you can always remove the passphrases from the SSH keys, as described in https://serverfault.com/questions/50775/how-do-i-change-my-private-key-passphrase , by setting an empty new passphrase. 如果您想避免必须输出密码短语,您可以随时从SSH密钥中删除密码短语,如https://serverfault.com/questions/50775/how-do-i-change-my-中所述private-key-passphrase ,通过设置一个空的新密码。 This should do away with the password prompt entirely, but it is even less secure than the previous option. 这应该完全取消密码提示,但它比以前的选项更不安全。

When I tried to push my code, I got the below error: 当我试图推送我的代码时,我收到以下错误:

$ 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

After a few hours of research, I found I need to use the below command: 经过几个小时的研究,我发现我需要使用以下命令:

$ git config --global credential.helper cache

After executing the above command, I got the prompt for entering my GitHub username and password. 执行上述命令后,我收到输入GitHub用户名和密码的提示。 After providing the correct credentials, I am able to push my code. 提供正确的凭据后,我可以推送我的代码。

The right solution is: 正确的解决方案是:

  1. Run the Windows default terminal - cmd and get the directory of your master profile 运行Windows默认终端 - cmd并获取主配置文件的目录

     echo %USERPROFILE% 
  2. Run Git Bash in the directory above and create the .bashrc file with the command 在上面的目录中运行Git Bash并使用该命令创建.bashrc文件

     echo "" > .bashrc 
  3. Open the .bashrc file with your favourite text editor and paste code from GitHub Help into that file: 使用您喜欢的文本编辑器打开.bashrc文件,并将GitHub帮助中的代码粘贴到该文件中:

     env=~/.ssh/agent.env ... COPY WHOLE CODE FROM URL - I can't add it to Stack Overflow because it breaks layout... OMG! 
  4. Restart Git Bash and it asks you for your password (only first time) and done. 重启Git Bash,它会询问您的密码(仅限第一次)并完成。 No password bothering again. 没有密码再次困扰。

You need to create the authorized_keys file under the .ssh folder of the user under which you are going to connect to the repository server. 您需要在要连接到存储库服务器的用户的.ssh文件夹下创建authorized_keys文件。 For example, assuming you use username buildservice on repo.server you can run: 例如,假设您在repo.server上使用用户名buildservice ,您可以运行:

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

Then you have to check the following things: 然后你必须检查以下事项:

  1. That corresponding id_rsa private key is presented in builservice@build.server:~/.shh/id_rsa . 相应的id_rsa私钥在builservice@build.server:~/.shh/id_rsa

  2. That fingerprint of repo.server is stored in the buildservice@build.server:~/.ssh/known_hosts file. repo.server的指纹存储在buildservice@build.server:~/.ssh/known_hosts文件中。 Typically that will be done after the first attempt of ssh to connect to the repo.server . 通常,这将在首次尝试ssh连接到repo.server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM