この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:9
ページ更新者:T
更新日時:2026-06-11 07:10:02

タイトル: リモートリポジトリからクライアントへのSSHクローン
SEOタイトル: git SSH クローン完全ガイド — 鍵生成・config・トラブル

この記事の要点
  • SSH 鍵生成: ssh-keygen -t ed25519 -C "you@example.com" (Ed25519 推奨)
  • GitHub / GitLab に公開鍵 (.pub) を登録 → git clone git@github.com:user/repo.git
  • ~/.ssh/config で Host エイリアスと IdentityFile を指定すると複数鍵を使い分けできる
  • ssh-agent + ssh-add でパスフレーズ入力を省略 (Windows は OpenSSH Authentication Agent サービス)
  • Permission denied (publickey) エラーは公開鍵未登録 / 鍵パーミッション (.ssh は 700, 秘密鍵は 600) / 別ユーザ で発生

SSH と HTTPS、どちらでクローンする?

項目HTTPSSSH
URLhttps://github.com/user/repo.gitgit@github.com:user/repo.git
認証Personal Access Token (PAT)SSH 公開鍵
セットアップ簡単 (Token のみ)鍵生成 + 登録
毎回の認証Credential Helper でキャッシュ不要 (鍵 / エージェント)
2024 以降の GitHubパスワード認証廃止、PAT or OAuth変更なし
ファイアウォール越え443 のみ開ければ OK22 番ポートが必要

個人開発・継続的に使うなら SSH 推奨。CI/CD や一時利用、社内 FW で 22 が閉じている場合は HTTPS。

手順 1: SSH 鍵を生成

Ed25519 が現在の推奨。RSA を選ぶ場合は 4096 bit。

# Ed25519 (推奨)
ssh-keygen -t ed25519 -C "you@example.com"

# RSA (古い環境向け)
ssh-keygen -t rsa -b 4096 -C "you@example.com"

# プロンプト
# Enter file in which to save the key (~/.ssh/id_ed25519): [Enter]
# Enter passphrase: [できれば入力]
# Enter same passphrase again: [もう一度]

# 生成物
~/.ssh/id_ed25519       # 秘密鍵 (絶対に外部に出さない)
~/.ssh/id_ed25519.pub   # 公開鍵 (GitHub に登録するもの)

Windows での生成

# Windows 10+ には OpenSSH 同梱
ssh-keygen -t ed25519 -C "you@example.com"
# 鍵は %USERPROFILE%\.ssh\ に保存される

# Git Bash でも同じ
ssh-keygen -t ed25519 -C "you@example.com"

手順 2: 公開鍵を GitHub / GitLab に登録

# 公開鍵を表示してクリップボードへ
cat ~/.ssh/id_ed25519.pub

# Linux: xclip / Windows: clip / macOS: pbcopy
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
cat ~/.ssh/id_ed25519.pub | pbcopy
type %USERPROFILE%\.ssh\id_ed25519.pub | clip

# 出力例:
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAbcDef... you@example.com

GitHub での登録

  1. GitHub Settings → SSH and GPG keys を開く
  2. New SSH key ボタン
  3. Title: 任意 (例: My MacBook Pro)
  4. Key type: Authentication Key (デフォルト)
  5. Key: 公開鍵 (.pub) の中身を貼付
  6. Add SSH key

GitLab での登録

  1. 右上アバター → Preferences
  2. 左メニュー SSH Keys
  3. Key 欄に貼付 → Add key

手順 3: 接続テスト

ssh -T git@github.com

# 初回:
# The authenticity of host 'github.com (140.82.121.4)' can't be established.
# ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
# Are you sure you want to continue connecting (yes/no)? yes

# 成功:
# Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

# GitLab
ssh -T git@gitlab.com
# Welcome to GitLab, @USERNAME!

# 失敗時
# git@github.com: Permission denied (publickey).
# → 鍵未登録 / 鍵が読まれていない / config 設定誤り

手順 4: クローン

# GitHub
git clone git@github.com:USERNAME/REPO.git

# GitLab
git clone git@gitlab.com:USERNAME/REPO.git

# 自社 Git サーバ
git clone git@git.example.com:team/repo.git

# 注意: URL は HTTPS と異なる
# HTTPS: https://github.com/USERNAME/REPO.git
# SSH:   git@github.com:USERNAME/REPO.git  ← ":" であって "/" ではない

~/.ssh/config の活用

複数アカウント / 複数サーバを使うなら必須:

# ~/.ssh/config (Windows: %USERPROFILE%\.ssh\config)

# 個人用 GitHub
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

# 会社用 GitHub (別アカウント) ← エイリアスで識別
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

# GitLab
Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_gitlab
    IdentitiesOnly yes

