JSch: UnknownHostKey exception, even if there is a host key fingerprint in the known_hosts file

There are already two questions about this exception:

I am using a Windows machine and trying to connect to a virtual machine created using Vagrant running Ubuntu. Here is my code:

public static void main(String[] args) {
    String host = "localhost";
    String username = "vagrant";
    int port = 2200;
    String privateKey = "C:\\keys\\openSSH_pair1\\open_ssh_private";
    JSch js = new JSch();
    try {
        js.addIdentity(privateKey, "pass");
        js.setKnownHosts("C:\\Users\\user\\.ssh\\known_hosts");
        Session session = js.getSession(username, host, port);
        session.connect();
        System.out.println("Connected");
    } catch (JSchException e) {
        e.printStackTrace();
    }
}

@Pascal suggests installing strictHostKeyCheckingin no, which works for me, but this is not the preferred solution. His preferred solution is command line SSH, so the host will be added to the file known_hosts. I installed and executed Git ssh -i openSSH_pair1\open_ssh_private vagrant@localhost -p 2200 and got this output before being prompted to pass the phrase and establish a connection

'[localhost]: 2200 ([127.0.0.1]: 2200)' . ECDSA 11: 5d: 55: 29: 8: 77: d8: 08: 4: 00: 9b: 3: 61: 93: Fe: 5. (/)? : '[localhost]: 2200' (ECDSA) .

, known_hosts git_home\.ssh localhost:2200, known_hosts user_home\.ssh. , ssh, authorized_keys

ssh-keygen -y -f open_ssh_private > open_ssh_gen.pub
cat open_ssh_gen.pub >> ~/.ssh/authorized_keys

com.jcraft.jsch.JSchException: UnknownHostKey: localhost. RSA key fingerprint is 50:db:75:ba:11:2f:43:c9:ab:14:40:6d:7f:a1:ee:e3
    at com.jcraft.jsch.Session.checkHost(Session.java:797)
    at com.jcraft.jsch.Session.connect(Session.java:342)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at connect.Main.main(Main.java:24)

,

js.setKnownHosts("C:\\Users\\user\\.ssh\\known_hosts");
+4
1

, ECDSA known_hosts, ssh :

ECDSA: 11: 5d: 55: 29: 8a: 77: d8: 08: b4: 00: 9b: a3: 61: 93: fe: e5.

JSch RSA, known_hosts:

RSA 50: db: 75: ba: 11: 2f: 43: c9: ab: 14: 40: 6d: 7f: a1: ee: e3


, , JCE, ECDSA In JSch.

. JSch.


ssh RSA -o HostKeyAlgorithms=ssh-rsa.

. SSH RSA ECDSA?

+9

Source: https://habr.com/ru/post/1621815/


All Articles