简体   繁体   English

为什么 git 在 Windows 下记不住我的密码

[英]Why git can't remember my passphrase under Windows

I have just start using git and i can't get it to remember my passphrase I'm using cmd.exe elevated and my git host is github and i have create a ssh key like that guide on github我刚刚开始使用 git,但我无法记住我的密码我正在使用提升的 cmd.exe,我的 git 主机是 github,我已经创建了一个 ssh 密钥,就像 github 上的指南一样

but i still get但我仍然得到

*\subnus.mvc>git push origin master
Enter passphrase for key '/c/Users/Subnus/.ssh/id_rsa':

I realize that this question is coming up on two years old, but I had the same issue and several answers here did not completely answer the question for me.我意识到这个问题是在两年前提出的,但我遇到了同样的问题,这里的几个答案并没有完全回答我的问题。 Here are three step-by-step solutions, depending on whether you use TortoiseGit in addition to msysgit or not.以下是三个分步解决方案,具体取决于您是否使用 TortoiseGit 和 msysgit。

First solution Assumes Windows, msysgit, and PuTTY.第一个解决方案假设 Windows、msysgit 和 PuTTY。

  1. Install msysgit and PuTTY as instructed.按照说明安装 msysgit 和 PuTTY。

  2. (Optional) Add PuTTY to your path. (可选)将 PuTTY 添加到您的路径。 (If you do not do this, then any references to PuTTY commands below must be prefixed with the full path to the appropriate executable.) (如果您不这样做,那么下面对 PuTTY 命令的任何引用都必须以相应可执行文件的完整路径为前缀。)

  3. If you have not done so already, then generate a key hash as instructed at GitHub or as instructed by your Git host.如果您还没有这样做,请按照 GitHub 或您的 Git 主机的说明生成密钥哈希。

  4. Again, if you have not already done so, convert your key for use with PuTTY's pageant.exe using puttygen.exe .同样,如果您还没有这样做,请使用puttygen.exe转换您的密钥以与 PuTTY 的 pageant.exe 一起使用。 Instructions are in PuTTY's documentation, in this helpful guide , and several other places in cyberspace.说明在 PuTTY 的文档、这个有用的指南以及网络空间的其他几个地方。

  5. Run PuTTY's pageant.exe , open your .ppk file ("Add Key"), and provide your passphrase for your key.运行 PuTTY 的pageant.exe ,打开您的 .ppk 文件(“添加密钥”),并为您的密钥提供密码。

  6. Access Windows' environment variables dialog (Right-click on "Computer", Click on "Properties", Click on "Advanced system settings" or the "Advanced" tab, click on "Environment Variables").访问 Windows 的环境变量对话框(右键单击“计算机”,单击“属性”,单击“高级系统设置”或“高级”选项卡,单击“环境变量”)。 Add the following environment variable:添加以下环境变量:

    GIT_SSH=C:\full\path\to\plink.exe GIT_SSH=C:\full\path\to\plink.exe

    Replace "C:\full\path\to" with the full installation path to PuTTY, where plink.exe is found.将 "C:\full\path\to" 替换为 PuTTY 的完整安装路径,其中找到 plink.exe。 It is probably best to add it to the "User variables" section.最好将其添加到“用户变量”部分。 Also, make sure that the path you use to plink.exe matches the path you use for Pageant (pageant.exe).此外,请确保您用于 plink.exe 的路径与您用于 Pageant (pageant.exe) 的路径相匹配。 In some cases, you may have several installations of PuTTY because it might be installed along with other applications.在某些情况下,您可能安装了多个 PuTTY,因为它可能与其他应用程序一起安装。 Using plink.exe from one installation and pageant.exe from another will likely cause you trouble.使用一个安装中的 plink.exe 和另一个安装中的 pageant.exe 可能会给您带来麻烦。

  7. Open a command prompt.打开命令提示符。

  8. If you are trying to connect to a git repository hosted at Github.com then run the following command:如果您尝试连接到托管在 Github.com 的 git 存储库,请运行以下命令:

    plink.exe git@github.com plink.exe git@github.com

    If the git repository you are trying to connect to is hosted somewhere else, then replace git@github.com with an appropriate user name and URL.如果您尝试连接的 git 存储库托管在其他地方,则将git@github.com替换为适当的用户名和 URL。 (Assuming Github) You should be informed that the server's host key is not cached, and asked if you trust it. (假设 Github)你应该被告知服务器的主机密钥没有被缓存,并询问你是否信任它。 Answer with a y .回答y This will add the server's host key to PuTTY's list of known hosts.这会将服务器的主机密钥添加到 PuTTY 的已知主机列表中。 Without this step, git commands will not work properly.如果没有这一步,git 命令将无法正常工作。 After hitting enter, Github informs you that Github does not provide shell access.按回车后,Github 会通知您 Github 不提供 shell 访问。 That's fine...we don't need it.没关系……我们不需要它。 (If you are connecting to some other host, and it gives you shell access, it is probably best to terminate the link without doing anything else.) (如果您正在连接到其他主机,并且它为您提供了 shell 访问权限,那么最好终止链接而不做任何其他事情。)

  9. All done!全部完成! Git commands should now work from the command line. Git 命令现在应该可以从命令行运行。 You may want to have pageant.exe load your .ppk file automatically at boot time , depending on how often you'll be needing it.您可能希望 pageant.exe 在启动时自动加载您的 .ppk 文件,具体取决于您需要它的频率。

