LoginSignup
1
0
はじめての記事投稿
Qiita Engineer Festa20242024年7月17日まで開催中!

WindowsでのGit SSH認証のトラブルシューティング

Posted at

目的

Windows PCのgitのSSHの設定につまづいてしまったので、その時に調査した内容と対応方法をまとめます。

現象

GitHubの公式ドキュメントをもとにSSHの設定を行いましたが、自分のプライベートリポジトリをクローンしようとすると認証エラーとなってしまいました。

>git clone <repository path>
Cloning into '<repository name>'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

しかし、SSHでGitHubに接続はできています。

>ssh -T git@github.com
Hi rkobayashi! You've successfully authenticated, but GitHub does not provide shell access.

環境

  • Microsoft Windows [Version 10.0.22631.3737]
  • git version 2.45.2.windows.1

原因

gitが使用しているSSHクライアントがシステムとは異なるものを使用しているため、鍵の設定が共有されていないことが原因でした。

こちらの記事を読んで気づきました。

対策

gitで使用するSSHクライアントをシステムのものに変更します。

git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"

調査方法

以下、調査した時に使用したコマンドを紹介します。

sshにvオプションを追加することでデバッグ情報が出力されます。
下のコマンドでsshではOpenSSH_for_Windows_8.6p1が使用されていることがわかります。

>ssh -vT git@github.com
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
...
debug1: Exit status 1

環境変数GIT_SSH_COMMANDを設定することでgitのsshにデバッグオプションを追加することができます。

powershellでの実行例
> $env:GIT_SSH_COMMAND="ssh -v"
> git clone <repository path>
Cloning into '<repository name>'...
OpenSSH_9.7p1, OpenSSL 3.2.1 30 Jan 2024
...
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

まとめ

WindowsのgitでSSH認証エラーについて、異なるSSHクライアントが原因で問題になることがわかりました。
以上、最後までお読みいただきありがとうございました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0