LoginSignup
208
193

More than 5 years have passed since last update.

新しいMacでGitHubのSSH接続をするまでの環境構築手順

Last updated at Posted at 2018-03-14

新しいMacでGitHubのSSH接続を確認するところまでの環境構築メモ。
※すでにGitHubアカウントが存在する前提で進めます

大まかな流れ

  1. gitのインストール
  2. SSH認証キーの作成
  3. GitHubに公開鍵を登録
  4. SSH接続確認

Homebrew経由でgitをインストールする

まずはgitをインストールします。
ここではHomebrew経由でインストールするので、まだインストールしていない場合はHomebrewをインストールしましょう。

MacにHomebrewをインストールする手順とWarningの解決方法 - Qiita

Homebrewが使えるようになったら、ターミナルで以下コマンドを打ちgitをインストールします。

$ brew install git

以下コマンドでgitのバージョン番号が表示されればOKです。

$ git --version

SSH認証キーの作成

SSH認証に使う秘密鍵(id_rsa)と 公開鍵(id_rsa.pub)を生成します。

$ ssh-keygen -t rsa

~/.ssh/configに接続設定を追加

$ vim ~/.ssh/config
# global setting for macOS Sierra
Host *
  AddKeysToAgent yes
  UseKeychain yes

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

AddKeysToAgentUseKeychainの設定は、macos Sierra以降でマシン再起動すると毎回パスワード入力を求められるようになったので、その対策として入れてます。

生成した秘密鍵に読み取り専用属性を与える

$ cd ~/.ssh
$ chmod 600 id_rsa

ssh-agentに秘密鍵を登録

$ eval `ssh-agent`
$ ssh-add ~/.ssh/id_rsa

登録されたか確認

$ ssh-add -l

The agent has no identities.と出たら登録されていない状態です。

GitHubに公開鍵を登録する

続いてGitHubに公開鍵の登録をします。

公開鍵をコピーする

$ pbcopy < ~/.ssh/id_rsa.pub

GitHubへアクセスしSSH Keysを登録

GitHubで「settings」 > 「SSH and GPG keys」 > 「New SSH Key」と進み、「Key」項目に公開鍵を貼り付けて「Add SSH key」をクリック。
スクリーンショット 2018-03-13 21.03.16.png
GitHubから「[GitHub] A new public key was added to your account」という件名のメールが来るはずなので確認しましょう。

公開鍵が登録されたか確認

$ ssh -T git@github.com
$ Are you sure you want to continue connecting (yes/no)? yes
$ Enter passphrase for key
Hi unsolublesugar! You've successfully authenticated, but GitHub does not provide shell access.

SSH接続確認

.ssh/configに登録したホスト名でも接続できるか確認。

$ ssh github
PTY allocation request failed on channel 0
Hi unsolublesugar! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

疎通確認ができたら終わり。

208
193
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
208
193