LoginSignup
19
14

More than 1 year has passed since last update.

GitHubやGitLabへSSH接続する設定

Last updated at Posted at 2019-05-23

はじめに

GitLab.com を業務で使うことになったのですがセキュリティ対策のため二段階認証設定をしたところ、Eclipseやコマンドラインからhttps接続できなくなったため(当たり前)、初めてSSH接続にチャレンジしました。

パーソナルアクセストークンを生成し、それをパスワードとすることでhttps接続が可能なようです。
必要な方は こちらのページ を参考にしてください。

PC環境

  • Windows 10 Pro
  • Git for Windows ver.2.37.3

SSH鍵生成

Git Bash を起動し ssh-keygen コマンドを実行します。

オプション 内容
-t 鍵タイプ
-b 鍵長(ビット)
-C コメント(メアドが通例らしい)
$ ssh-keygen -t rsa -b 4096 -C "xxxxxxxx@example.com"

保存先が表示されるので問題なければそのまま Enter を押します。

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

パスフレーズを設定します。
設定すると秘密鍵自体も暗号化されますが、fetchなどリモートリポジトリにアクセスする度にパスフレーズを求められます。
ここでは未指定とし、そのまま Enter を押します。

Created directory '/c/Users/xxxxxxxx/.ssh'.
Enter passphrase (empty for no passphrase):

再度 Enter を押します。

Enter same passphrase again:

SSH鍵の生成が完了しました。

Your identification has been saved in /c/Users/xxxxxxxx/.ssh/id_rsa.
Your public key has been saved in /c/Users/xxxxxxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VJ2U...  xxxxxxxx@pcname
The key's randomart image is:
+---[RSA 4096]----+
|   **E=+=+o+.+..+|
|  o.=.=+.B= = ..o|
| . . + .B..o o ..|
|  . .  .o.  o . o|
|   . .  S  . o  o|
|    .     + o   .|
|         o =     |
|          o      |
|                 |
+----[SHA256]-----+
種類 ファイルパス
秘密鍵 C:\Users\xxxxxxxx\.ssh\id_rsa
公開鍵 C:\Users\xxxxxxxx\.ssh\id_rsa.pub

公開鍵をコピー

$ clip < ~/.ssh/id_rsa.pub

公開鍵の内容がクリップボードにコピーされます。
(macOS の場合は pbcopy コマンドだそうです)

公開鍵を登録

GitHub の場合

GitHubにログインし、右上から [Settings] を選択します。
image.png
左側のメニューから [SSH and GPG keys] を選択し、[New SSH key] ボタンを選択します。
Title欄には分かりやすい任意の名称を入力します。
Key Type は [Authentication Key] を選択します。
Key欄へ先ほどコピーした公開鍵をペーストします。
[Add SSH key] ボタンをクリックして完了です。
image.png

GitLab の場合

GitLabにログインし、右上から [設定] を選択します。
rapture_20190523171214.jpg
左側のメニューから [SSH鍵] を選択し、Key欄へ先ほどコピーした公開鍵をペーストします。
タイトルには分かりやすい任意の名称を入力します。
[キーを追加] ボタンをクリックして完了です。
rapture_20190523171915.jpg

設定確認

Git Bash で ssh コマンド実行

Git Bash を起動し ssh コマンドを実行します。

GitHubの場合
$ ssh -T git@github.com
GitLabの場合
$ ssh -T git@gitlab.com

初回のみ下記のようにフィンガープリントの確認を行って信頼済みサイトとしてローカルに保存します。
{暗号化方式} key fingerprint is SHA256:xxxxxxxx で出力されているのフィンガープリントが正しいかどうかを確認します。
フィンガープリントはそれぞれ下記ページで公開されています。

GitHub:https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints
GitLab:https://docs.gitlab.com/ee/user/gitlab_com/index.html#ssh-host-keys-fingerprints

フィンガープリントは SHA256:フィンガープリント の形式で入力してください。

The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3.........
Are you sure you want to continue connecting (yes/no/[fingerprint])? {yesではなくフィンガープリントの入力を推奨}
Warning: Permanently added 'gitlab.com,35.231.145.151' (ECDSA) to the list of known hosts.

以下のように最終行に表示されればOK。

GitHubの場合
Hi {アカウント名}! You've successfully authenticated, but GitHub does not provide shell access.
GitLabの場合
Welcome to GitLab, @{アカウント名}!

公開鍵が GitHub/GitLab 側に登録されていない場合は以下のように表示されます。

GitHubの場合
git@github.com: Permission denied (publickey).
GitLabの場合
git@gitlab.com: Permission denied (publickey).

以上です。

19
14
1

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
19
14