Second solution Assumes Windows, msysgit, and TortoiseGit.第二种解决方案假设 Windows、msysgit 和 TortoiseGit。

TortoiseGit comes with PuTTY executables and a specially modified version of plink (called TortoisePlink.exe) that will make things easier. TortoiseGit 带有 PuTTY 可执行文件和经过特殊修改的 plink 版本(称为 TortoisePlink.exe),这将使事情变得更容易。

  1. Install msysgit and TortoiseGit as instructed.按照说明安装 msysgit 和 TortoiseGit。

  2. If you have not done so already, then generate a key hash as instructed at GitHub or as instructed by your Git host.如果您还没有这样做,请按照 GitHub 或您的 Git 主机的说明生成密钥哈希。

  3. Again, if you have not already done so, convert your key for use with TortoiseGit's pageant.exe using TortoiseGit's puttygen.exe .同样,如果您还没有这样做,请使用 TortoiseGit 的puttygen.exe转换您的密钥以与 TortoiseGit 的 pageant.exe 一起使用。 Instructions are in PuTTY's documentation, in the helpful guide linked to in the first solution, and in several other places in cyberspace.说明在 PuTTY 的文档、第一个解决方案中链接的有用指南以及网络空间的其他几个地方。

  4. Run TortoiseGit's pageant.exe , open your .ppk file ("Add Key") and provide your passphrase for your key.运行 TortoiseGit 的pageant.exe ,打开您的 .ppk 文件(“添加密钥”)并为您的密钥提供密码。

  5. Access Windows' environment variables dialog (Right-click on "Computer", Click on "Properties", Click on "Advanced system settings" or the "Advanced" tab, click on "Environment Variables").访问 Windows 的环境变量对话框(右键单击“计算机”,单击“属性”,单击“高级系统设置”或“高级”选项卡,单击“环境变量”)。 Add the following environment variable:添加以下环境变量:

    GIT_SSH=C:\full\path\to\TortoisePlink.exe GIT_SSH=C:\full\path\to\TortoisePlink.exe

    Replace "C:\full\path\to" with the full installation path to TortoiseGit, where TortoisePlink.exe is found.将 "C:\full\path\to" 替换为 TortoiseGit 的完整安装路径,在该路径中找到 TortoisePlink.exe。 It is probably best to add it to the "User variables" section.最好将其添加到“用户变量”部分。 Also, make sure that the path you use to TortoisePlink.exe matches the path you use for Pageant (pageant.exe).此外,请确保您用于 TortoisePlink.exe 的路径与您用于 Pageant (pageant.exe) 的路径相匹配。 In some cases, you may have several installations of PuTTY because it might be installed along with other applications.在某些情况下,您可能安装了多个 PuTTY,因为它可能与其他应用程序一起安装。 Using TortoisePlink.exe from the TortoiseGit installation and pageant.exe from another installation of a different application (or from a standalone PuTTY installation) will likely cause you trouble.使用 TortoiseGit 安装中的 TortoisePlink.exe 和不同应用程序的另一个安装(或独立 PuTTY 安装)中的 pageant.exe 可能会给您带来麻烦。

  6. All done!全部完成! Git commands should now work from the command line. Git 命令现在应该可以从命令行运行。 The first time you try to connect to your git repository you will probably be informed that the server's host key is not cached, and asks if you trust the server.当您第一次尝试连接到您的 git 存储库时,您可能会被告知服务器的主机密钥未缓存,并询问您是否信任该服务器。 Click on "Yes".点击“是”。 (This is TortoisePlink.exe in action.) (这是运行中的 TortoisePlink.exe。)

    You may want to have pageant.exe load your .ppk file automatically at boot time , depending on how often you'll be needing it.您可能希望 pageant.exe 在启动时自动加载您的 .ppk 文件,具体取决于您需要它的频率。

