.ssh/configで変なHostを設定せずにGithubの複数のアカウントを使い分ける

Githubのアカウントが複数になってくると、結構厄介ですよね。

githubで複数ユーザを使い分ける
http://d.hatena.ne.jp/monjudoh/20110411/1302521587

ここにあるように.ssh/configにHostエントリを書けば出来るんですが、githubのページに表示されているclone用のレポジトリの文字列を書き換えなきゃ成らないのが億劫です。

要するに鍵の指定を出来れば良い訳なので、そこで、

git over ssh with options
http://hisashim.org/2010/02/23/git-ssh-options.html

を参考にして、GIT_SSHをいい感じに設定してくれるスクリプトを作ってみました。

gitsshm: GIT_SSH Manager for github

使い方

1.鍵を配置する。

まず使いたい鍵は ~/github-keys/にこんな感じで入れておきます。

~/github-keys/ 
+- user_A/
     +- id_rsa
     +- id_rsa.pub
+- user B/
     +- id_rsa
     +- id_rsa.pub
2.セットアップする。

~/github-keys直下の各usernameに対して、~/github-keys/.gitsshm/username-ssh.shという名前のスクリプトを生成してくれます。中身はこんな感じ。

exec /usr/bin/ssh -i /Users/everpeace/github-keys/username/id_rsa "$@"

sshの公開鍵認証はssh-agentを使う事でいちいち秘密鍵パスフレーズを入力することが省略できます。各鍵をagentに登録する機能も付けてみました。

こんな感じで使います。

$ source githsshm setup
GIT_SSH is not set now.
WARNING: GIT_SSH may not be maintained by gitsshm.  GIT_SSH will be overwritten.
mkdir /Users/everpeace/github-keys/.gitsshm
all keys in /Users/everpeace/github-keys will be added to ssh_agent
Enter passphrase for /Users/everpeace/github-keys/user_A/id_rsa: <-パスフレース入力 
Identity added: /Users/everpeace/github-keys/user_A/id_rsa (/Users/everpeace/github-keys/user_A/id_rsa)
create /Users/everpeace/github-keys/.gitsshm/user_A-ssh.sh
Enter passphrase for /Users/everpeace/github-keys/user_B/id_rsa: <-パスフレース入力 
Identity added: /Users/everpeace/github-keys/user_B/id_rsa (/Users/everpeace/github-keys/user_B/id_rsa)
create /Users/everpeace/github-keys/.gitsshm/user_B-ssh.sh

Now you can switch GIT_SSH by -s action for available usernames:
user_A
user_B

current GIT_SSH is not set.

ssh-agentに登録されている鍵はこんな感じで確認できます。

$ source gitsshm listkeys
2048 84:.....:80 /Users/everpeace/github-keys/user_A/id_rsa (RSA)
2048 84:.....:91 /Users/everpeace/github-keys/user_B/id_rsa (RSA)
3.ユーザをスイッチする。

これで、準備ができました。

$ source gitsshm switch everpeace
GIT_SSH is not set now.
WARNING: GIT_SSH may not be maintained by gitsshm.  GIT_SSH will be overwritten.
current GIT_SSH is /Users/everpeace/github-keys/.gitsshm/user_A-ssh.sh

これで、gitでssh接続する時にuser_Aで認証できてると思います。

スイッチ可能なユーザはこんな感じで確認できます。

$ source gitsshm list
available usernames:
user_A
user_B

current GIT_SSH is /Users/everpeace/github-keys/.gitsshm/user_A-ssh.sh

今の設定はこんな感じで確認できます。

$ source gitsshm current
current GIT_SSH is /Users/everpeace/github-keys/.gitsshm/user_A-ssh.sh
4.リセットする。

ssh-agentから鍵を削除して、ラッパースクリプトも解除してGIT_SSHを削除します。

$ source gitsshm reset
detected GIT_SSH is maintained by chgitssh.
Identity removed: /Users/everpeace/github-keys/user_A/id_rsa (/Users/everpeace/github-keys/user_A/id_rsa.pub)
Identity removed: /Users/everpeace/github-keys/user_B/id_rsa (/Users/everpeace/github-keys/user_B/id_rsa.pub)
remove dir /Users/everpeace/github-keys/.gitsshm
GIT_SSH is not set now.

注意

  • このスクリプトはGIT_SSHを上書きしてまうので注意してください。
  • 環境変数を書き換えるので、sourceつけないと駄目です。
  • でも、alias gitsshm='source path/to/gitsshm'しておくとコマンドっぽく使えます。
  • ラッパースクリプトを生成してそれを指定してるだけなので、別にgithubじゃなくても使えます。
  • ssh-agent使ってないと落ちちゃうので気をつけて下さい。

コード

Gistにのせてありますので良ければ活用してください

追記

githubのレポジトリにもしてみました。よければフォークしてね。
https://github.com/everpeace/gitsshm