おそらく、同じ状況に陥る条件としては、まず、鍵を作成する際、次のように -f オプションでファイル名を指定し、
% ssh-keygen -b 4096 -f github
その上で、~/.ssh/config に、例えば、以下のように書いている場合などです。
Host github
HostName github.com
User git
IdentityFile ~/.ssh/github
これで SSH 接続できるかどうかを確認してみると、次のようになります。
% ssh github
PTY allocation request failed on channel 0
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
結果のメッセージに、自分のユーザー名( username )が含まれていれば良いはずなのですが、1
しかし、git clone を実行すると、以下のように Permission denied となってしまいました。
% git clone git@github.com:username/repositoryname.git
Cloning into 'repositoryname'...
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.
問題は、~/.ssh/config の Host を指定するところで、任意の名前で大丈夫なのですが、
GitHub の Web ページから git@github.com:~ の部分をコピペして、git clone git@github.com:~ と実行したいのであれば、Host は github.com にしておく必要があります。
ちなみに、一応 github も残しておくなら、次のように書けば OK です。
Host github github.com
HostName github.com
User git
IdentityFile ~/.ssh/github
もちろん、初めから、git clone git@github:~ というコマンドで git clone していれば、何も問題はなかったのですが、
コピペに慣れていると、またハマりそうなので、自戒も込めて記事を書いておくことにしました。
この辺りの理屈については、分かっているつもりでも、ふとした時に思い出せなかったり、間違った思い込みをしていたりしそうなので、改めて、注意が必要ですね。