Monday, March 16, 2009

073 结合ssh-agent 使用 ssh-copy-id

结合ssh-add/ssh-agent 使用 ssh-copy-id
当我们没有给 -i参数传递值,或者~/.ssh/identity.pub 不存在,我们会得到下面的异常消息
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-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.
[注意:这里加载了通过 ssh-add -L得到的密钥]

有关ssh-copy-id 三个需要小心的地方
  1. 缺省的公钥: ssh-copy-id使用~/.ssh/identity.pub作为缺省的公钥文件(比如,当你每个-i参数传值时)。但是,可能我想使用id_dsa.pub, or id_rsa.pub, 或者identity.pub作为缺省的公钥文件。如果这些文件存在,默认使用的是identity.pub文件。
  2. 未指定代理:如果在运行ssh-agent 时ssh-add -L 返回“The agent has no identities”(比如,ssh-agent没有密钥),ssh-copy-id 仍然会把“The agent has no identities”复制到远程主机的认证密钥文件中。
  3. 认证密钥文件中存在重复的密钥:我希望ssh-copy-id 命令可以识别出远程主机上的认证密钥文件中重复的密钥。但是如果你重复执行ssh-copy-id ,他会在认证密钥文件中增加多个重复的密钥而不会检查他们是否重复。尽管这样不会影响我们的工作,但是这样确实让认证文件看起来很凌乱。

No comments:

Post a Comment