Hack 73. ssh-agent と共に ssh-copy-id を使用する

ssh-add/ssh-agent と共に ssh-copy-id を使用する

オプション -i に何も値が渡されないと、 ~/.ssh/identity.pub が有効ではない場合に
ssh-copy-id は次のようにエラーメッセージを表示します。

jsmith@local-host$ ssh-copy-id -i remote-host
/usr/bin/ssh-copy-id: ERROR: No identities found

ssh-add を使用して ssh-agent にキーをロードしている場合、
ssh-copy-id は ssh-agent からキーを取得しリモートホストにコピーします。
たとえば、ssh-copy-id にオプション -i を渡さない場合、
ssh-add -L コマンドで与えられたキーをリモートホストにコピーします。

jsmith@local-host$ ssh-agent $SHELL
jsmith@local-host$ ssh-add -L
The agent has no identities.

jsmith@local-host$ ssh-add
Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)

jsmith@local-host$ ssh-add -L
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79D
aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa

jsmith@local-host$ ssh-copy-id -i remote-host

jsmith@remote-host’s password:
Now try logging into the machine, with “ssh ‘remote-host’”, 
and check in: .ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting.
[Note: これは ssh-add -L によって表示されたキーを追加します]

ssh-copy-id について 3 つのちょっとしたお知らせ

次は ssh-copy-id についてのちょっとしたお知らせです。
1. デフォルトの公開鍵: ssh-copy-id は(オプション -i に何も渡されない場合)公開鍵ファイルとして ~/.ssh/identity.pub を使用します。
代わりに、デフォルトの鍵として id_dsa.pub または id_rsa.pub または identity.pub を使用します。
どれか 1 つが存在している場合、リモートホストにそのファイルをコピーすべきです。
2, 3 存在している場合、デフォルトとして identity.pub をコピーすべきです。

2. エージェントは ID をもたない: ssh-agent が実行中で ssh-add -L が“The agent has no identities” を返すとき
(鍵は ssh-agent に追加されません)、
ssh-copy-id は“The agent has no identities” というメッセージをリモートホストの authorized_keys エントリにコピーします。

3. authorized_keys 内の重複エントリ: ssh-copy-id がリモートホストの authorized_keys 上で重複したエントリをチェックしてほしいと思います。
ローカルホスト上で何度も ssh-copy-id を実行した場合、
重複をチェックせずにリモートホスト上の authorized_keys ファイルに同じ鍵を追記しつづけます。
重複したエントリをもっていても期待通りに動作します。
しかし、authorized_keys ファイルを整理しておきたいです。