26
31

More than 5 years have passed since last update.

GithubへのpushでPermission denied (publickey)が出た時の対処法

Posted at

$ git push origin masterを実行した際に次のエラー文が返ってきました。


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.

対象のリポジトリへのアクセス権限がなくプッシュは許可されなかった。

1:鍵を作成する

$ ssh-keygen -t rsa -C GitHubに登録したメールアドレス
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
  [鍵ファイルを保存するフォルダはどこか]
Enter passphrase (empty for no passphrase):
  [パスフレーズを入力してください]
Enter same passphrase again:
  [パスフレーズを再度入力してください]

ssh-keygenは認証用の鍵を作成するコマンド
-t rsa はRSA暗号というタイプの暗号鍵を生成する
ssh-keygen実行時の質問はEnterを3回で先へ進める
・パスフレーズはパスワードのようなもの

2:ssh-agentへ登録する

$ ssh-add -K ~/.ssh/github

3:登録の確認

$ ssh-add -l

The agent has no identities.(登録できていない)
2048 SHA256:ReBd~~~~~~~(RSA)(登録完了)

4:作成した鍵をコピーする

鍵ファイル id_rsa id_rsa.pubができているか確認

$ ls ~/.ssh/
id_rsa id_rsa.pub

id_rsaが秘密鍵
id_rsa.pubが公開鍵
→この公開鍵をgithubに登録し直す

id_rsa.pubを開く

$ less ~/.ssh/id_rsa.pub
↓こんな文字列が書かれているかと思います。鍵の情報です。

ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx mail@mail.com
id_rsa.pubの中の文字列 ssh-rsa〜xxxxxの終わりまでをコピーします。 (mail@mail.comは不要です)

5:githubで鍵を登録する

次はgithubのマイページで、この鍵を登録します。
GitHub右上プロフィール画面、settings → SSH and GPG keys → new keysの順でクリックする
先ほどコピーした(xxxxx)を公開鍵 id_ras.pubの文字列をkeyの箇所に貼り付ける

6:ターミナルで設定する

configファイルを作成する

$ vim ~/.ssh/config   
~/.ssh/config

を実行して、以下のコードを貼り付ける

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

7:接続確認

$ ssh -T git@github.com

Hi xxxxxxxxx! You've successfully authenticated, but GitHub does not provide shell access.
これでsuccessfullyの文字が出れば接続成功!

再度、$ git push origin masterを実行する

26
31
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
26
31