[[ともっくす alloc] init]

ともっくすの雑多な日記と技術的なメモ

GitHubの2つ目のアカウントについてSSH接続とかしたい

今まで使ってたGitHubアカウントとは別に新しくアカウントを作りたいなーってことで作った.

これまで使ってたやつはアカウント毎削除してしまってもいいんだけど,とりあえず残しておくことに.

で,新しいGitHubアカウントを作ったものの,このままだとgit pushとか出来ないので,SSH接続の設定をする.

でも,複数アカウントでも出来るのか.

こちらを参考にさせていただきました.
> 【メモ】githubの複数アカウントにSSH接続するための設定手順 | Developers.IO


鍵の生成からGitHubへの登録まで

まず,GitHubアカウントに接続するための公開鍵と秘密鍵を生成する.

鍵の名前は今回はgithub-secondとする.

$ cd ~/.ssh/
$ ssh-keygen -t rsa -C "email@email.com" -f github-second
Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (~~~): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
$ ls
github-second.pub github-second
$ pbcopy < ~/.ssh/github-second.pub

これで,github-secondの鍵が出来,クリップボードへのコピーも出来た.*1


次に,GitHubに鍵を登録する.

Sign in · GitHubにアクセスし,Add SSH Keyを選択し,Titleを記入し,Keyにペーストし,Add Keyを押す.
f:id:o_tomox:20131007155848p:plain

これで,登録完了.


ホストの指定から接続まで

次に,SSH Configファイルを設定する.

GitHubの新しいアカウント名をo-tomoxとすると,以下のように追記する.

$ emacs ~/.ssh/config
(略:他のこれまでの設定)
Host github-o-tomox
  User git
  Port 22
  HostName github.com
  IdentityFile ~/.ssh/github-second
  TCPKeepAlive yes
  IdentitiesOnly yes

Hostをgithub-[アカウント名],IdentityFileを生成した秘密鍵へのパスとして設定する.


これで,SSHの設定はできたので,接続してみる.

$ ssh -T github-o-tomox
The authenticity of host github-private can't be established.
RSA key fingerprint is ...
Are you sure you want to continue connecting (yes/no)?
$ yes
Hi o-tomox! You've successfully authenticated, but Github does not provide shell access.

これで接続できた.


リポジトリにアクセスする

例えば,gittestというリポジトリをremoteとしてoriginに登録する場合は,普通はこのようにする.

$ git add remote origin git@github.com:o-tomox/gittest.git

しかし,今回はSSH Configにホストとしてgithub-o-tomoxで登録しているので,

$ git add remote origin git@github-otomox:o-tomox/gittest.git

とする必要がある.らしい.

でも,完全に理解しているわけではない…



まあ,これで,GitHubの複数アカウントに対応できる.

*1:pbcopyによるクリップボードへのコピーはMacのみ