视窗

视窗

我正在尝试使用 PowerShell 在 Windows 11 中拉取 GitHub 存储库。我的机器在 Windows 中配置了 OpenSSH 客户端。

我核实过:

  • ssh-agent 正在运行
  • 使用 ssh-agent 注册的密钥与使用 GitHub 配置文件注册的密钥相同(总体上也是相同的密钥
  • ssh 配置文件为空
  • 我可以向 GitHub 进行身份验证()。ssh -T [email protected]

当我尝试使用 Windows PowerShell 或 从 GitHub 拉取代码时cmd,GitHub 无法识别身份验证。当我使用 Ubuntu(通过 WSL2 在同一主机中)从 GitHub 拉取代码时,GitHub 会识别身份验证并克隆存储库。

为什么?

OpenSSH 在 Windows 中单独安装

PS C:\Windows\System32\OpenSSH> ssh -V
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3

以及在 WSL2 Ubuntu 中。

nick@Nix-XPS-XIII:~/test$ ssh -V
OpenSSH_8.9p1 Ubuntu-3ubuntu0.4, OpenSSL 3.0.2 15 Mar 2022

在 Windows 中,SSH 密钥和配置位于两个位置:PS C:\ProgramData\ssh>PS %USERPROFILE%\.ssh>

视窗

使用 Windows 设置 | 应用程序 | 可选功能安装 OpenSSL。

PS C:\Users\nick> Get-WindowsCapability -Online | Where-Object { $_.Name -like 'OpenSSH.Client*' }

Name  : OpenSSH.Client~~~~0.0.1.0
State : Installed

PS C:\Windows\System32\OpenSSH> dir

    Directory: C:\Windows\System32\OpenSSH

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----            5/6/2022 10:15 AM         320512 scp.exe
-----            5/6/2022 10:15 AM         398848 sftp.exe
-----            5/6/2022 10:15 AM         506880 ssh-add.exe
-----            5/6/2022 10:15 AM         393216 ssh-agent.exe
-----            5/6/2022 10:15 AM         720896 ssh-keygen.exe
-----            5/6/2022 10:15 AM         572416 ssh-keyscan.exe
-----            5/6/2022 10:15 AM        1073152 ssh.exe

该密钥是GitHub中正确的密钥:

PS C:\Users\nick> ssh-add -l -E sha256
256 SHA256:jLE1Wa+qZ+Gwxvcw1PEWFeuoKnNxkQDU8ZP7O0optmo nick@Nix-XPS-XIII (ED25519)

GitHub SSH 和 GPG 密钥

GitHub 接受身份验证。

PS C:\Users\nick> ssh -vT [email protected]
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
debug1: Reading configuration data C:\\Users\\nick/.ssh/config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to github.com [140.82.112.4] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\nick/.ssh/id_rsa type -1
debug1: identity file C:\\Users\\nick/.ssh/id_rsa-cert type -1
debug1: identity file C:\\Users\\nick/.ssh/id_dsa type -1
debug1: identity file C:\\Users\\nick/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ecdsa type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ecdsa-cert type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ecdsa_sk type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ed25519 type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ed25519-cert type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ed25519_sk type -1
debug1: identity file C:\\Users\\nick/.ssh/id_ed25519_sk-cert type -1
debug1: identity file C:\\Users\\nick/.ssh/id_xmss type -1
debug1: identity file C:\\Users\\nick/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.6
debug1: Remote protocol version 2.0, remote software version babeld-dd067d10
debug1: compat_banner: no match: babeld-dd067d10
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen C:\\Users\\nick/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen C:\\Users\\nick/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in C:\\Users\\nick/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: nick@Nix-XPS-XIII ED25519 SHA256:jLE1Wa+qZ+Gwxvcw1PEWFeuoKnNxkQDU8ZP7O0optmo agent
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_rsa
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_ecdsa_sk
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_ed25519_sk
debug1: Will attempt key: C:\\Users\\nick/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: nick@Nix-XPS-XIII ED25519 SHA256:jLE1Wa+qZ+Gwxvcw1PEWFeuoKnNxkQDU8ZP7O0optmo agent
debug1: Server accepts key: nick@Nix-XPS-XIII ED25519 SHA256:jLE1Wa+qZ+Gwxvcw1PEWFeuoKnNxkQDU8ZP7O0optmo agent
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([140.82.112.4]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem full
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: client_input_hostkeys: searching C:\\Users\\nick/.ssh/known_hosts for github.com / (none)
debug1: client_input_hostkeys: searching C:\\Users\\nick/.ssh/known_hosts2 for github.com / (none)
debug1: client_input_hostkeys: hostkeys file C:\\Users\\nick/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: no new or deprecated keys from server
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi nmdemarco! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2112, received 2576 bytes, in 0.1 seconds
Bytes per second: sent 16591.1, received 20236.2
debug1: Exit status 1

ssh 配置文件是空的。

    Directory: C:\Users\nick\.ssh

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          10/15/2023  2:01 PM              0 config

PS C:\Users\nick\.ssh>

尝试克隆项目不起作用

PS C:\Users\nick\projects> git clone [email protected]:practichem/arista-protocolbridge.git
Cloning into 'arista-protocolbridge'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
PS C:\Users\nick\projects>

答案1

正如 Tom Yan 在评论中所说,Windows 版 Git 附带了自己的 OpenSSH 客户端。此客户端未配置为使用 Microsoft OpenSSH 配置目录:\ProgramData\ssh并且也不会使用 Microsoft OpenSSH 代理。Microsoft OpenSSH 代理默认使用命名管道进行监听,而 Git OpenSSH 代理使用模拟 UNIX 套接字。

有两种(技术上是三种)可能的解决方案:

  1. 让 Git OpenSSH 客户端使用 Microsoft OpenSSH 代理。由于它无法连接到 Windows 命名管道,因此您需要一个代理。(我没有尝试过任何代理,因此不推荐任何代理。)

  2. 让 Git 使用 Microsoft OpenSSH 客户端。只需运行git config --global core.sshcommand "C:/Windows/System32/OpenSSH/ssh.exe"

  3. (完全停止使用 Microsoft OpenSSH,只使用 Git 的 OpenSSH。)


您可以使用GIT_SSH_COMMAND它来获取详细日志记录。在 PowerShell 上,您可以像这样使用它:

$env:GIT_SSH_COMMAND="ssh -v"
git clone [email protected]:practichem/arista-protocolbridge.git

您将能够轻松分辨出使用的是 Git 的 OpenSSH 客户端还是 Microsoft OpenSSH 客户端。Git 的 OpenSSH 将绝不参照__PROGRAMDATA__或类似物。

这个环境变量可能会覆盖core.sshcommand上面提到的设置。

相关内容