Third solution Assumes Windows, msysgit, and the native command prompt.第三种解决方案假定 Windows、msysgit 和本机命令提示符。

  1. Install msysgit安装 msysgit
  2. Make sure to allow git to be used on the MS-DOS command prompt确保允许在 MS-DOS 命令提示符下使用 git
  3. Run start-ssh-agent运行start-ssh-agent
  4. Enter SSH passphrases输入 SSH 密码
  5. All done!全部完成! Git commands should now work in the native command prompt. Git 命令现在应该可以在本机命令提示符下工作。

Every time I set up a new desktop I forget these instructions, so I'm adding another answer here since I stumble across it equally often!每次设置新桌面时,我都会忘记这些说明,所以我在这里添加另一个答案,因为我同样经常偶然发现它!


Quick Steps for Impatient Users Like Me像我这样不耐烦的用户的快速步骤

  1. Enable the OpenSSH Authentication Agent service and make it start automatically.启用OpenSSH Authentication Agent服务并使其自动启动。
    • 👉 Update 👈 👉更新👈
    • With the latest Windows update Version 10.0.19042.867 I had to re-do this step!使用最新的 Windows 更新Version 10.0.19042.867我不得不重新执行此步骤!
  2. Add your SSH key to the agent with ssh-add on the command line.在命令行上使用ssh-add将您的 SSH 密钥添加到代理。
  3. Test git integration, if it still asks for your passphrase, continue on.测试 git 集成,如果它仍然要求您输入密码,请继续。
  4. Add the environment variable $ENV:GIT_SSH=C:\Windows\System32\OpenSSH\ssh.exe to your session, or permanently to your user environment.将环境变量$ENV:GIT_SSH=C:\Windows\System32\OpenSSH\ssh.exe到您的会话中,或永久添加到您的用户环境中。

Detailed Steps: Overview详细步骤:概述

Windows has been shipping with OpenSSH for some time now. Windows 与 OpenSSH 一起发布已经有一段时间了。 It includes all the necessary bits for ssh to work alongside Git, but it still seems to need some TLC before it works 100% seamlessly.它包括 ssh 与 Git 一起工作的所有必要位,但它似乎仍然需要一些 TLC 才能 100% 无缝工作。 Here's the steps I've been following with success as of Windows ver 10.0.18362.449 (you can see your Windows 10 version by opening a cmd.exe shell and typing ver ).这是我从 Windows 版本 10.0.18362.449 开始成功执行的步骤(您可以通过打开 cmd.exe shell 并键入ver来查看您的 Windows 10 版本)。

