ec2内にgit cloneしようとしたら、Cloning into 'myapp'... Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

[kazumawada@ip-10-0-0-197 rails]$ git clone git@github.com:kazumawada/myapp.git
Cloning into 'myapp'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
[kazumawada@ip-10-0-0-197 rails]$ git remote set-url origin https://kazumawada@github.com/kazumawada/myapp.git 
fatal: not a git repository (or any of the parent directories): .git
[kazumawada@ip-10-0-0-197 rails]$ cd
[kazumawada@ip-10-0-0-197 ~]$ cd ~/.ssh
[kazumawada@ip-10-0-0-197 .ssh]$ vi config

github-[ユーザー名]がポイント!

Host github-kazumawada
  Hostname github.com
  User git
  IdentityFile ~/.ssh/ec2_git_rsa

sshでcloneする際も、git@github-[ユーザー名]:ユーザー名/リポ名

[kazumawada@ip-10-0-0-197 .ssh]$ ssh -T git@github-kazumawada
Hi kazumawada! You've successfully authenticated, but GitHub does not provide shell access.
[kazumawada@ip-10-0-0-197 .ssh]$ cd
[kazumawada@ip-10-0-0-197 ~]$ cd /var/www/rails
[kazumawada@ip-10-0-0-197 rails]$ git clone git@github-kazumawada:kazumawada/myapp.git
Cloning into 'myapp'...
remote: Enumerating objects: 40, done.
remote: Counting objects: 100% (40/40), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 1893 (delta 22), reused 18 (delta 9), pack-reused 1853
Receiving objects: 100% (1893/1893), 26.29 MiB | 8.76 MiB/s, done.
Resolving deltas: 100% (1072/1072), done.
[kazumawada@ip-10-0-0-197 rails]$ ls
myapp
[kazumawada@ip-10-0-0-197 rails]$ 

githubの鍵もグリーンになった f:id:kaz08:20210219105952p:plain

*本当にその鍵が使われているかどうかは、sshでcloneして見ればいいことがわかった。他の方法はもちろんあるかもしれないけど、分かればどんな方法でもいいから、深追いはしない。

参考

qiita.com

f:id:kaz08:20210207143003j:plain