SSH でログインするときは user@host とか、あるいは -l
オプションとかでアカウント名を指定します。
$ ssh ore@example.com # Logged in as ore $ ssh are@example.com # Logged in as are
が、たまに同じアカウント名でも認証に使用された公開鍵で実際のアカウントを識別するサービスがあります(GitHub とか BitBucket とかそういうの)。
$ ssh git@example.com -i ~/.ssh/ore.key # Logged in as ore $ ssh git@example.com -i ~/.ssh/are.key # Logged in as are
このままだと毎回 -i
で秘密鍵を指定しなければならないので面倒です。なので大抵は次のように ~/.ssh/config
で鍵を使い分けていると思います。
~/.ssh/config
Host example-ore User git HostName example.com IdentitiesOnly yes IdentityFile ~/.ssh/ore.key Host example-are User git HostName example.com IdentitiesOnly yes IdentityFile ~/.ssh/are.key
$ ssh example-ore # Logged in as ore $ ssh example-are # Logged in as are
やりたいこと
host-A はデスクトップとかノートとか、host-B は自分用の開発環境とか、example.com はいわゆる git なサーバです。
- host-A から host-B に SSH でログインする
- host-B で ore.key と are.key を使い分けて example.com にログインしたい
- example.com の認証にはエージェントフォワーディングを利用する
- ssh-agent は host-A で起動する
- example.com の認証にはエージェントフォワーディングを利用する
- 秘密鍵は host-A にしか置きたくない
- host-A の ssh-agent には ore.key と are.key の両方を追加しておく
このようなケースだと host-A の ~/.ssh/config
に前述のように書いていても example.com への認証には使われませんし、
host-B には秘密鍵が無いので ~/.ssh/config
で鍵を切り替えることもできません。
そこで host-B には公開鍵だけを保存して ~/.ssh/config
の IdentityFile で公開鍵を指定します。
~/.ssh/config
Host example-ore User git HostName example.com IdentitiesOnly yes IdentityFile ~/.ssh/ore.key.pub Host example-are User git HostName example.com IdentitiesOnly yes IdentityFile ~/.ssh/are.key.pub
すると example.com の認証で、host-A の ssh-agent が持っている秘密鍵の中から、host-B の ~/.ssh/config
の IdentityFile で指定された公開鍵とペアであるもの、が使われるようになります。
[ore@host-A ~]$ ssh -A host-B [ore@host-B ~]$ ssh example-ore # Logged in as ore [ore@host-B ~]$ ssh example-are # Logged in as are