Git Bash 和 Pageant 没有使用密钥

Git Bash and Pageant are not using keys

我已经为 Windows 安装了 Git(针对 MinTTY 和 PuTTY\plink.exe 进行了配置)并安装了 PuTTY,我正试图让它与 Bitbucket 存储库一起使用。我已经在 Pageant 和网站上加载了我的 SSH 密钥,但每当我尝试做任何需要 pulling/pushing:

的事情时
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

当我 运行 建议的 ssh -v hg@bitbucket.org 时,它使用 id_rsa 但 none 我在 ~/.ssh 中的其他键。尝试使用 ssh-add ~/.ssh/bitbucket_rsa 结果:

Could not open a connection to your authentication agent.

我读过有关配置 PuTTY 以允许转发的信息,但这通常是建议结束的地方,所以我只是为默认会话设置此选项并将其保存在那里。

我已经 运行 ps 检查哪些应用程序 运行ning, ssh-agent 不在其中。 运行 eval 'ssh-agent' 启动守护进程,但没有区别。

您不需要 PuTTY 即可在 ssh 中使用 Bitbucket:打包在 git-for-windows 中的 OpenSSH 版本 (C:\prgs\git\PortableGit-2.7.0-64-bit\usr\bin\ssh.exe) 工作正常。

确保在您当前的 shell 会话中,您已将主页设置为 %USERPROFILE%PortableGit-2.7.0-64-bit\git-cmd.exe 足以打开正确配置的常规 CMD)。

%USERPROFILE%\.ssh 中创建名为 config 的文件(与 step 3 of the Atlassian documentation 中一样):

Host bitbucket.org
 IdentityFile ~/.ssh/bitbucket_rsa

(您也可以使用“/C/path/to/bitbucket_rsa”。)

这允许使用像 bitbucket.org:user/repo 这样的 SSH URL。 使用 ssh -Tv bitbucket.org 进行测试(当然是在将 public 密钥添加到您的 Bitbucket 帐户之后)。

注意:仅当您的私钥受密码保护时才需要 ssh-agent

2018 年更新,两年后:“Say Farewell to PuTTY as Microsoft adds an OpenSSH Client to Windows 10”。 是时候抛弃 putty 了:不需要不同的 (ppk) 密钥格式,并且 proprietary solution, now that OpenSSH 已作为 Windows 功能正式分发(目前处于测试阶段,2018 年第一季度)。

你把事情搞混了。

PuTTY 和 Pageant 与 OpenSSH 工具不兼容,无法一起通信。 PuTTY 无法从您的 OpenSSH 代理读取密钥,而 OpenSSH 也无法从 Pageant 读取密钥。密钥格式也不同。这在这个世界上是一件坏事,但目前还没有解决方案。

此外 "agent forwarding" 与 "using agent" 不同。

如果您使用 plink 设置了 Git(确保您确实设置了),请检查您的 Pageant 是否 运行 以及是否在您存储的个人资料中选中 "Attempt authentication using Pageant" 选项Connection 下的 PuTTY → SSHAuth.

如果它不能解决您的问题,post 来自 PuTTY 的一些调试日志。

要访问从 Git Bash 加载到 Pegeant 中的 SSH 密钥,Git 在 Windows 上随附,您可以使用 this program。安装过程在链接页面上有描述。

这就是最终对我有用的东西。

顺便说一句,我在 Windows 上也有 Bash,但我认为这不重要。

我安装了 Sourcetree 并用 plink.exeputtygen.exepageant.exe 指向它的文件夹。您也可以单独下载并安装它们。

配置Windows环境变量

  1. 在 Windows 10 搜索栏中输入 Environment。否则,打开系统属性/高级系统设置并找到您的环境变量。
  2. 添加一个新的系统变量
    • 变量名:GIT_SSH
    • 变量值plink.exe 文件的完整路径(您也可能在同一文件夹中有 pageant.exeputtygen.exe)。
      • 我的是:C:\Program Files (x86)\Atlassian\SourceTree\tools\putty\plink.exe

      • 注意:较新版本的 Sourcetree 似乎安装了 ot %localappdata%。这些工具位于 %localappdata%\SourceTree\app-x.x.x\tools\putty

      • 注意:较新的 Sourcetree(Windows 上的 v2)使用版本化目录,因此每次更新它时,您都必须更新这个很痛苦。最好只复制 plink.exe 并将其放在不会更改的地方。

      • 如果您有上述任何程序运行您可以随时打开任务管理器,找到进程,然后打开文件夹位置以获取plink.exe.

利润

确保重新启动您的终端,以便它们获得更新的环境变量。对我来说,我的集成终端在 Visual Studio Code 内是 运行 Bash,所以我不得不重新启动 Visual Studio 代码。关闭集成终端并打开一个新终端肯定是可以接受的,但我也希望 Visual Studio 代码中的内置 git 功能也能正常工作。

备注

鉴于我安装了 Sourcetree,我能够使用它的界面从 Bitbucket 中克隆出来并通过它的界面推送,但是尝试通过终端无法正常工作,因为它们使用的是不同的凭据集。

另一个需要指出的有趣的事情是,如果您导航到项目的 git 配置位于:./.git/config,您可以将您的遥控器从使用 SSH 换成 HTTPS。您可以从 Bitbucket 存储库的 Overview 中获取以下值。

  • ssh 布局:git@bitbucket.org:USERNAME/REPO_NAME.git
  • https 布局:https://USERNAME@bitbucket.org/USERNAME/REPO_NAME.git

我注意到在 Windows 10 上使用 HTTPS 时它会使用 Windows 凭据管理器(我试图自己解决这个问题时尝试将我的凭据添加到其中,但我仍在使用SSH 所以没关系)当你去与远程存储库交互时,它会提示你输入你的凭据并将它们存储起来以备后用 Windows Credential Manager :)

希望其中一种方法对您有用。 HTTPS 方法将跳过整个 SSH 密钥生成并将其推送到 Bitbucket,但对我来说感觉更安全和便携。

MacOS

您可能需要将您的密钥添加到钥匙串中,尤其是当您使用 Visual Studio 代码并且您的密钥上有密码时(目前 Visual Studio 代码不允许您输入密码)。

ssh-add -K ~/.ssh/id_rsa

https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#adding-your-ssh-key-to-the-ssh-agent

增加

我不确定这些是否对某些人有帮助,但我一直在关注 Visual Studio Windows 的 SSH 支持:https://github.com/Microsoft/vscode/issues/13680.

Git Bash 对 Windows 7 的作用:将 .pkk 文件转换为 OpenSSH 格式:

https://www.simplified.guide/putty/convert-ppk-to-ssh-key

将生成的密钥添加到 IdentityFile .ssh/config Git Bash,例如:

Host repository
     # My converted OpenSSH key
     IdentityFile /c/Users/me/open-ssh.pri

     # This repository server uses a specific name, not usually needed.
     User git

     # This repository server uses a specific port, not usually needed
     Port 8322

     # Repository server full name
     Hostname repo.server.com

对于未来的 google 员工,

就像@CTS_AE 所说的那样做,或者你可以在提升的 powershell

中 运行
[Environment]::SetEnvironmentVariable("GIT_SSH", "C:\Program Files\PuTTY\plink.exe", "Machine")

确保您已经使用 ssh url 没有 http url

克隆了您的存储库

要检查,请使用 git origin --remote -v

如果远程 url 是像下面这样的 http url

origin  https://username@example.com/username/repo.git

使用git remote set-url origin命令更改遥控器url,例如:

git remote set-url origin git@example.com:username/repo.git