LoginSignup
4
4

More than 1 year has passed since last update.

【Git】git cloneをする際に発生するPermissionエラー対処(git@github.com: Permission denied (publickey).)

Last updated at Posted at 2022-05-10

はじめに

GitHubからgit cloneする際に発生するPermissionエラーの対処方法について記載しております。
初歩的かもしれませんが、ご了承下さい。

前提

  • GitHubに登録済みの状態(テストアカウント作成)
  • Gitの初期設定が未実施の状態
  • 使用端末はMacOS(初期でGit導入済み)

※通常では、Gitの初期設定を実施

今回は、GitHubからコードをクローンすることだけにフォーカスしております。

内容

GitHubからSSH接続でgit cloneしようとすると以下エラーが発生。
クローンできず。

スクリーンショット 2022-05-11 0.37.08.png

エラー
ma-sato@satsuton Desktop % git clone git@github.com:atom/atom.git
Cloning into 'atom'...
The authenticity of host 'github.com (52.192.72.89)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
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.
ma-sato@satsuton Desktop %

対処

以下の流れで対処を実施します。

①ターミナルにて公開鍵・秘密鍵の作成
②公開鍵をGitHubにアップする
③GitHub接続確認
④再度、git clone実施

①ターミナルにて公開鍵・秘密鍵の作成

ターミナルにて公開鍵と秘密鍵を作成します。

  • 鍵を入れるフォルダに移動
コマンド
cd ~/.ssh
実行例
ma-sato@satsuton .ssh % cd ~/.ssh
ma-sato@satsuton .ssh % pwd
/Users/ma-sato/.ssh
ma-sato@satsuton .ssh %
  • 鍵を作成
コマンド
ssh-keygen -t rsa

上記コマンドを実行すると、3つ程聞かれるので入力をする。

確認項目 入力内容 備考
Enter file in which to save the key 公開鍵と秘密鍵を格納するパスを指定(絶対パス) 例)/Users/ma-sato/.ssh/testid_rsa

※デフォルトの場合は、以下になります。デフォルトで問題ない場合は、そのままEnterを押下。
/Users/ユーザーディレクトリ/.ssh/id_rsa
Enter passphrase (empty for no passphrase): パスフレーズ(パスワード)の入力 今回は何も入力せずそのまま実行
※パスフレーズは設定されない状態
Enter same passphrase again: パスフレーズ(パスワード)をもう一度入力 今回は何も入力せずそのまま実行
※パスフレーズは設定されない状態
実行例(鍵情報があるため一部省略しております。)
ma-sato@satsuton .ssh % ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/ma-sato/.ssh/id_rsa): /Users/ma-sato/.ssh/testid_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/ma-sato/.ssh/testid_rsa
Your public key has been saved in /Users/ma-sato/.ssh/testid_rsa.pub
  • 鍵を確認

対象のディレクトリに鍵ができていることを確認。

実行例
ma-sato@satsuton .ssh % ls -la /Users/ma-sato/.ssh/ | grep testid
-rw-------   1 ma-sato  staff  2610  5 10 08:50 testid_rsa
-rw-r--r--   1 ma-sato  staff   577  5 10 08:50 testid_rsa.pub
ma-sato@satsuton .ssh %

ファイルの説明は、以下になります。

TH TH TH
testid_rsa.pub 公開鍵 GitHubに登録を行う鍵(第三者への公開は問題ない)
testid_rsa 秘密鍵 ローカルに保持しておく鍵(第三者への公開は厳禁)

②公開鍵をGitHubにアップする

  • GitHubにログインを実施

  • 右上のアイコンをクリックし、Settingsをクリック

スクリーンショット 2022-05-10 23.56.37.png

  • 左側のメニューからSSH and GPG keysをクリック

スクリーンショット 2022-05-10 23.56.50.png

  • SSH KeysのNew SSH keyをクリック

スクリーンショット 2022-05-10 23.57.01.png

  • Titleに任意の鍵の名前、Keyに公開鍵(testid_rsa.pub)の内容をコピー&ペーストで入力。

入力後、Add SSH keyをクリック。

スクリーンショット 2022-05-10 23.58.12.png

※公開鍵の内容は、以下で確認可能(他にも確認方法があるとは思います。)

公開鍵確認
ma-sato@satsuton .ssh % cat testid_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAAQ*****************************(一部省略)
ma-sato@satsuton .ssh %
  • SSH keysに鍵が登録できていることを確認。

スクリーンショット 2022-05-10 23.58.30.png

③GitHub接続確認

  • 以下のコマンドを実行
コマンド
ssh -T git@github.com

以下のようなメッセージが表示されればGitHubへの接続が成功しております。

実行例
ma-sato@satsuton .ssh % ssh -T git@github.com
Hi masuwa9028! You've successfully authenticated, but GitHub does not provide shell access.
ma-sato@satsuton .ssh %

Permissionエラーが表示された場合

①ターミナルにて公開鍵・秘密鍵の作成にてファイル名を設定している場合は、以下のエラーが表示される。

エラー
ma-sato@satsuton ~ % ssh -T git@github.com
git@github.com: Permission denied (publickey).
ma-sato@satsuton ~ %

対処は、以下になります。

  • 以下のファイルを作成
コマンド
touch ~/.ssh/config
実行例
ma-sato@satsuton .ssh % touch ~/.ssh/config
ma-sato@satsuton .ssh % ls ~/.ssh/config
/Users/ma-sato/.ssh/config
ma-sato@satsuton .ssh %
  • 作成したファイルに以下の内容を記載する。
コマンド
vim ~/.ssh/config
記載例
Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/秘密鍵のファイル名
  User git

※今回の場合は、以下になります。

今回の場合
Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/testid_rsa
  User git
  • ファイル内容が記載されていることを確認
コマンド
cat ~/.ssh/config
実行例
ma-sato@satsuton .ssh % cat ~/.ssh/config
Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/testid_rsa #ここに自分の鍵のファイル名
  User git
ma-sato@satsuton .ssh %
  • 再度、接続を確かめる。
コマンド
ssh -T github

以下のようなメッセージが表示されればGitHubへの接続が成功しております。

実行例
ma-sato@satsuton .ssh % ssh -T github
Hi masuwa9028! You've successfully authenticated, but GitHub does not provide shell access.
ma-sato@satsuton .ssh %

④再度、git clone実施

再度、git cloneを実施する。

コマンド
ma-sato@satsuton ~ % git clone git@github.com:atom/atom.git
Cloning into 'atom'...
remote: Enumerating objects: 203916, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 203916 (delta 10), reused 10 (delta 10), pack-reused 203901
Receiving objects: 100% (203916/203916), 321.97 MiB | 7.96 MiB/s, done.
Resolving deltas: 100% (144727/144727), done.
ma-sato@satsuton ~ %

原因

対処で記載している通り、SSH接続の初期設定忘れになります。

参考記事

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