MeWrite Docs

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).

原因

  1. SSH鍵が設定されていない: SSH鍵が生成されていない
  2. 公開鍵が登録されていない: GitHubに公開鍵が追加されていない
  3. SSH Agentに鍵が追加されていない: 鍵が読み込まれていない
  4. 鍵のパスが間違っている: デフォルト以外の場所に鍵がある
  5. リポジトリへのアクセス権がない: Organization や private リポジトリの権限

解決策

1. SSH鍵の生成

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# SSH鍵を生成(Ed25519 推奨)
ssh-keygen -t ed25519 -C "your_email@example.com"

# RSA鍵を生成(古いシステム用)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 対話的な質問に答える
# Enter file in which to save the key (/Users/you/.ssh/id_ed25519): [Enter]
# Enter passphrase (empty for no passphrase): [パスワード入力]

# 生成された鍵を確認
ls -la ~/.ssh/
# id_ed25519(秘密鍵)と id_ed25519.pub(公開鍵)

2. SSH Agentに鍵を追加

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# SSH Agent を起動
eval "$(ssh-agent -s)"

# 鍵を追加
ssh-add ~/.ssh/id_ed25519

# 追加された鍵を確認
ssh-add -l

# macOS の場合、Keychainに保存
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

3. GitHubに公開鍵を登録

1
2
3
4
5
6
7
8
# 公開鍵をコピー
cat ~/.ssh/id_ed25519.pub

# macOS
pbcopy < ~/.ssh/id_ed25519.pub

# Linux (xclip)
xclip -sel clip < ~/.ssh/id_ed25519.pub
  1. GitHub にログイン
  2. Settings → SSH and GPG keys
  3. “New SSH key” をクリック
  4. Title に識別しやすい名前を入力
  5. Key に公開鍵を貼り付け
  6. “Add SSH key” をクリック

4. SSH接続のテスト

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# GitHub への接続テスト
ssh -T git@github.com

# 成功すると以下のメッセージが表示される
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.

# GitLab への接続テスト
ssh -T git@gitlab.com

# デバッグモード
ssh -vT git@github.com

5. SSH設定ファイルの作成

1
2
# ~/.ssh/config を作成・編集
nano ~/.ssh/config
# ~/.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
1
2
3
4
5
# 設定ファイルの権限
chmod 600 ~/.ssh/config
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

6. HTTPSに切り替え

1
2
3
4
5
6
7
8
# SSH URLからHTTPS URLに変更
git remote set-url origin https://github.com/username/repository.git

# 現在のリモートURLを確認
git remote -v

# Personal Access Token を使用
# Settings → Developer settings → Personal access tokens

7. Deploy Key の設定(CI/CD用)

1
2
3
4
5
6
# デプロイ専用の鍵を生成
ssh-keygen -t ed25519 -C "deploy@ci" -f ~/.ssh/deploy_key

# リポジトリの Settings → Deploy keys
# 公開鍵を追加
# "Allow write access" にチェック(push する場合)

8. Organization のSSO認証

1
2
3
4
5
6
# Organization が SAML SSO を使用している場合
# SSH鍵の認可が必要

# 1. GitHub → Settings → SSH and GPG keys
# 2. 対象の鍵の横にある "Configure SSO" をクリック
# 3. Organization を選択して "Authorize"

9. 古い鍵形式の問題

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# OpenSSH 8.8 以降では RSA SHA-1 が無効
# エラー: no mutual signature supported

# 新しい形式の鍵を生成
ssh-keygen -t ed25519 -C "your_email@example.com"

# または SSH設定で許可(非推奨)
# ~/.ssh/config
Host github.com
    HostName github.com
    User git
    PubkeyAcceptedKeyTypes +ssh-rsa

Windows での設定

1
2
3
4
5
6
7
8
9
# SSH Agent を有効化
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent

# 鍵を追加
ssh-add $env:USERPROFILE\.ssh\id_ed25519

# Git の設定
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"

デバッグのコツ

詳細なデバッグ出力

1
2
3
4
5
6
7
# 最大の詳細度
ssh -vvv git@github.com

# 確認ポイント:
# - 正しい鍵ファイルが読み込まれているか
# - 鍵の署名方式が正しいか
# - Offering public key の行

SSH Agent の確認

1
2
3
4
5
6
7
8
9
# 登録されている鍵を表示
ssh-add -l

# 鍵がない場合
# The agent has no identities.

# すべての鍵を削除して再追加
ssh-add -D
ssh-add ~/.ssh/id_ed25519

鍵のフィンガープリント確認

1
2
3
4
# ローカルの鍵のフィンガープリント
ssh-keygen -lf ~/.ssh/id_ed25519.pub

# GitHub に登録されている鍵と照合

Git の他のエラー

最終更新: 2025-12-08