【完全ガイド】git clone/push で Permission denied (publickey) の解決方法|SSH鍵の生成・登録・複数アカウント対応まで徹底解説
- 作成日 2026.07.08
- git
Git を使い始めて、多くの人が最初にぶつかる壁の一つ:
$ git clone git@github.com:username/myrepo.git
Cloning into 'myrepo'...
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.
または、しばらく問題なく使えていたのに、突然:
$ git push origin main
git@github.com: Permission denied (publickey).
一体何が起きているのか、慌ててしまう場面。
GitHub は2021年8月に、パスワード認証を廃止し、SSH 鍵 or Personal Access Token 必須化しました。この変更で「昔のやり方でクローンできない」と困った方も多いはず。さらに、
- SSH 鍵って何?作り方が分からない
- 鍵は作ったのに GitHub は認識しない
- 会社と個人でアカウント切り替えたい
- Kamal / rsync でも似たエラー
- port 22 が会社ネットワークで塞がれてる
- macOS / Windows / WSL で挙動が違う
- Deploy Key と個人鍵、何が違う?
- HTTPS と SSH、どっちを使うべき?
など、正しく理解しないと永遠に解決しない症状の複合体です。
本記事では、Permission denied (publickey) の完全な解決方法を、リファレンスとして実用的に整理します。エラーの本質、原因診断、SSH 鍵の生成(ed25519 推奨)、GitHub / GitLab / Bitbucket への登録、ssh-agent、~/.ssh/config、複数アカウント切り替え、Deploy Key、port 443 回避、macOS Keychain、WSL / Windows 対応、Kamal / rsync 対応、HTTPS vs SSH 比較、実践パターン、FAQまで完全網羅。この1本で SSH 認証の全問題を確実に解決できるようになります。
- 1. 結論:3ステップで直る場合が多い
- 2. まず理解する:エラーの本質
- 3. 診断フロー
- 4. SSH 鍵の生成
- 5. 公開鍵を GitHub に登録
- 6. ssh-agent の設定
- 7. ファイル権限の設定(要注意)
- 8. ~/.ssh/config の活用
- 9. 複数 GitHub アカウントの切り替え
- 10. 主要な原因別対処
- 11. Deploy Key(サーバー側の鍵)
- 12. HTTPS vs SSH の使い分け
- 13. デバッグ手順
- 14. 実践シナリオ
- 15. よくある質問(FAQ)
- 15.1. Q1. HTTPS と SSH、結局どっちがいい?
- 15.2. Q2. 鍵をなくした・忘れた
- 15.3. Q3. パスフレーズを毎回聞かれる
- 15.4. Q4. 秘密鍵は同期していい?
- 15.5. Q5. ssh-add すると Could not open a connection to your authentication agent
- 15.6. Q6. SourceTree / GitKraken を使っている
- 15.7. Q7. 会社と個人で分けたい
- 15.8. Q8. GitHub 以外(自社Git サーバー等)
- 15.9. Q9. git@github.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
- 15.10. Q10. パスフレーズの強度
- 15.11. Q11. Bad owner or permissions on ~/.ssh/config
- 15.12. Q12. 昔生成した RSA 鍵、まだ使える?
- 16. 参考リンク・関連資料
- 17. まとめ
結論:3ステップで直る場合が多い
時間がない方向けに、最短の対処手順を示します。
ステップ1:SSH 鍵が存在するか確認
ls -al ~/.ssh
# id_ed25519 と id_ed25519.pub があるか(RSA なら id_rsa / id_rsa.pub)
なければ次で生成:
ssh-keygen -t ed25519 -C "your_email@example.com"
# Enter で保存先確定、パスフレーズは任意
ステップ2:公開鍵を GitHub に登録
# 公開鍵をクリップボードへ
cat ~/.ssh/id_ed25519.pub | pbcopy # macOS
cat ~/.ssh/id_ed25519.pub | clip # Windows
cat ~/.ssh/id_ed25519.pub | xclip -sel c # Linux (要 xclip)
GitHub → Settings → SSH and GPG keys → New SSH key → 貼り付け
ステップ3:接続テスト
ssh -T git@github.com
# Hi username! You've successfully authenticated
これで解決するケースが7割。残り3割の詳しい原因と対処は以下で解説します。
まず理解する:エラーの本質
メッセージの意味
git@github.com: Permission denied (publickey).
これはGitHub のサーバーが言っているメッセージ:
git@github.comとして SSH 接続を試みた- GitHub は「あなたが誰か」を SSH 公開鍵で判定しようとした
- 公開鍵が一致しない or 提供されていない → 拒否
SSH 鍵認証の仕組み
[あなたのマシン] [GitHub サーバー]
秘密鍵(id_ed25519) ←─────────→ 公開鍵(登録済み)
- ローカル保管 - GitHub 側に保管
- 絶対に外に出さない - 誰が見てもOK
- 「解錠」の役割 - 「鍵穴」の役割
秘密鍵でメッセージに署名 → 公開鍵で GitHub が検証 → 一致すれば認証成功。
なぜエラーが出るか
以下のいずれかが原因:
- 鍵が生成されていない
- 鍵は生成したが GitHub に公開鍵を登録していない
- 鍵は登録したが ssh-agent が読み込んでいない
- 鍵のファイル権限が緩すぎる(SSH がセキュリティ上使用拒否)
- リモート URL が HTTPS で SSH 鍵を使っていない
- 複数鍵がある / 別アカウント用の鍵を送信している
- port 22 がネットワークで塞がれている
git以外のユーザー名で接続を試みている
診断フロー
原因を素早く特定するチェックリスト:
# 1. リモート URL 確認
git remote -v
# HTTPS → SSH に変更 or Token 使用
# git@... → SSH(正常なパターン)
# 2. SSH 鍵の存在
ls -al ~/.ssh
# id_ed25519 / id_rsa などがあるか
# 3. ssh-agent に鍵が登録されているか
ssh-add -l
# 鍵一覧が表示される
# 4. 接続テスト(詳細)
ssh -Tvv git@github.com
# 詳細なデバッグ情報
上記から状況を絞り込めます。
SSH 鍵の生成
ed25519(推奨)
ssh-keygen -t ed25519 -C "your_email@example.com"
対話:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/you/.ssh/id_ed25519): [Enter]
Enter passphrase (empty for no passphrase): [任意]
Enter same passphrase again: [同上]
Your identification has been saved in /Users/you/.ssh/id_ed25519
Your public key has been saved in /Users/you/.ssh/id_ed25519.pub
RSA(古いシステム向けフォールバック)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
なぜ ed25519 推奨か
| 項目 | RSA | ed25519 |
|---|---|---|
| セキュリティ | 4096bit で十分 | より強力 |
| 鍵サイズ | 3KB 前後 | 68 bytes |
| 署名速度 | 遅い | 高速 |
| サポート | 全環境 | ほぼ全環境(2015年〜) |
新規は ed25519 一択。RSA は古いサーバー向けフォールバック。
パスフレーズの是非
- 設定する: 秘密鍵が漏れても保護される(推奨)
- 設定しない: 便利だが、鍵漏洩時にリスク
パスフレーズを設定するなら ssh-agent で毎回入力を省略。
カスタムファイル名
ssh-keygen -t ed25519 -C "..." -f ~/.ssh/id_ed25519_github
# ファイル名を指定
複数アカウントで使い分ける時に必須。
公開鍵を GitHub に登録
公開鍵をコピー
# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Windows (Git Bash)
clip < ~/.ssh/id_ed25519.pub
# Linux(xclip がある場合)
xclip -sel clip < ~/.ssh/id_ed25519.pub
# または表示してコピー
cat ~/.ssh/id_ed25519.pub
内容例:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...省略... your_email@example.com
GitHub での登録
- GitHub にログイン
- 右上のアバター → Settings
- 左メニュー → SSH and GPG keys
- New SSH key をクリック
- Title: 分かりやすい名前(例: “Mac Book Pro 2024″)
- Key type: Authentication Key(デフォルト)
- Key: 公開鍵を貼り付け
- Add SSH key
GitLab での登録
- GitLab にログイン
- 右上アバター → Preferences
- 左メニュー → SSH Keys
- Add new key をクリック
- Key: 公開鍵を貼り付け
- Title: 分かりやすい名前
- Expires at: 任意(無期限も可能)
- Add key
Bitbucket での登録
- アバター → Personal settings
- SSH keys → Add key
- Label と Key を入力
- Add key
接続テスト
# GitHub
ssh -T git@github.com
# Hi username! You've successfully authenticated
# GitLab
ssh -T git@gitlab.com
# Welcome to GitLab, @username!
# Bitbucket
ssh -T git@bitbucket.org
# logged in as username
“shell access is not provided” が出ても、これは正常な成功メッセージ(Git ホスティングは通常のシェルを許可しないため)。
ssh-agent の設定
なぜ必要か
- SSH 接続時に 秘密鍵ファイルを読む必要
- パスフレーズ付きの場合、毎回入力が発生
- ssh-agent が鍵をメモリにキャッシュ、これらを解決
起動と鍵の追加
# エージェント起動
eval "$(ssh-agent -s)"
# Agent pid 12345
# 鍵を追加
ssh-add ~/.ssh/id_ed25519
# パスフレーズ入力(あれば)
# Identity added: ~/.ssh/id_ed25519
# 追加された鍵を確認
ssh-add -l
macOS:Keychain と統合
# パスフレーズを Keychain に保存
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
再起動後もパスフレーズ入力不要に。
~/.ssh/config に:
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
Linux:起動時に自動起動
~/.bashrc / ~/.zshrc に:
# ssh-agent 起動(既に起動していなければ)
if [ -z "$SSH_AUTH_SOCK" ]; then
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519 2>/dev/null
fi
または systemd 管理:
systemctl --user enable ssh-agent
詳細はsystemctl vs service の記事も参照。
Windows(PowerShell)
# サービス有効化
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
# 鍵追加
ssh-add $env:USERPROFILE\.ssh\id_ed25519
WSL2
# WSL 内で
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Windows の鍵を使いたい場合はコピー
cp /mnt/c/Users/YourName/.ssh/id_ed25519* ~/.ssh/
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
ファイル権限の設定(要注意)
SSH はセキュリティ上、権限が緩いファイルを拒否します:
# ~/.ssh ディレクトリ: 700
chmod 700 ~/.ssh
# 秘密鍵: 600
chmod 600 ~/.ssh/id_ed25519
# 公開鍵: 644
chmod 644 ~/.ssh/id_ed25519.pub
# config ファイル: 600
chmod 600 ~/.ssh/config
# authorized_keys(サーバー側): 600
chmod 600 ~/.ssh/authorized_keys
権限違反の症状
$ ssh -T git@github.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/you/.ssh/id_ed25519' are too open.
→ chmod 600 ~/.ssh/id_ed25519 で修正。
WSL や共有マシンで頻発
Windows と WSL 間で鍵をコピーすると権限がおかしくなりがち:
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 700 ~/.ssh
~/.ssh/config の活用
複数の SSH 接続先を管理する強力な設定ファイル:
基本
# ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
IdentitiesOnly yes の重要性
複数鍵がある時、指定した鍵だけを試す:
IdentitiesOnly yes
これがないと、全ての鍵を順に試行し、GitHub が「Too many keys」でエラーすることも。
権限設定
chmod 600 ~/.ssh/config
複数 GitHub アカウントの切り替え
個人用と会社用を1台の PC で使い分けたい場合。
ステップ1:2つの鍵を作成
# 個人用
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_personal
# 会社用
ssh-keygen -t ed25519 -C "work@example.com" -f ~/.ssh/id_ed25519_work
ステップ2:各アカウントに公開鍵登録
- 個人 GitHub には
id_ed25519_personal.pubを登録 - 会社 GitHub には
id_ed25519_work.pubを登録
ステップ3:~/.ssh/config
# 個人 GitHub
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
# 会社 GitHub
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
ステップ4:git clone / remote 設定
# 個人リポジトリ
git clone git@github.com-personal:personal-user/repo.git
# 会社リポジトリ
git clone git@github.com-work:company-user/repo.git
# 既存リポジトリの remote 変更
git remote set-url origin git@github.com-personal:personal-user/repo.git
ステップ5:git config でユーザー切り替え
# 個人リポジトリ内で
git config user.name "Personal Name"
git config user.email "personal@example.com"
# 会社リポジトリ内で
git config user.name "Work Name"
git config user.email "work@example.com"
または .gitconfig の includeIf で自動:
# ~/.gitconfig
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
主要な原因別対処
原因1:SSH URL でなく HTTPS URL
$ git remote -v
origin https://github.com/user/repo.git (fetch) ← HTTPS
→ SSH に変更:
git remote set-url origin git@github.com:user/repo.git
または clone 時に SSH URL を選ぶ。
原因2:Personal Access Token(HTTPS の場合)
HTTPS 使う場合は Personal Access Token 認証:
# GitHub Settings → Developer settings → Personal access tokens
# トークン生成後、パスワード欄に入力
# または credential helper で永続化
git config --global credential.helper store
原因3:port 22 が塞がれている
会社ネットワーク等で port 22 がブロック:
ssh -T -p 443 git@ssh.github.com
# Hi username! ... → 成功
~/.ssh/config で永続化:
Host github.com
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519
GitHub は port 443 で ssh.github.com 経由の SSH を提供。
原因4:間違ったユーザー名
ssh -T username@github.com # ❌
ssh -T git@github.com # ✅
必ず git@github.com。Git ホスティングサービスは共通で git ユーザー。
原因5:秘密鍵ファイル名が変
デフォルト以外の名前:
ls ~/.ssh/
# my_custom_key my_custom_key.pub
→ ~/.ssh/config で指定:
Host github.com
IdentityFile ~/.ssh/my_custom_key
または ssh-add で読み込み:
ssh-add ~/.ssh/my_custom_key
原因6:鍵が古い RSA + SHA-1
GitHub は 2022年に RSA SHA-1 を廃止。古い鍵は使えない可能性:
# 対処: ed25519 に切り替え
ssh-keygen -t ed25519 -C "..."
# 新鍵を GitHub に登録
原因7:ssh-agent が起動していない
$ ssh-add -l
Could not open a connection to your authentication agent.
$ eval "$(ssh-agent -s)"
Agent pid 12345
$ ssh-add ~/.ssh/id_ed25519
Deploy Key(サーバー側の鍵)
サーバーからのアクセス用の特殊な鍵:
特徴
- リポジトリ単位で設定
- 個人アカウントの鍵とは別
- 通常は読み取り専用(書き込み許可も可)
セットアップ
サーバー上で:
ssh-keygen -t ed25519 -f ~/.ssh/deploy_key -C "deploy-key-server-name"
cat ~/.ssh/deploy_key.pub
GitHub リポジトリ:
- Settings → Deploy keys → Add deploy key
- 公開鍵を貼り付け
サーバーの ~/.ssh/config:
Host github-deploy
HostName github.com
User git
IdentityFile ~/.ssh/deploy_key
IdentitiesOnly yes
Clone:
git clone git@github-deploy:username/repo.git
なぜ Deploy Key を使うか
- セキュリティ: 個人アカウントを侵害しても deploy key は限定的
- チーム離脱時: 個人アカウントを削除しても deploy は継続
- スコープ制限: このリポジトリのみアクセス可能
Kamal 2 での利用
# config/deploy.yml
env:
clear:
GIT_SSH_COMMAND: "ssh -i /root/.ssh/deploy_key"
デプロイ環境で deploy key を使用。詳細はKamal 2 デプロイの記事も参照。
HTTPS vs SSH の使い分け
比較表
| 観点 | HTTPS | SSH |
|---|---|---|
| セットアップ | 簡単(トークン取得のみ) | 中程度(鍵生成が必要) |
| 認証 | トークン | 鍵ペア |
| ポート | 443(ほぼどこでも開く) | 22(塞がれることあり) |
| CI/CD 対応 | Personal Access Token | Deploy Key |
| プッシュ速度 | 少し遅い | 少し速い |
| 設定の永続化 | credential helper | ssh-agent |
| 推奨シーン | 一時的 / 会社ネット制限あり | 個人開発 / 長期利用 |
どっちを選ぶ
- 個人開発長期利用: SSH
- 会社の制限ネットワーク: HTTPS + token
- 短期利用: HTTPS
- サーバーからのpull: Deploy Key(SSH)
デバッグ手順
ssh -T で接続テスト
ssh -T git@github.com
成功パターン:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
詳細デバッグ
ssh -Tvv git@github.com
出力から:
debug1: Reading configuration data /Users/you/.ssh/config
debug1: /Users/you/.ssh/config line 1: Applying options for github.com
debug1: Connecting to github.com [192.30.255.113] port 22.
debug1: Connection established.
...
debug1: Offering public key: /Users/you/.ssh/id_ed25519
...
debug1: Server accepts key: /Users/you/.ssh/id_ed25519 ...
Hi username!
- どの鍵を提示したか
- 受け入れられたか
- どのconfigファイルが適用されたか
を確認可能。
一般的なパターン
# 鍵未提供
debug1: No more authentication methods to try.
Permission denied (publickey).
→ ssh-add で鍵を追加、または -i オプション or config指定。
# 権限が緩すぎ
Permissions 0644 for '/Users/you/.ssh/id_ed25519' are too open.
→ chmod 600 ~/.ssh/id_ed25519。
# 鍵は提供したが GitHub 側で認識せず
debug1: Offering public key: ...
debug1: Authentications that can continue: publickey
→ 公開鍵が GitHub に登録されていない。要確認。
実践シナリオ
シナリオ1:新規セットアップ(Mac)
# 鍵生成
ssh-keygen -t ed25519 -C "you@example.com"
# パスフレーズを Keychain に
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
# ~/.ssh/config
cat >> ~/.ssh/config <<EOF
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
UseKeychain yes
AddKeysToAgent yes
EOF
# 公開鍵コピー
pbcopy < ~/.ssh/id_ed25519.pub
# GitHub に貼り付け(Web操作)
# 接続テスト
ssh -T git@github.com
シナリオ2:Windows + WSL2
# WSL2 内で
ssh-keygen -t ed25519 -C "you@example.com"
# ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# GitHub 登録(clip.exe を経由)
cat ~/.ssh/id_ed25519.pub | clip.exe
# 接続テスト
ssh -T git@github.com
シナリオ3:port 22 が塞がれた会社ネット
# port 22 ダメ
ssh -T git@github.com
# → タイムアウト
# port 443 で試す
ssh -T -p 443 git@ssh.github.com
# Hi username!
# ~/.ssh/config に恒久化
cat >> ~/.ssh/config <<EOF
Host github.com
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519
EOF
シナリオ4:既存repo をSSH に変更
git remote -v
# origin https://github.com/user/repo.git
git remote set-url origin git@github.com:user/repo.git
git remote -v
# origin git@github.com:user/repo.git
シナリオ5:Kamal デプロイでの SSH設定
# config/deploy.yml
ssh:
user: deploy
keys:
- /Users/me/.ssh/id_ed25519_kamal
または ~/.ssh/config:
Host kamal-server
HostName 203.0.113.42
User deploy
IdentityFile ~/.ssh/id_ed25519_kamal
詳細はKamal 2 デプロイの記事、SSH host key verification failed の記事も参照。
シナリオ6:rsync でも同じエラー
$ rsync -avz ./ deploy@server:/path/
Permission denied (publickey).
→ 同じ鍵認証の問題。SSH と同じ対処。
詳細はscp vs rsync の記事も参照。
シナリオ7:GitHub Actions で SSH 使う
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Deploy
run: |
ssh-keyscan -H your-server.com >> ~/.ssh/known_hosts
ssh user@your-server.com "..."
シナリオ8:新PC への環境移行
# 古い PC で
cd ~/.ssh
tar czf ssh_backup.tar.gz id_ed25519 id_ed25519.pub config known_hosts
# 新 PC で
mkdir -p ~/.ssh
tar xzf ssh_backup.tar.gz -C ~/.ssh/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config
# 接続テスト
ssh -T git@github.com
シナリオ9:チーム開発者への設定共有
# セットアップスクリプトを共有
cat > setup_ssh.sh <<'EOF'
#!/bin/bash
ssh-keygen -t ed25519 -C "$1" -f ~/.ssh/id_ed25519 -N ""
cat ~/.ssh/id_ed25519.pub
echo "この公開鍵を GitHub に登録してください"
EOF
chmod +x setup_ssh.sh
./setup_ssh.sh "new@example.com"
シナリオ10:認証の一括診断
#!/bin/bash
# ssh-diagnose.sh
echo "1. SSH 鍵ファイル確認"
ls -al ~/.ssh/
echo "2. ssh-agent 確認"
ssh-add -l 2>&1
echo "3. GitHub 接続テスト"
ssh -T git@github.com 2>&1
echo "4. リモート URL 確認"
git remote -v 2>&1
echo "5. GitHub SSH URL の解決"
dig ssh.github.com +short 2>&1
よくある質問(FAQ)
Q1. HTTPS と SSH、結局どっちがいい?
個人開発なら SSH が便利(一度設定すれば永続的にパスワード不要)。会社の制限ネットワークなら HTTPS + Personal Access Token。
Q2. 鍵をなくした・忘れた
# 新しい鍵を生成
ssh-keygen -t ed25519 -C "..."
# 古い鍵をGitHubから削除、新しい鍵を登録
# GitHub → Settings → SSH keys
Q3. パスフレーズを毎回聞かれる
ssh-agent に登録、または macOS なら Keychain 連携:
ssh-add ~/.ssh/id_ed25519
# または macOS
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Q4. 秘密鍵は同期していい?
Dropbox / iCloud には保存しない。漏洩リスク。1Password / Bitwarden の Secure Note などは検討可。
理想はマシンごとに別鍵を生成し、GitHub に複数登録。
Q5. ssh-add すると Could not open a connection to your authentication agent
ssh-agent が起動していない:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Q6. SourceTree / GitKraken を使っている
GUI ツールは独自の SSH 鍵管理をすることも:
- SourceTree: 内蔵ssh-agent
- GitKraken: Preferences → SSH で設定
システムの SSH と統一する設定推奨。
Q7. 会社と個人で分けたい
~/.ssh/config + 別ホスト名の技を使用(前述の「複数 GitHub アカウント」参照)。
Q8. GitHub 以外(自社Git サーバー等)
同じ手順で対応可能:
Host gitlab.company.com
HostName gitlab.company.com
User git
IdentityFile ~/.ssh/id_ed25519_company
Q9. git@github.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
「publickey」以外の方式も試そうとしている。同じく鍵認証の問題。
Q10. パスフレーズの強度
- 弱いパスフレーズ: 鍵漏洩時に無意味
- 強いパスフレーズ + Keychain 連携: セキュリティと利便性の両立
Q11. Bad owner or permissions on ~/.ssh/config
chmod 600 ~/.ssh/config
Q12. 昔生成した RSA 鍵、まだ使える?
現時点で 2048bit 以上のRSA 鍵は動くが、ed25519 への切り替えを強く推奨。GitHub は将来的にRSA を廃止する可能性。
参考リンク・関連資料
公式
- GitHub Docs: Error: Permission denied (publickey) – GitHub 公式トラブルシュート
- GitHub Docs: Generating a new SSH key – 鍵生成公式
- GitLab Docs: Use SSH keys – GitLab SSH
- Bitbucket Docs: SSH keys – Bitbucket SSH
関連記事(本サイト)
- SSH host key verification failed – SSH 接続の対
- git push rejected – push エラー
- git merge conflict の解決方法 – conflict 対応
- git rebase 使い方 – 履歴整理
- git stash 使い方 – 一時退避
- Kamal 2 デプロイ – SSH でのデプロイ
- scp vs rsync – SSH でのファイル転送
- Docker no space left on device – Docker系
- docker daemon 接続エラー – Docker 系
- systemctl vs service – systemd 管理
- Linuxでファイル差分を確認する方法(diff/colordiff/vimdiff) – config 比較
- Linuxでシンボリックリンクを作成・確認・削除する方法 – .ssh ディレクトリ
- Rails 8 アップグレードガイド – Rails 開発
- Solid Queue 使い方 – Rails ジョブ
まとめ
Permission denied (publickey) の解決、要点を再整理します。
標準セットアップ手順
# 1. 鍵生成
ssh-keygen -t ed25519 -C "you@example.com"
# 2. ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 3. 公開鍵をGitHub に登録
cat ~/.ssh/id_ed25519.pub | pbcopy # macOS
# → GitHub Settings → SSH keys
# 4. 接続テスト
ssh -T git@github.com
# Hi username!
# 5. clone
git clone git@github.com:user/repo.git
主要な原因と対処
| 原因 | 対処 |
|---|---|
| 鍵未生成 | ssh-keygen -t ed25519 -C "..." |
| 未登録 | GitHub Settings で登録 |
| ssh-agent 未起動 | eval "$(ssh-agent -s)" |
| 権限緩い | chmod 600 ~/.ssh/id_ed25519 |
| URL が HTTPS | git remote set-url origin git@... |
| port 22 ブロック | port 443 + ssh.github.com |
| ユーザー名間違い | git@github.com 使用 |
| 複数鍵の混乱 | ~/.ssh/config で明示 |
診断コマンド
ls -al ~/.ssh # 鍵の存在
ssh-add -l # ssh-agent 内容
ssh -T git@github.com # 接続テスト
ssh -Tvv git@github.com # 詳細デバッグ
git remote -v # リモートURL
必須の権限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config
ed25519 推奨
# ✅ 推奨
ssh-keygen -t ed25519 -C "..."
# ⚠️ 古いシステムのみ
ssh-keygen -t rsa -b 4096 -C "..."
複数アカウントの ~/.ssh/config
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
事故防止
- 秘密鍵をGit に含めない(絶対に)
- 秘密鍵を共有・同期しない(Dropbox等)
chmod 600を常に確認IdentitiesOnly yesで誤送信防止- 公開鍵は自由に共有可(暗号化に使えない)
現代の推奨
- ed25519 鍵(新規は必ず)
- ~/.ssh/config で管理
- ssh-agent + Keychain 連携
- Deploy Key(サーバー用)
- HTTPS + PAT(会社ネット制限時)
これらの知識は、Git 開発・チーム作業・CI/CD・サーバー運用・Kamal デプロイなど、あらゆる場面で活用できます。本記事をブックマークしておけば、SSH 認証の問題を確実に解決できるようになります。
本記事は2026年6月時点の情報をもとに、OpenSSH 8.x/9.x、Git 2.40+、GitHub / GitLab / Bitbucket の各仕様に基づき作成しています。Git ホスティングサービスの仕様変更により挙動が異なる場合があるため、最新の情報はGitHub SSH ドキュメントもあわせてご確認ください。
-
前の記事
【完全ガイド】git pull / merge conflict の解決方法|3-way merge の仕組みからoursとtheirsまで徹底解説 2026.07.07
-
次の記事
【完全ガイド】git rebase の使い方|interactive rebase・squash・fixup・autosquashまで徹底解説 2026.07.08
コメントを書く