【GitHub】ssh.github.comからcloneできない

他の人が書いたスクリプトの中で、 git clone git@ssh.github.com:user/repo.git のように ssh.github.com からcloneする処理があったが、なぜかこれに失敗して困った。

error: couldn't make loader for git@ssh.github.com:user/repo.git: trouble cloning git@ssh.github.com:user/repo.git: exit status 128

接続確認してみるが、github.com には接続できるものの ssh.github.com は失敗する。

ssh -T git@github.com 
Hi Udomomo! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@ssh.github.com
git@ssh.github.com: Permission denied (publickey).

鍵が正しく作られていなければ両方失敗するはずなので、ローカルの id_rsa を調べてみたところ、その置き場所に原因があった。
公開鍵認証をする際は、デフォルトでは .ssh/id_rsa秘密鍵を置く。しかし自分の場合、普段GitHubだけでなくawsなど他のサービス用の秘密鍵を管理しやすくするため、 .ssh/github/id_rsa のようにディレクトリを分けてその中に秘密鍵を置いていた。秘密鍵のパスを替えた場合、 .ssh/config 内の IdentityFile でパスを指定できるのだが、今回の場合は以下のようになっていた。

  • github.com -> .ssh/config 内で IdentityFile を指定した状態で設定済み。そのため正しい秘密鍵を使って認証に成功。
  • ssh.github.com -> .ssh/config 内に設定がない。そのためデフォルトである .ssh/id_rsaを探すものの秘密鍵がなく認証に失敗。

対応としては、 ssh.github.com.ssh/config に追記すればよい。なお、github.com への接続を HostName を指定して ssh.github.com に飛ばすだけでは解決できない。コマンドラインで与えられたホスト名は .ssh/config 内の Host の項目のみを探索して判定されるため、 HostName に書いてもマッチせず、 IdentityFile の指定を渡すことができない。

Host github.com
    HostName ssh.github.com
    Port 443
    IdentityFile ~/.ssh/github/id_rsa
    User git

Host ssh.github.com
    Port 443
    IdentityFile ~/.ssh/github/id_rsa
    User git
ssh -T git@github.com 
Hi Udomomo! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@ssh.github.com
Hi Udomomo! You've successfully authenticated, but GitHub does not provide shell access.