IBM CloudのGitレポジトリーにGit CloneでアクセスするときにSSH鍵認証方法で困ったので、その備忘を記載しておきます。
前提の環境
- クライアントPC(Mac)を持っている
- IBM Cloudアカウント(ライトアカウントでOK)を取得している
- IBM CloudのCloud Foundryを利用している
- 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にアクセスする
GitHubのサイト右上のユーザーアイコンからSettingsを選択する
Macのターミナルから以下コマンドで公開鍵情報をクリップボードにコピーする
bash-3.2$ pbcopy < ~/.ssh/id_rsa_bx.pub
GitHubのKeyの箇所にペーストする。Titleは適当なものを入れてAdd Key
を押す
ターミナルで~/.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アクセスのアドレスをコピーする(赤枠でコピーできます)。
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に投稿し始めた初心者のため、ご指摘事項ありましたらコメントください