Alex Alex - 1 year ago 230
Java Question

com.jcraft.jsch.JSchException: UnknownHostKey

I'm trying to use Jsch to establish an SSH connection in Java. My code produces the following exception:

com.jcraft.jsch.JSchException: UnknownHostKey:
RSA key fingerprint is 22:fb:ee:fe:18:cd:aa:9a:9c:78:89:9f:b4:78:75:b4

I cannot find how to verify the host key in the Jsch documentation. I have included my code below.

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class ssh {
public static void main(String[] arg) {

try {
JSch jsch = new JSch();

//create SSH connection
String host = "";
String user = "username";
String password = "123456";

Session session = jsch.getSession(user, host, 22);

} catch(Exception e) {

Answer Source

I would either:

  1. Try to ssh from the command line and accept the public key (the host will be added to ~/.ssh/known_hosts and everything should then work fine from Jsch) -OR-
  2. Configure JSch to not use "StrictHostKeyChecking" (this introduces insecurities and should only be used for testing purposes), using the following code:

    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");

Option #1 (adding the host to the ~/.ssh/known_hosts file) has my preference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download