只加载私钥时ssh-copy-id如何获取公钥?

Eso*_*ric 2 ssh key-authentication

当我为用户加载了私钥时,我可以运行ssh-copy-id user@remotehostname并提示输入密码。当我输入正确的密码时,我现在可以使用密钥登录。我以前总是将此命令-i <path to public key>作为参数运行,但现在意识到我不需要公钥的路径。

当我以 user@hostname 身份登录时,我查看 .ssh/authorized_keys 文件,并看到我的私钥匹配的公钥,这让我感到困惑——我从未提供过公钥。

当我运行命令时,ssh-copy-id 如何知道哪个公钥与我在本地加载的私钥匹配?当我不提供它时,它如何知道要添加到授权密钥文件中的内容?

我希望我很清楚——我一直在运行ssh-copy-id -i <public key>,这对我来说是有道理的——它登录并将公钥复制到授权密钥文件中。但是如果我不提供公钥(即我ssh-add <private key>在运行 ssh-copy-id 之前运行),只要我加载了私钥,它仍然可以工作,而且我不明白它是如何获取公钥的。

编辑:澄清一下,我没有保留默认的 id*.pub 命名约定。所以我看到的关于在手册页中搜索 id*.pub 的逻辑似乎并不适用。事实上,我可以创建一个名为 randompair 的密钥对,加载 randompair,将 rendompair.pub 重命名为 newname.pub,运行 ssh-copy-id 并且它仍然加载正确的公钥。

查看 bash 脚本本身让我对它如何实现这一点感到有些困惑。

Gil*_*il' 5

这在最近系统的手册页中有很好的记录。请注意,该脚本有多个不同版本;Arch LinuxRHEL/CentOS似乎与Debian/Ubuntu具有相同的版本,但FreeBSD 的选项略有不同。

By default, ssh-copy-id calls ssh-add -L to list the keys that you have registered in the SSH agent. ssh-add -L outputs a list of public keys for which you have the private key in the agent. You might wonder how the agent can do this since you don't pass it a public key either. The answer is that it's always possible to reconstruct the public key from the private key (this is true of all the cryptosystems that SSH supports and most that it doesn't). This is only true of the “mathematical” part of the key, however. The public key file can also contain a comment (which you can set with ssh-keygen -C), and the agent does not load this comment, so if you use ssh-copy-id and it takes a key via the agent, the remote host won't have this comment in authorized_keys.

如果没有正在运行的代理或它没有任何密钥,请使用最近的 Linuxssh-copy-id查找(直接来自手册页

匹配的最新文件:~/.ssh/id*.pub, (不包括那些匹配的文件~/.ssh/*-cert.pub)因此,如果您创建的密钥不是您想要ssh-copy-id使用的touch(1)密钥,只需在您喜欢的密钥.pub文件上使用以将其恢复为最新的。

旧版本的脚本和非 Linux 版本没有这种最新文件行为。据我所知,即使是旧版本也没有探测代理,默认情况下只是读取默认路径~/.ssh/id_rsa.pub