0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHubで ‘Permission denied’ エラーが出たら?SSHキーの作成と設定方法

Last updated at Posted at 2025-05-04

はじめに

GitHubでcloneしようとしたら、
Permission denied (publickey) という謎のエラーが出た…。
最初すごく戸惑った経験があるので「SSHキーの作成方法」と「GitHubへの登録方法」をMacユーザーの私が、Windows環境で優しく解説していきます。

特に新しいPCに変えた時や初めてSH接続するときに役立つ内容です。

こんなエラーです。

// 公開鍵認証失敗
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.

Permission denied (publickey).
ここが一番重要なポイントで、「公開鍵認証が失敗しました」という意味です。
GitHub 側は「この接続元のSSH鍵、知らないよ?」という反応をしている状態ですね。

既にSSHキーがあるか確認する

ls ~/.ssh

出力例

  • id_rsa: 秘密鍵(他人に見せない)
  • id_rsa.pub: 公開鍵(Githubに登録する)

SSHキーを作成する

"your_email@example.com" は GitHub に登録しているメールアドレス

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Enterキーで進む

Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Hoge/.ssh/id_rsa): 

Enterキーで進む

Enter passphrase (empty for no passphrase): 

Enterキーで進む

Enter same passphrase again: 

成功すると2つのファイルが作成される

  • C:\Users\Hoge.ssh\id_rsa

  • C:\Users\Hoge.ssh\id_rsa.pub

Your identification has been saved in C:\Users\Hoge/.ssh/id_rsa
Your public key has been saved in C:\Users\Hoge/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:uUkIqPT6a9uCpZEDbxACNmcdER0ba1ebCxjZ9BUWaR0 your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
|oo o.+=+=. . ==E.|
|+ +. . o*.o =o . |
|.o. .  = o +.    |
|+..  ...... .    |
|oo..  . S  .     |
| ++.   . o       |
| o*     o        |
| o.o.            |
|  .++.           |
+----[SHA256]-----+

正常に鍵が作成できたか確認する

ls ~/.ssh

こういうのが出力されればOK

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2025/04/09      8:45           3389 id_rsa
-a----        2025/04/09      8:45            751 id_rsa.pub
-a----        2025/04/08      8:30             92 known_hosts

公開鍵(.pub)をGitHubに登録する

以下のコマンドで出力された文字列全体をコピー

  • Mac(またはWSL)の場合
cat ~/.ssh/id_rsa.pub
  • Windowsの場合
 type $env:USERPROFILE\.ssh\id_rsa.pub

GitHubに公開鍵を追加

GitHubにログイン

Settingsを選択

DF1B28D3-75E0-4894-8500-A05413530155_1_201_a.jpeg

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

DAA27DFE-C437-4FA8-A2E4-2CBF80FDDECD_1_201_a.jpeg

New SSH keyをクリック

image.png

Title:任意のタイトル
Key:コピーした公開鍵を貼り付ける

Add SSH keyを押下

これでSSHの設定は完了です✨

接続テスト

ssh -T git@github.com

成功時のメッセージ

Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.

最後に

GitHubはつまずきやすいですが、少しずつ慣れていけば大丈夫。

この記事の通りにリポジトリを作ってみましょう!
GitHubとローカルプロジェクトを連携する方法

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?