0
0

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接続できなかった話

0
Posted at

状況

どこでも見る、sshの接続確認の方法。これはできてた。

$ ssh -T git@github.com
Hi yo16! You've successfully authenticated, but GitHub does not provide shell access.

but〜は気にしなくていいらしい。

しかし、git pullができない!という問題。リモートリポジトリはあるよ!スペルミスもなし!( git config remote.origin.url で何度も確認済み)

$ sudo git pull origin main
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

ちなみに sudo git してるのは、ローカルを更新する権限がないよ!と言われてしまうからです。<これが今回のキモでした!!>

結果的には恥ずかしい感じなんですが、きっとハマる人がいるので恥を晒していきますよ!

原因

ssh -T git@github.com を実行しているときに使うsshキーは、 ~/.ssh/id_rsa って、普通のユーザーの id_rsa のファイルです。ところが、 sudo git pull とかでgithubへ接続するときに使うsshキーは、 /root/.ssh/id_rsa です!

なので、 ~/.ssh/id_rsa.pub をgithubへ登録すると、 ssh -T git@github.com ではそれを使うので「正常」と言われます。でも、 sudo git pull 〜 では、 /root/.ssh/id_rsa を使うので、そんな人知らないとなるわけです。

configを使うにしても、 sudo git 〜 と入力したら、 ~/.ssh/config なんて見てないです!超時間かけて調べて試行錯誤してたのに全部無駄だったのね〜!!という話ですよ。

対策

ケースバイケースでいろいろだと思いますが、今回の私の場合は今回の端末を1人でしか使わないし、使う予定もないので、rootさん大活躍で問題なし。ということで、 /root/.ssh/id_rsa.pub をgithubに登録して終了。

gitでリモートリポジトリを使うときは、 sudo git 〜 で!

(たぶん、いや絶対、もっといい方法あるわ。知らんけど。)

雑記

linuxの経験の浅さによって、5時間ほどずっぽりとハマりました。ググってもググっても、このこと書いてる人に当たらない!みなさん賢いです!その点自分はどんだけ・・・

どうやって気づいたかと言うと、githubのヘルプ。

Should the sudo command be used with Git?
You should not be using the sudo command with Git. If you have a very good reason you must use sudo, then ensure you are using it with every command (it's probably just better to use su to get a shell as root at that point). If you generate SSH keys without sudo and then try to use a command like sudo git push, you won't be using the same keys that you generated.
引用元 Error: Permission denied (publickey)

えっと、このエラー対策の記事の一番上に書いてあるモノです。。git に sudo 使わない方がいいんじゃない?という感じでしょうかね。もし使うんなら全部にしなよ!そんなやついないと思うけど!みたいな?(どんだけ英語苦手なんだ訳)

ssh-keygen を sudo でやんないといけないのかなーくらいにぼーっと考えてやってみたら、ssh-keygen で id_rsa を上書きするよ?というメッセージが出なくて、今回の原因に気づきました。(そこ!?)

git コマンドをsudo 使わずにやりたいなー毎回めんどくさいし。でもやりかた調べてもわからない。そこは繋がらないことの本質ではないから後で考えよう。って考えが、泥沼から出られなかった大きな一因でした。ええ、結局今もわからないですが。

いやーよかった。linux経験の底の浅さが見事に露呈した問題でした。でもどうせまたすぐ忘れると思うので、自分用メモとして、また新人が陥ってたら優しく先輩が教えてあげるネタとして、恥を忍んで書き残した次第でございます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?