Permission denied (publickey)
SSH認証でGitリポジトリに接続できない場合に発生するエラー
概要
Permission denied (publickey) は、GitHubやGitLabなどのリモートリポジトリにSSH接続する際、SSH鍵認証に失敗した場合に発生するエラーです。
エラーメッセージ
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.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
原因
- SSH鍵が設定されていない: SSH鍵が生成されていない
- 公開鍵が登録されていない: GitHubに公開鍵が追加されていない
- SSH Agentに鍵が追加されていない: 鍵が読み込まれていない
- 鍵のパスが間違っている: デフォルト以外の場所に鍵がある
- リポジトリへのアクセス権がない: Organization や private リポジトリの権限
解決策
1. SSH鍵の生成
| |
2. SSH Agentに鍵を追加
| |
3. GitHubに公開鍵を登録
| |
- GitHub にログイン
- Settings → SSH and GPG keys
- “New SSH key” をクリック
- Title に識別しやすい名前を入力
- Key に公開鍵を貼り付け
- “Add SSH key” をクリック
4. SSH接続のテスト
| |
5. SSH設定ファイルの作成
| |
# ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
# macOS の場合
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
UseKeychain yes
# 複数のGitHubアカウント
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
| |
6. HTTPSに切り替え
| |
7. Deploy Key の設定(CI/CD用)
| |
8. Organization のSSO認証
| |
9. 古い鍵形式の問題
| |
Windows での設定
| |
デバッグのコツ
詳細なデバッグ出力
| |
SSH Agent の確認
| |
鍵のフィンガープリント確認
| |
Git の他のエラー
この記事は役に立ちましたか?