How to print the SSH host key as a client

Jente Hidskes jente.hidskes at
Wed Feb 9 10:17:05 CET 2022

Hey folks,

I am working on a small SFTP client using libssh2. I would like for
this client to persist the host key of the server it is connecting to,
but I seem to be unable to get a hold of it.

After initialising libssh2 and calling libssh2_session_handshake, I am
able to print the host key fingerprint through libssh2_hostkey_hash as
in the example programs. I assumed I would be able to print the actual
host key rather than the fingerprint with libssh2_session_hostkey, but
that returns an empty string.

I suspect that libssh2_session_hostkey only returns the local host key
when operating as a server (given this[1] source code comment), but
the docs[2] are unclear about this: they just state "get the remote
key" and "returns a pointer to the current host key".

Am I doing something wrong, or is it indeed impossible right now to
print the host key when operating as a client?

Kind regards,



More information about the libssh2-devel mailing list