タイトル: リモートリポジトリからクライアントへのSSHクローン
SEOタイトル: git SSH クローン完全ガイド — 鍵生成・config・トラブル
| この記事の要点 |
|
SSH と HTTPS、どちらでクローンする?
| 項目 | HTTPS | SSH |
|---|---|---|
| URL | https://github.com/user/repo.git | git@github.com:user/repo.git |
| 認証 | Personal Access Token (PAT) | SSH 公開鍵 |
| セットアップ | 簡単 (Token のみ) | 鍵生成 + 登録 |
| 毎回の認証 | Credential Helper でキャッシュ | 不要 (鍵 / エージェント) |
| 2024 以降の GitHub | パスワード認証廃止、PAT or OAuth | 変更なし |
| ファイアウォール越え | 443 のみ開ければ OK | 22 番ポートが必要 |
個人開発・継続的に使うなら 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 での登録
- GitHub Settings → SSH and GPG keys を開く
- New SSH key ボタン
- Title: 任意 (例: My MacBook Pro)
- Key type: Authentication Key (デフォルト)
- Key: 公開鍵 (.pub) の中身を貼付
- Add SSH key
GitLab での登録
- 右上アバター → Preferences
- 左メニュー SSH Keys
- 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 ~/.ssh | chmod 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.com | SSH 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 で切替。