I assume here that you already have your SSH key setup, and is located at ~/.ssh/id_rsa我在这里假设您已经设置了 SSH 密钥,并且位于~/.ssh/id_rsa

Enable the ssh-agent service on your Windows 10 box.在您的 Windows 10 机器上启用 ssh-agent 服务。

  1. Start-> Type 'Services' and click on the Services App that appears.开始-> 输入“服务”并单击出现的服务应用程序。
  2. Find the OpenSSH Authentication Agent service in the list.在列表中找到OpenSSH Authentication Agent服务。
  3. Right-click on the OpenSSH Authentication Agent service, and choose 'Properties'.右键单击OpenSSH Authentication Agent服务,然后选择“属性”。
  4. Change the Startup type: to Automatic .Startup type:更改为Automatic
  5. Click the Start button to change the service status to Running .单击Start按钮将服务状态更改为Running
  6. Dismiss the dialog by clicking OK , and close the Services app.单击OK关闭对话框,然后关闭 Services 应用程序。

Add your key to the ssh-agent将您的密钥添加到ssh-agent

  1. Open your shell of preference (I'll use Windows Powershell in this example, applies to Powershell Core too) .打开您偏好的 shell (我将在本例中使用 Windows Powershell,也适用于 Powershell Core)
  2. Add your SSH key to the ssh-agent : ssh-add (you can add the path to your key as the first argument if it differs from the default) .将您的 SSH 密钥添加到ssh-agentssh-add (如果它与默认值不同,您可以将密钥的路径添加为第一个参数)
  3. Enter your passphrase if/when prompted to do so.如果/在提示时输入您的密码。

Try Git + SSH试试 Git + SSH

  1. Open your shell (again, I'm using Powershell) and clone a repo.打开你的 shell (同样,我使用的是 Powershell)并克隆一个 repo。 git clone git@github.com:octocat/Spoon-Knife
  2. If you see this prompt, continue on to the next section:如果您看到此提示,请继续下一部分:
Enter passphrase for key '/c/Users/your_user_name/.ssh/id_rsa':

Set your GIT_SSH Environment Variable设置您的GIT_SSH环境变量

In any session you can simply set this environment variable and the prompt for your passphrase will stop coming up and ssh will use the ssh-agent on your behalf.在任何会话中,您都可以简单地设置此环境变量,并且您的密码提示将停止出现,ssh 将代表您使用ssh-agent Alternatively, you can set your passphrase into your user's environment permanently.或者,您可以将密码永久设置到用户的环境中。

To set GIT_SSH in the current shell only:仅在当前 shell 中设置GIT_SSH

  1. Open your shell of preference.打开您喜欢的外壳。 (Powershell for me) (对我来说是Powershell)
  2. Set the environment variable GIT_SSH to the appropriate ssh.exe : $Env:GIT_SSH=$((Get-Command -Name ssh).Source)将环境变量 GIT_SSH 设置为适当的ssh.exe$Env:GIT_SSH=$((Get-Command -Name ssh).Source)
  3. Retry the steps in Try Git + SSH above.重试上面尝试 Git + SSH中的步骤。

To set GIT_SSH permanently永久设置GIT_SSH

  1. Open File Explorer.打开文件资源管理器。 Start-> type 'File Explorer' and click on it in the list.开始-> 键入“文件资源管理器”并在列表中单击它。
  2. Right-click 'This PC' and click on 'Properties'.右键单击“这台电脑”,然后单击“属性”。
  3. Click on 'Advanced system settings'.点击“高级系统设置”。
  4. Click the 'Environment Variables...' button.单击“环境变量...”按钮。
  5. Under 'User variables for your_user_name' click New...在“您的用户名的用户变量”下单击新建...
  6. Set Variable name: field to GIT_SSHVariable name:字段设置为 GIT_SSH
  7. Set the Variable value: field to path-to-ssh.exe (typically C:\Windows\System32\OpenSSH\ssh.exe ).Variable value:字段设置为 path-to-ssh.exe(通常为C:\Windows\System32\OpenSSH\ssh.exe )。
  8. Click OK to dismiss the New User Variable dialog.单击确定关闭新用户变量对话框。
  9. Click OK to dismiss the Environment Variables dialog.单击“确定”关闭“环境变量”对话框。
  10. Retry the steps in Try Git + SSH above.重试上面尝试 Git + SSH中的步骤。

Note that this is likely going to change with new steps/procedures as Windows 10 progresses and as I learn more.请注意,随着 Windows 10 的进展以及我了解更多,这可能会随着新的步骤/程序而改变。 I will attempt to keep this updated, I look forward to feedback in the comments.我会尽量保持更新,期待评论中的反馈。

In case you are using Git bash under Windows you can perform the following:如果您在Windows下使用Git bash ,您可以执行以下操作:

eval `ssh-agent -s`
ssh-add ~/.ssh/*_rsa

it will ask for pass phrase in the second command, and that's it.它会在第二个命令中询问密码,就是这样。 Each additional action you will need to do (which once required pass phrase) won't ask you for the pass phrase (see an example in the screen shot below):您需要执行的每个附加操作(曾经需要密码短语)都不会要求您输入密码短语(请参见下面屏幕截图中的示例):

在 Windows 上的 Git bash 中添加密码短语

对于需要更详细说明的任何人,请参阅此页面: http ://help.github.com/working-with-key-passphrases/

One extra solution 5 years, 8 months and 6 days after the question was posted wouldn't be a bad idea so here goes.问题发布后 5 年、8 个月和 6 天后的一个额外解决方案并不是一个坏主意,所以就这样吧。

NOTE: Assumes you are using a windows computer.注意:假设您使用的是 Windows 计算机。

  1. Download thegit-credential-winstore .下载git-credential-winstore
  2. Run it!运行! If you have GIT in your PATH environment variable, it should just work.如果您的PATH环境变量中有 GIT,它应该可以正常工作。 If you don't, run git-credential-winstore -i C:\Path\To\Git.exe .如果不这样做,请运行git-credential-winstore -i C:\Path\To\Git.exe

The next time you attempt to commit to a repository, you'll be prompted to enter your credentials.下次您尝试提交到存储库时,系统会提示您输入您的凭据。 That should be it.应该是这样的。 You will not be asked for your credentials any longer until you change your password.在您更改密码之前,不会再要求您提供凭据。


Just for your knowledge... Your credentials are stored in the Windows Credential Store仅供您参考...您的凭据存储在 Windows 凭据存储中

Where are you storing my credentials?你在哪里存储我的凭据?

This app just uses the existing Windows Credential Store to hold your credentials.此应用程序仅使用现有的 Windows 凭据存储来保存您的凭据。 You can see the stored credentials by going to Control Panel > User Accounts > Credential Manager and choosing "Windows Credentials".您可以通过转到控制面板 > 用户帐户 > 凭据管理器并选择“Windows 凭据”来查看存储的凭据。 The entries starting "git:" are from git-credential-winstore.以“git:”开头的条目来自 git-credential-winstore。

That's not git, it's ssh. 那不是git,而是ssh。

I don't do windows, but ssh has an agent concept that can remember passphrases for you. 我不使用Windows,但是ssh的代理概念可以为您记住密码短语。 OS X has this enabled by default. OS X默认情况下启用了此功能。 Your ssh client probably has a way to configure this. 您的ssh客户端可能有一种配置方式。

Let's assume you'd like to use a pure Git Bash only solution without using TortoiseGit or PuTTY.假设您想使用纯Git Bash解决方案而不使用 TortoiseGit 或 PuTTY。 Also, you don't want to store your passphrases permanently as it's almost the same as if you would've generated your SSH key without a passphrase in the first place.此外,您不想永久存储您的密码,因为这几乎与您在没有密码的情况下生成 SSH 密钥一样。 But you still want to use some caching.但是您仍然想使用一些缓存。

For caching purposes ssh-agent process is used, which is included with the Git Bash distribution.出于缓存目的,使用了ssh-agent进程,它包含在 Git Bash 发行版中。 This process isn't started by default, so it needs to be launched first.此过程默认不启动,因此需要先启动。 For any SSH keys to be cached they should be added to this process with ssh-add command which will prompt you for a key's passphrase and store it in memory.对于要缓存的任何 SSH 密钥,应使用ssh-add命令将它们添加到此进程中,该命令将提示您输入密钥的密码并将其存储在内存中。

Drawbacks of other solutions:其他解决方案的缺点:

  • Auto-launching ssh-agent like in GitHub's article asks for a passphrase right from the start when you launch Git Bash, regardless of whether you'll need to use your SSH key this session or not. GitHub 文章中的自动启动ssh-agent会在您启动 Git Bash 时从一开始就要求输入密码,无论您是否需要在此会话中使用 SSH 密钥。 If you're working with your local repo today you'll probably want to provide a passphrase only when really needed (eg when interacting with a remote repo).如果您今天使用本地存储库,您可能只想在真正需要时提供密码(例如,在与远程存储库交互时)。
  • If you launch your ssh-agent like in GitLab's article with eval $(ssh-agent -s) you're probably tired of typing that in each time.如果您像GitLab 的文章中那样使用eval $(ssh-agent -s)启动ssh-agent ,您可能已经厌倦了每次都输入它。 Chances are, eventually, you've added those two lines to your .bashrc config to auto-launch.最终,您可能已将这两行添加到.bashrc配置中以自动启动。 Downsides are the same as above plus an extra one: each time you launch a new Git Bash terminal you'll get an extra ssh-agent process (GitHub's bash script checks if that process has already started).缺点与上述相同,还有一个额外的:每次启动新的 Git Bash 终端时,您都会获得一个额外的 ssh-agent 进程(GitHub 的 bash 脚本检查该进程是否已经启动)。
  • Like the two above but especially so when you have separate SSH keys for different hosts, eg one for GitHub and another one for GitLab, so providing them all at once is annoying and inconvenient.就像上面两个一样,但尤其是当您为不同的主机拥有单独的 SSH 密钥时,例如一个用于 GitHub,另一个用于 GitLab,因此一次性提供它们既烦人又不方便。

So this solution is for those who wonder how to make Git Bash ask for a passphrase only once per Windows session and only when really needed.因此,此解决方案适用于那些想知道如何让 Git Bash 在每个 Windows 会话中仅在真正需要时才要求输入密码的人。 It resembles the behavior of passphrases management with GnuPG commits auto-signing using default-cache-ttl .它类似于 GnuPG 使用default-cache-ttl提交自动签名的密码管理行为。

Configuring SSH to ask for passphrases once, when needed, using Git Bash only配置 SSH 以在需要时仅使用 Git Bash 请求密码短语

  1. First, we want to auto-launch the ssh-agent when starting a Git Bash shell.首先,我们希望在启动 Git Bash shell 时自动启动ssh-agent We'll use a modified GitHub's script for that as it checks whether the process has already started, but we don't want it to ssh-add keys right away.我们将为此使用修改后的 GitHub 脚本,因为它会检查进程是否已经开始,但我们不希望它立即ssh-add密钥。 This script goes to your ~/.bashrc or ~/.profile or ~/.bash_profile ( ~ is your User's home directory like C:\Users\Username – run cd ~ and then pwd for the Git Bash to print it out):该脚本转到您的~/.bashrc~/.profile~/.bash_profile~是您的用户主目录,如C:\Users\Username – 运行cd ~然后pwd让 Git Bash 将其打印出来):

     ### Start ssh-agent env=~/.ssh/agent.env agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; } agent_start () { (umask 077; ssh-agent >| "$env") # use -t here for timeout . "$env" >| /dev/null ; } agent_load_env # agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?) if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then agent_start fi unset env
  2. Now edit or create a ~/.ssh/config file and add an AddKeysToAgent option for each host stanza you want caching to be turned on (you can also turn it on globally by placing the directive at the beginning of the file before all the host declarations):现在编辑或创建一个~/.ssh/config文件,并为要打开缓存的每个主机节添加一个AddKeysToAgent选项(您也可以通过将指令放在文件开头的所有主机之前来全局打开它声明):

     # GitHub.com Host github.com Preferredauthentications publickey IdentityFile ~/.ssh/id_ed25519_github AddKeysToAgent yes # GitLab.com Host gitlab.com Preferredauthentications publickey IdentityFile ~/.ssh/id_ed25519_gitlab AddKeysToAgent yes

    From ssh config man page : If this option is set to yes and a key is loaded from a file, the key and its passphrase are added to the agent with the default lifetime, as if by ssh-add(1).从 ssh config 手册如果此选项设置为 yes 并且从文件加载密钥,则密钥及其密码将添加到具有默认生命周期的代理,就像通过 ssh-add(1) 一样。

The default maximum lifetime is forever or until the ssh-agent process gets killed (either manually from task manager or when your PC is shut down).默认的最长生命周期是永远或直到ssh-agent进程被终止(从任务管理器手动或在您的 PC 关闭时)。 If you wish to use a finite timeout you can set it with ssh-agent's -t parameter.如果您希望使用有限超时,您可以使用 ssh-agent 的-t参数设置它。 Change the line in the bash script from the first step above, eg for 30 minutes key cache lifetime:从上面的第一步更改 bash 脚本中的行,例如 30 分钟的密钥缓存生命周期:

(umask 077; ssh-agent -t 30m >| "$env")

See here for other time format qualifiers.有关其他时间格式限定符,请参见此处

If you set a password for your key file, you'll always need to type in that password when connecting.如果您为密钥文件设置了密码,则在连接时始终需要输入该密码。 If you create a passwordless key, then you won't have to type it every time, however, anyone with access to your key file can now connect to your github account.如果您创建一个无密码密钥,那么您不必每次都输入它,但是,任何有权访问您的密钥文件的人现在都可以连接到您的 github 帐户。

ssh-agent may also work. ssh-agent 也可以工作。 Try running that and see if it will remember your passphrase.尝试运行它,看看它是否会记住您的密码。

[edit - misread the question, this is an answer to a related problem. [编辑-误读了问题,这是对相关问题的回答。 leaving rephrased version for posterity]为后代留下改写版本]

My case was that I was trying to push to a repo that was hosted on one of our servers.我的情况是,我试图推送到托管在我们其中一台服务器上的存储库。 Whenever I tried to do a push, git would ask me for my password (nb - password, not the passphrase to my private key).每当我尝试推送时,git 都会询问我的密码(nb - 密码,而不是我私钥的密码)。

By adding my public key to the authorised keys on the server, I was able to get password-free pushes to that server.通过将我的公钥添加到服务器上的授权密钥中,我能够获得对该服务器的无密码推送。 And, because there was no passphrase on my private key (which is bad practice btw!) I didn't need to type anything at all in.而且,因为我的私钥上没有密码(顺便说一句,这是不好的做法!)我根本不需要输入任何内容。

Here's the command to add your public key to a server.这是将您的公钥添加到服务器的命令。 It assumes the user git is the user on the server.它假定用户git是服务器上的用户。

cat .ssh/id_rsa.pub | ssh git@GIT_MASTER_IP 'cat >> .ssh/authorized_keys'

You could achieve the same thing by logging onto the server, and manually appending your public key to the file at ~/.ssh/authorized_keys您可以通过登录服务器并手动将您的公钥附加到~/.ssh/authorized_keys文件中来实现相同的目的

I realise this is several years overdue, but I stumbled across this question trying to find a solution for it, and I found something that suits all levels of expertise, so I thought I'd share.我意识到这已经过了好几年了,但我偶然发现了这个问题,试图找到一个解决方案,我找到了适合所有专业水平的东西,所以我想我会分享。

GitHub provide a very helpful installer that makes everything nice and easy: https://help.github.com/articles/caching-your-github-password-in-git/ GitHub 提供了一个非常有用的安装程序,让一切变得简单易用: https ://help.github.com/articles/caching-your-github-password-in-git/

You can create a .bashrc file in the home directory of your user like C:/Users/youruser , and put there:您可以在用户的​​主目录中创建一个.bashrc文件,例如C:/Users/youruser ,然后放在那里:

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

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

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

This script executes every time after bash runs.每次 bash 运行后都会执行此脚本。 So you will need to enter the password only once, when git-bash is started!所以你只需要输入一次密码,当git-bash启动时!

Some versions of bash require .bash_profile file instead .bashrc , so just in case clone .bashrc :某些版本的 bash 需要.bash_profile文件而不是.bashrc ,所以以防万一克隆.bashrc

copy .bashrc .bash_profile

There is a much simpler solution to this problem if you don't mind having you password stored in plaintext and you're using TortoiseGit. 如果您不介意以明文形式存储密码并且使用的是TortoiseGit,则有一个简单的解决方案。

Simply create a _netrc file in your user profile directory - ie in Windows 7 it would be C:\\Users\\MyName\\_netrc . 只需在用户配置文件目录中创建_netrc文件-即在Windows 7中为C:\\Users\\MyName\\_netrc This file will store your login information in this format: 该文件将以以下格式存储您的登录信息:

machine bitbucket.org login thisismyname password p455w0rD
machine bitbucket.org login another_account password pwdpwdPWd
machine github.com login thisismynameagain password p455w0rD

I'm using TortoisePLink as a SSH client and it works flawlessly. 我使用TortoisePLink作为SSH客户端,它可以完美运行。

可以尝试添加 -k arg ;

ssh-add -k ~/.ssh/id_rsa

Make sure that your ~/.ssh/config does not contain确保您的~/.ssh/config包含

UseKeychain yes

which prevents ssh-add from persisting to the ssh-agent .这可以防止ssh-add持久化到ssh-agent

Windows: If you went through steps from @d3r3kk and you still have problems, then try this: Windows:如果您执行了@d3r3kk 中的步骤,但仍然遇到问题,请尝试以下操作:

WINDOWS:视窗:

  • delete .bashrc file删除 .bashrc 文件
  • this file is usually located in C:\Users\myusername此文件通常位于 C:\Users\myusername

How to properly run the SSH-Agent on Windows如何在 Windows 上正确运行 SSH-Agent

None of the existing answers solved the problem to satisfaction for me.现有的答案都没有让我满意。 Here's how it should be done according to Microsoft:根据微软的说法,这是应该如何完成的:

Start a powershell console with admin rights:启动具有管理员权限的 powershell 控制台:

  • press ⊞ Win to open start menu, type powershell , press ctr shift enter⊞ Win打开开始菜单,输入powershell ,按ctr shift enter

Add the OpenSSH included in Windows to your PATH variable:将 Windows 中包含的 OpenSSH 添加到PATH变量中:

setx PATH "c:/Windows/System32/OpenSSH/;$Env:PATH;" 

Close the powershell terminal and open a new one.关闭 powershell 终端并打开一个新终端。

Enter the following commands to ensure the openssh agent runs as a service in the background:输入以下命令以确保 openssh 代理在后台作为服务运行:

# enable automatic start
Get-Service ssh-agent | Set-Service -StartupType Automatic

# start it now
Start-Service ssh-agent

Now the service should be running, you can check via现在服务应该正在运行,您可以通过以下方式检查

Get-Service ssh-agent

Load your private key into the ssh-agent, eg将您的私钥加载到 ssh-agent 中,例如

ssh-add $env:USERPROFILE/.ssh/id_rsa

Source: https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement#user-key-generation来源: https ://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement#user-key-generation

You should now be able to use your private key without entering the password every time.您现在应该能够使用您的私钥,而无需每次都输入密码。

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

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