4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GitHub で SSH 接続自体は確認できたのに git clone は Permission denied になる場合の対処法

4
Posted at

おそらく、同じ状況に陥る条件としては、まず、鍵を作成する際、次のように -f オプションでファイル名を指定し、

% ssh-keygen -b 4096 -f github

その上で、~/.ssh/config に、例えば、以下のように書いている場合などです。

~/.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/configHost を指定するところで、任意の名前で大丈夫なのですが、

GitHub の Web ページから git@github.com:~ の部分をコピペして、git clone git@github.com:~ と実行したいのであれば、Hostgithub.com にしておく必要があります。

ちなみに、一応 github も残しておくなら、次のように書けば OK です。

~/.ssh/config
Host github github.com
HostName github.com
User git
IdentityFile ~/.ssh/github

もちろん、初めから、git clone git@github:~ というコマンドで git clone していれば、何も問題はなかったのですが、

コピペに慣れていると、またハマりそうなので、自戒も込めて記事を書いておくことにしました。

この辺りの理屈については、分かっているつもりでも、ふとした時に思い出せなかったり、間違った思い込みをしていたりしそうなので、改めて、注意が必要ですね。

  1. https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/testing-your-ssh-connection

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?