How to find public key of the server using any SFTP/SSH client

public-keysftpssh

Is there any way to get SFTP server's public key from any Sftp client or by using any tool? I have access (username/password) to connect to production SFTP remote server and I would like to find the public key from any SFTP client or by using any other tool. My intention is to use that public key in Java JSCh library and to connect to the SFTP server.

Best Answer

The only correct way is to ask the server administrator to provide you the host key.

If you retrieve the host key remotely yourself, you cannot really trust the key, as you can be under a Man-in-the-middle attack already.


Anyway, the easiest way is to use ssh-keyscan tool:

> ssh-keyscan example.com
# example.com:22 SSH-2.0-OpenSSH_5.3
example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0hVqZOvZ7yWgie9OHdTORJVI5fJJoH1yEGamAd5G3werH0z7e9ybtq1mGUeRkJtea7bzru0ISR0EZ9HIONoGYrDmI7S+BiwpDBUKjva4mAsvzzvsy6Ogy/apkxm6Kbcml8u4wjxaOw3NKzKqeBvR3pc+nQVA+SJUZq8D2XBRd4EDUFXeLzwqwen9G7gSLGB1hJkSuRtGRfOHbLUuCKNR8RV82i3JvlSnAwb3MwN0m3WGdlJA8J+5YAg4e6JgSKrsCObZK7W1R6iuyuH1zA+dtAHyDyYVHB4FnYZPL0hgz2PSb9c+iDEiFcT/lT4/dQ+kRW6DYn66lS8peS8zCJ9CSQ==

If you are on Windows, you can get a Windows build of ssh-keyscan from Win32-OpenSSH project or Git for Windows. It's built-in in recent versions of Windows 10.


For more options and background information, see my article Where do I get SSH host key fingerprint to authorize the server?

Related Question