Closed ericwq closed 9 months ago
after see this x/crypto/ssh: add support for hostkeys@openssh.com, I don't know if this is the reason: x/crypto/ssh
doesn't support hostkeys@openssh.com yet.
I believe this is due to the behavior of golang.org/x/crypto/ssh -- the ClientConfig.HostKeyCallback will only be called once, even if the server has multiple public keys. The ClientConfig.HostKeyAlgorithms specifies the list of acceptable algorithms, in order of preference. When a server has multiple acceptable public keys, only one will be chosen, deterministically based on that ordering. This single public key is then passed to the HostKeyCallback, which either accepts (nil return) or rejects (non-nil error return) this server key, but either way it does not receive any other server keys or get called multiple times.
github.com/skeema/knownhosts provides a benefit in a different but slightly related scenario: server has multiple public keys, and some (but not all) of those keys are already in the knownhosts file. In this case github.com/skeema/knownhosts is useful for easily populating ClientConfig.HostKeyAlgorithms based on the already-known keys from the knownhosts file.
However, your situation here is different, if I understand correctly: server has multiple public keys, none of which are in the knownhosts file yet, and you wish to add all of them to the knownhosts file. github.com/skeema/knownhosts cannot directly help with this situation, because it doesn't control how or when the HostKeyCallback function is invoked by logic in golang.org/x/crypto/ssh.
I don't know if there's any way in golang.org/x/crypto/ssh to obtain the server's full list of public keys from a single client connection. In theory you could probe a server by making a bunch of different client connections, shrinking the HostKeyAlgorithms list each time, but this is hacky and grossly inefficient.
Sorry that github.com/skeema/knownhosts can't solve this directly. If you do find a good solution, please comment back here, I'd be curious to know too. Thanks!
thanks for your patient reply. it helps me a lot. I will pay attention to this problem. will notify you if it make any progress.
base known_hosts
here is the base
~/.ssh/known_hosts
file.output from ssh client
Then I login with
ssh -p 8022 ide@localhost
:known_hosts changed by ssh client
here is the
~/.ssh/known_hosts
file after ssh client login. we notice that 3 lines of [localhost]:8022 was added.my application
then I restore
~/.ssh/known_hosts
file to remove the lines added by ssh client. finally, I use the following code to call my application (as bellow).output of my application
here is the output of my application.
known_hosts changed by my application
here is the
~/.ssh/known_hosts
file after run my application. There is only one line (ecdsa-sha2-nistp256) was added.compare with ssh client
key return to client
ssh client and my application return different keys for user to choose. different key has different fingerprint.
hostkeys in known_host
~/.ssh/known_hosts
file. it's ssh-ed25519, ssh-rsa, ecdsa-sha2-nistp256.~/.ssh/known_hosts
file, it's ecdsa-sha2-nistp256@evanelias @lonnywong do you have any idea/suggestion about the compare result?