akiyoko blog

akiyoko の IT技術系ブログです

GitHubプライベートリポジトリの作成 & SSH鍵の設定

GitHubのプライベートリポジトリは、無料プランでは作成することができません。
有料プランへのアップグレードは、GitHubの右上の [Account setting] -> [Billing] -> [Available plans] から選択可能です。

f:id:akiyoko:20140304072139p:plain


現在、「Micro」プラン($7/月)に加入しているのですが、プライベートリポジトリを clone, push する際に、GitHubSSH鍵を登録しておかないと、パスフレーズを毎回入力しないといけなくなるので面倒ですよね。

$ git clone https://github.com/akiyoko/testproject.git
Cloning into 'testproject'...
Username for 'https://github.com': akiyoko
Password for 'https://akiyoko@github.com':


SSH公開鍵を GitHub に登録しておくことで、パスフレーズを使わずに、セキュリティ的にも安心して GitHub を使うことができます。
今回は、その手順をメモしておきます。


1. GitHub用のSSH鍵を設定する(Mac側)

1.1. SSH鍵を作成する

$ ssh-keygen -t rsa -C "akiyoko@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/akiyoko/.ssh/id_rsa): /Users/akiyoko/.ssh/github_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/akiyoko/.ssh/github_rsa.
Your public key has been saved in /Users/akiyoko/.ssh/github_rsa.pub.
The key fingerprint is:
00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 akiyoko@example.com

ちなみに、「-t rsa」オプションは付けなくてもデフォルトで「RSA」なので省略可、「-C」オプションで GitHubアカウントに紐付けたメールアドレスを指定しなくても(オプション自体を省略しても)問題ありませんでした。でも、「-C」オプションは指定した方が無難かも(公開鍵に書き込まれますが)。。

1.2. SSHの設定

vim ~/.ssh/config
-----
Host github
  Hostname github.com
  Port 22
  User git
  IdentityFile ~/.ssh/github_rsa
-----
(※↑を末尾に追加)

$ chmod 0600 ~/.ssh/config

注意:config の User に「akiyoko」などと GitHubアカウントを設定してしまうと「Permission denied (publickey).」となってしまいます。


2. GitHubSSH鍵を登録(GitHub側)

右上の「Account setting」アイコンをクリックし、左のメニューから [SSH Keys] を選択して、「Add SSH key」をクリック。

f:id:akiyoko:20140304072203p:plain


Title と Key を入力し、「Add key」をクリックして SSH鍵を登録します。

$ cat ~/.ssh/github_rsa.pub

or

# Macの場合は↓でクリップボードにコピーできる
$ pbcopy < ~/.ssh/github_rsa.pub

参考:http://ri.hateblo.jp/entry/2013/09/22/195005

f:id:akiyoko:20140304072214p:plain


3. SSHしてみる(Mac側)

$ ssh github
The authenticity of host 'github.com (192.30.252.131)' can't be established.
RSA key fingerprint is 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi akiyoko! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

4. private リポジトリを作成する(GitHub側)

「Repositories」タブから、「New」ボタンをクリック。

f:id:akiyoko:20140304072304p:plain


リポジトリ情報を適宜入力して、「Create repojitory」をクリック。

Repository name 任意(空白を含まない方がよい)
Type Private
Initialize this repository with a README チェックを入れると、Markdown形式の「README.md」が作成され、「Initial commit」とのコメントで最初のコミットがされた状態でリポジトリが作成される。

f:id:akiyoko:20140304072318p:plain

f:id:akiyoko:20140304072438p:plain

なお、「Initialize this repository with a README」のチェックを外すと、何もコミットされていない状態のリポジトリが作成されるので、clone後に「git init」が必要となります。


5. cloneしてみる(Mac側)

$ git clone github:akiyoko/testproject.git
Cloning into 'testproject'...
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4/4), done.
Checking connectivity... done

これで、毎回パスフレーズを入力しなくてもよくなりました。


ここで注意!
Macで操作していると、一度パスフレーズを入力すると、キーチェーンにパスワードが登録されてしまうようです。
そのままでは SSH鍵の設定がうまく行っているのか分かりづらいので、一旦「キーチェーンアクセス」から「github.com」のエントリを削除しておくとよいです。

f:id:akiyoko:20140304072328p:plain



参考

おまけ

# 公開鍵の fingerprint を確認
$ ssh-keygen -lf ~/.ssh/github_rsa.pub

# 秘密鍵から公開鍵を作成(公開鍵を亡くしちゃったとき)
$ ssh-keygen -yf ~/.ssh/github_rsa > ~/.ssh/github_rsa.pub