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にはデフォルトで入っているので問題ないと思うが)

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の権限を変更しなくて済む。

your_email@example.comをご自身の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を押すと)パスワードなしでssh-keyが生成される。
今回は念の為パスワードを指定する。

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. ssh-agentに作成したkeyを登録する

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

eval "$(ssh-agent -s)"

↓こうなったらOK

Agent pid 12345

②以下のコマンドで登録しよう。

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 ~/.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を入力する。
以下のようになっているはずなので、yesと入力するだけで良い。

% ssh -T git@github.com
The authenticity of host 'github.com (xx.xx.xxx.xx)' can't be established.
AA11111 key fingerprint is SHA256:hogehogehogehoge...
This key is not known by any other names
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

②確認しよう。

vim ~/.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?