LoginSignup
3
3

More than 3 years have passed since last update.

IBM CloudのGitHub SSH鍵認証でのアクセス方法

Last updated at Posted at 2018-02-24

IBM CloudのGitレポジトリーにGit CloneでアクセスするときにSSH鍵認証方法で困ったので、その備忘を記載しておきます。

前提の環境

  1. クライアントPC(Mac)を持っている
  2. IBM Cloudアカウント(ライトアカウントでOK)を取得している
  3. IBM CloudのCloud Foundryを利用している
  4. IBM Cloudの継続的デリバリーでGitHubを利用できる

エラーの内容

IBM CloudのGitHubのクローンを、クライアントPCに作成しようとしたところ、認証画面(Username、Password)が出てきたので、IBM Cloudアカウントで認証にトライするが失敗してしまう。

bash-3.2$ git clone https://git.ng.bluemix.net/your_name/example.git
Cloning into 'example'...
Username for 'https://git.ng.bluemix.net': your_name
Password for 'https://your_name@jp.ibm.com@git.ng.bluemix.net': 
remote: HTTP Basic: Access denied
remote: You must use a personal access token with 'api' scope for Git over HTTP.
remote: You can generate one at https://git.ng.bluemix.net/profile/personal_access_tokens
fatal: Authentication failed for 'https://git.ng.bluemix.net/your_name/example.git/'

原因と対応方法

GitHubにユーザー名とパスワードではなく、SSH鍵認証でアクセスする必要がある。クライアントでSSH鍵を作成し、GitHubに公開鍵を登録することによってアクセスが可能となる。

SSH鍵作成〜登録の手順

Macのターミナルを起動して、以下コマンドを入力しSSH鍵を作成する

ssh-keygen -t rsa -C "your_mail@example.com" -b 4096
bash-3.2$ ssh-keygen -t rsa -C "your_mail@example.com" -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_name/.ssh/id_rsa): /Users/your_name/.ssh/id_rsa_bx
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/your_name/.ssh/id_rsa_bx.
Your public key has been saved in /Users/your_name/.ssh/id_rsa_bx.pub.
The key fingerprint is:
SHA256:Y*************************************Y your_mail@example.com
The key's randomart image is:
+---[RSA 4096]----+
|            oo.+o|
|          . *oo+o|
|         . +o+*o*|
|          .oEo+B@|
|        S o....OO|
|       . + .. .X+|
|        .  .+.*.=|
|          .o.  =.|
|           .=*.  |
+----[SHA256]-----+

秘密鍵id_rsa_bxと公開鍵id_rsa_bx.pubができていることを確認する

bash-3.2$ ls -al ~/.ssh/id_rsa_bx*
-rw-------  1 your_name  staff  3243 Feb 24 09:09 /Users/your_name/.ssh/id_rsa_bx
-rw-r--r--  1 your_name  staff   745 Feb 24 09:09 /Users/your_name/.ssh/id_rsa_bx.pub

IBM Cloudの継続的デリバリーからGitHubにアクセスする
Screen Shot 2018-02-24 at 15.05.09.png

↓IBM CloudのGitHubサイト
Screen Shot 2018-02-24 at 18.29.55.png

GitHubのサイト右上のユーザーアイコンからSettingsを選択する
Screen Shot 2018-02-24 at 15.44.57.png

メニューからSSH Keysを選択する
Screen Shot 2018-02-24 at 16.20.54.png

Macのターミナルから以下コマンドで公開鍵情報をクリップボードにコピーする

bash-3.2$ pbcopy < ~/.ssh/id_rsa_bx.pub

GitHubのKeyの箇所にペーストする。Titleは適当なものを入れてAdd Keyを押す
Screen Shot 2018-02-24 at 16.23.39.png

Add Keyの下に公開鍵情報が登録される
Screen Shot 2018-02-24 at 16.27.26.png

ターミナルで~/.ssh/configの中身に認証情報を追記する。

~/.ssh/config
Host git.ng.bluemix.net   #IBM CloudのGitHub
 user your_name   #GitHubでのユーザー名を入れる
 hostname git.ng.bluemix.net
 IdentityFile ~/.ssh/id_rsa_bx   #作成した秘密鍵ファイルの場所
 Identitiesonly yes

GitHubにアクセスして、SSHアクセスのアドレスをコピーする(赤枠でコピーできます)。
Screen Shot 2018-02-24 at 15.10.48.png

Macのターミナルでgit cloneコマンドでクローンを作成する。

bash-3.2$ git clone git@git.ng.bluemix.net:your_name/example.git
Cloning into 'example'...
remote: Counting objects: 1644, done.
remote: Compressing objects: 100% (1453/1453), done.
remote: Total 1644 (delta 163), reused 1643 (delta 163)
Receiving objects: 100% (1644/1644), 8.44 MiB | 2.29 MiB/s, done.
Resolving deltas: 100% (163/163), done.
Checking connectivity... done.

SSH鍵認証に成功し、クローンが作成できている。

*Qiitaに投稿し始めた初心者のため、ご指摘事項ありましたらコメントください

3
3
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
3
3