できない.dev

Git clone が SSH で失敗する

clone 時の SSH 失敗は「ホスト鍵未承認」「ポート 22 ブロック」「組織の SAML 強制」など、push と異なる原因が混じることが多い。

公開: 更新:

症状できない · exit 128
git clone ssh://git@127.0.0.1:2222/home/git/repo.git work
Cloning into 'work'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
解決後できた · exit 0
git clone ssh://git@127.0.0.1:2222/home/git/repo.git work
Cloning into 'work'...

2026-10-12 に alpine:3 で実際に打って取った出力。検証環境の詳細

要約

git clone git@github.com:... が失敗する原因は、push 失敗と一部共通している。
clone で特にハマりやすいのは、企業ネットワークでの 22番ポート遮断 と SAML SSO 未認可 である。

実行例

alpine:3 のコンテナに sshd を立てて ssh:// の URL で clone すると、初回はホスト鍵が未承認のため Host key verification failed.(終了コード 128)で止まった。
StrictHostKeyChecking=accept-new で一度つないでホスト鍵を記録しても、秘密鍵が 0644 のままだと bad permissions で鍵が無視され、Permission denied (publickey) になった。
chmod 600 などで直すと clone が通り、リポジトリの中身が取れた。

$ git clone ssh://git@127.0.0.1:2222/home/git/repo.git work
Cloning into 'work'...
Host key verification failed.
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.
$ echo $?
128
$ ssh -o StrictHostKeyChecking=accept-new -p 2222 git@127.0.0.1 whoami
Warning: Permanently added '[127.0.0.1]:2222' (ED25519) to the list of known hosts.
git
$ echo $?
0
$ ls -l /root/.ssh/id_ed25519
-rw-r--r--    1 root     root           411 Oct 12 02:38 /root/.ssh/id_ed25519
$ git clone ssh://git@127.0.0.1:2222/home/git/repo.git work
Cloning into 'work'...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/root/.ssh/id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/root/.ssh/id_ed25519": bad permissions
git@127.0.0.1: Permission denied (publickey).
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.
$ echo $?
128
$ chmod 700 /root/.ssh
$ chmod 600 /root/.ssh/id_ed25519
$ chmod 644 /root/.ssh/id_ed25519.pub
$ ls -l /root/.ssh
total 12
-rw-------    1 root     root           411 Oct 12 02:38 id_ed25519
-rw-r--r--    1 root     root            99 Oct 12 02:38 id_ed25519.pub
-rw-r--r--    1 root     root            98 Oct 12 02:38 known_hosts
$ git clone ssh://git@127.0.0.1:2222/home/git/repo.git work
Cloning into 'work'...
$ echo $?
0
$ ls work
README.md

— 2026-10-12 時点の出力

検証環境2026-10-12 検証
検証日
実行環境
alpine:3 Alpine Linux v3.24

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. ホスト鍵を承認

ssh -T git@github.com
# The authenticity of host 'github.com' can't be established.
# ... yes と入力

2. ポート22がブロックされる環境

~/.ssh/config に次の設定を書く。

Host github.com
  Hostname ssh.github.com
  Port 443
  User git

3. SSO 認可

GitHub の Settings → SSH keys を開き、該当する鍵で Configure SSO → Authorize を選ぶ。

4. パーミッション修正

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

よくある原因

  1. ホスト鍵を承認していない。
    初回接続時の fingerprint 確認をスキップしている。
  2. ポート 22 がブロックされている。
    社内ファイアウォールや一部 ISP では、外向きの 22 が閉じられている。
  3. SAML SSO で鍵を認可していない。
    GitHub Enterprise / 組織で、SSH 鍵を SSO 認可していない。
  4. 鍵ファイルのパーミッションが緩い。
    0644 などだと、SSH が鍵の利用を拒否する。