# 社内 Git
Host git.example.com
    HostName git.example.com
    User git
    Port 2222                                   # 非標準ポート
    IdentityFile ~/.ssh/id_ed25519_company
    IdentitiesOnly yes

会社アカウントのリポジトリをクローン

# エイリアスを使って URL を書き換える
git clone git@github-work:company/repo.git
# → ~/.ssh/config の Host github-work が適用され、id_ed25519_work で認証

ssh-agent でパスフレーズ入力を省略

秘密鍵にパスフレーズを設定すると安全ですが、毎回入力は面倒。ssh-agent がメモリ上に保持してくれます:

Linux / macOS

# 起動 + 鍵追加 (シェルセッション内)
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Enter passphrase: [入力] → 以降の git push でパスフレーズ不要

# 登録済鍵を一覧
ssh-add -l

# 永続化 (~/.bashrc / ~/.zshrc に追記)
if [ -z "$SSH_AUTH_SOCK" ]; then
   eval "$(ssh-agent -s)"
   ssh-add ~/.ssh/id_ed25519 2>/dev/null
fi

macOS Keychain 連携

# ~/.ssh/config に追記
Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_ed25519

# 初回 ssh-add 時にパスフレーズを Keychain に保存
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
# → macOS 再起動後も自動で読み込まれる

Windows ssh-agent

# 管理者 PowerShell
Get-Service ssh-agent
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent

# 通常 PowerShell
ssh-add $env:USERPROFILE\.ssh\id_ed25519

# 登録済確認
ssh-add -l

known_hosts (サーバ ホスト鍵)

初回接続時にサーバの公開鍵 (ホスト鍵) を ~/.ssh/known_hosts に記録します。これにより中間者攻撃を検知:

# 初回プロンプト後、自動的に保存される

# サーバ鍵が変わった場合 (再構築等):
# WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
# ...

# 古いエントリを削除
ssh-keygen -R github.com
# → 次回再接続で新しい鍵を再記録

# 既知の正しい鍵を事前に登録
ssh-keyscan github.com >> ~/.ssh/known_hosts

エラー: Permission denied (publickey)

SSH 認証失敗の代表エラー。原因と対処の早見表:

原因確認対処
公開鍵が未登録GitHub Settings → SSH keys公開鍵を追加
正しい秘密鍵が使われていないssh -vT git@github.com で読込鍵を確認~/.ssh/config で IdentityFile 指定
~/.ssh 権限不正ls -la ~/.sshchmod 700 ~/.ssh && chmod 600 ~/.ssh/id_*
ssh-agent に登録されていないssh-add -l で空ssh-add ~/.ssh/id_ed25519
会社プロキシ / FW で 22 ブロックssh -T -p 443 git@ssh.github.comSSH over HTTPS を利用 (下記)
別アカウントの鍵が読まれているssh -vT で識別IdentitiesOnly yes を config に

詳細ログを出して原因切り分け

ssh -vT git@github.com
# debug1: Reading configuration data /home/me/.ssh/config
# debug1: Connecting to github.com [140.82.121.4] port 22.
# debug1: Offering public key: /home/me/.ssh/id_ed25519
# debug1: Authentications that can continue: publickey
# debug1: No more authentication methods to try.
# git@github.com: Permission denied (publickey).

# どの鍵が使われたか確認できる

SSH over HTTPS (port 443)

会社の FW で 22 番ポートが閉じている場合、GitHub は ssh.github.com:443 でも受けてくれます:

# ~/.ssh/config
Host github.com
    HostName ssh.github.com
    User git
    Port 443
    IdentityFile ~/.ssh/id_ed25519

# 確認
ssh -T -p 443 git@ssh.github.com
# Hi USERNAME! You've successfully authenticated...

HTTPS から SSH へリモート URL を切り替え

# 現在のリモート確認
git remote -v
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

# SSH に変更
git remote set-url origin git@github.com:user/repo.git

# 確認
git remote -v
# origin  git@github.com:user/repo.git (fetch)
# origin  git@github.com:user/repo.git (push)

FAQ

Q: 秘密鍵のパスフレーズは設定すべき?
A: 強く推奨。秘密鍵が万一流出しても即時に悪用されない。ssh-agent + Keychain で日常運用は不便を感じない。

Q: 鍵の有効期限はある?
A: SSH 鍵自体に有効期限は無いが、1〜3 年で更新するのが推奨。GitHub では Key の最終使用日が表示されるので、未使用鍵は削除推奨。

Q: 既存リポジトリにも SSH 鍵が反映される?
A: SSH 鍵は GitHub アカウントに紐づくので、全リポジトリに自動的に適用される。ただしリモート URL を SSH 形式に変えていない既存クローンは HTTPS 認証のまま動く。git remote set-url で切替。