23
20

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にsshで接続する

23
Last updated at Posted at 2020-02-23

githubにsshで接続したいだけなのになぜこんなに時間がかかる。
なのでもう迷わなくて済むようにまとめました。

私の環境

  • macOS Tahoe 26.4

前提

  • すでにgithubアカウントを持っている
  • すでにssh、gitがインストールされている(macにはデフォルトで入っているので問題ないと思うが)
  • vimの使い方を知っていること

本記事の流れ

📁keyの保管場所を作成する
  ↓
🔑keyを発行する
  ↓
👀keyが発行されたかどうか確認する
  ↓
✅発行したkeyをagentに登録する
  ↓
⚙️オプションを設定する
  ↓
🐈github側を設定する
  ↓
🔗接続を確認する

1. ssh-keyを保管場所を作成する

何も指定しないと~/.ssh/直下に作成することになる。
しかし、複数のssh-keyを管理することを考慮してディレクトリをわけたい。
なので今回は ~/.ssh/github/直下にkeyを生成する。

①まずは.ssh配下に移動し、

cd ~/.ssh/

もしno such file or directoryと怒られたら

ディレクトリを作成してから、

mkdir ~/.ssh/

再度試しましょう。

cd ~/.ssh/

②そこでgithubディレクトリを作成し、

mkdir github

③lsで確認しよう。

ls

↓こうなったらOK

github

2. ssh-keyを発行する

以下の通りオプション指定しておくとchmodコマンド等で後からkeyの権限を変更しなくて済む。

// ご自身のgithubのメールアドレスに変更してから実行してください
ssh-keygen -t rsa -b 4096 -C 'your_email@example.com'

↓こうなったらOK

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

次に、①どこに、②どんな名前で保存するかを指定する

そのままenterを押すと ①~/.ssh直下に、②id_rsaという名前で保存される。
今回は、複数のssh-keyを管理することを考慮して特定の名前をつけたいので、①~/.ssh/github/直下に、②github_id_rsaという名前に指定する。

// /you/の箇所はご自身のマシンの名前にしてください
/Users/you/.ssh/github/github_id_rsa

↓こうなったらOK

Enter passphrase for "/Users/you/.ssh/github/github_id_rsa" (empty for no passphrase): 🔑

次にパスワードを設定する

Enter passphrase (empty for no passphrase): // パスワードを打ってreturnを押す
Enter same passphrase again: // もう一度入力する

↓こうなったらOK

The key's randomart image is:
+---[RSA 4096]----+
|                 |
|   .. o+o.       |
|    なんか        |
|      キラキラ   |
|        した     |
|     やつ        |
|      *.*o. .    |
|                 |
|                 |
+----[SHA256]-----+

3. 本当にkeyが作成されたか確認する

①まずは.ssh/github配下に移動し、

cd ~/.ssh/github

②lsで確認しよう。

ls

↓こうなったらOK

github_id_rsa	github_id_rsa.pub 

③確認(おまけ)

ls -al | grep github

↓こうなったらOK

-rw-------  1 you  staff  1234 Feb 23 22:58 github_id_rsa
-rw-r--r--  1 you  staff   567 Feb 23 22:58 github_id_rsa.pub

(権限はssh-keygenしたときにオプションで指定したため問題なし)

4. 作成したkeyをssh-agentに登録する

①念のためssh-agentが動いているか確認する

eval "$(ssh-agent -s)"

↓こうなったらOK

Agent pid 12345

②以下のコマンドで登録する

// /you/の箇所はご自身のマシンの名前にしてください
ssh-add /Users/you/.ssh/github/github_id_rsa

③先ほど指定したパスワードを入力する

Enter passphrase for /Users/you/.ssh/github/github_id_rsa: // 先ほどのパスワード

↓こうなったらOK

Identity added: /Users/you/.ssh/github/github_id_rsa (your_email@example.com)

5. ~/.ssh/configの設定

vimの使い方を知っている前提です

vimで開いて設定する

vim ~/.ssh/config

以下の通り書き込む

~/.ssh/config
Host github.com
    HostName github.com # このまま
    IdentityFile ~/.ssh/github/github_id_rsa # keyのpathを指定する
    IdentitiesOnly yes # IndentityFileで指定したpathのkeyのみを参照するため
    TCPKeepAlive yes # 接続中に操作せずに数分放置してもsshが切断されないようにするため
    AddKeysToAgent yes # 接続時に毎回パスワードを求められないようにするため
    UseKeychain yes # 接続時に毎回パスワードを求められないようにするため
    User git # このまま

6. github側での設定〜新規sshを追加する〜

こちらのコマンドで、あらかじめ生成したkeyをclipboardにコピーしておく

pbcopy < ~/.ssh/github/github_id_rsa.pub

githubのご自身のアイコンをタップし、「Settings」をクリックする

サイドバーから「SSH and GPG keys」を、その後「New SSH key」をクリックする
Screenshot 2026-04-17 at 18.26.41.png

Titleを入力したら、あらかじめコピーしておいたkeyをペーストし、「Add SSH key」をクリックする
Screenshot 2026-04-17 at 18.27.06.png

7. 接続を確認する

ssh -T git@github.com

↓こうなったらOK

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

もしThe authenticity of host 'github.com (xx.xx.xxx.xx)' can't be established.と怒られたら

初めてgithubに繋ぐ場合に出る警告なので、yesと入力すればよい。

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes ←ここです

8. git configも設定する

前提知識

gitの設定ファイルは3種類ある

種類 影響範囲 ファイルのpath 備考
system システム全体、全ユーザの全リポジトリ /etc/gitconfig -
global 当該ユーザの全リポジトリ ~/.gitconfig HOME直下
local 特定のリポジトリのみ 特定のリポジトリのpath/.git/config 指定したリポジトリ直下の .git

今回はglobalで設定を行う

~/.gitconfigを設定する

git config --global user.name your_github_user_name
git config --global user.email your_email@example.com

②確認する

cat ~/.gitconfig

↓こうなったらOK

[user]
        name = your_github_user_name
        email = your_email@example.com

③確認(おまけ)

git config --list

↓こうなったらOK

user.name=your_github_user_name
user.email=your_email@example.com

参考

GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~
お前らのSSH Keysの作り方は間違っている
GitHubにSSH認証で接続する
【Github, SSH】SSH鍵を作成し、Githubへそれを登録する手順
gitのssh接続に使用する~/.ssh/configの設定について
Gitの設定をgit configで確認・変更
gitとsshのconfigについて(備忘録)

23
20
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
23
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?