I'm trying to use Jsch to establish an SSH connection in Java. I have set "StrictHostKeyChecking" to yes. I understand that the hostkey of the server has to be obtained before hand and store in the hostkey file before the first attempt to connect to the server. How can I get the HostKey of the server. My code produces the following exception:
com.jcraft.jsch.JSchException: UnknownHostKey: ASY-PC RSA key fingerprint is 22:fb:ee:fe:18:cd:aa:9a:9c:78:89:9f:b4:78:75:b4
How can I make connection with StrictHostKeyChecking Yes. Here is my code.
package sshexample;
import com.jcraft.jsch.*;
import java.io.*;
public class SSHexample
{
public static void main(String[] args)
{
String user = "user";
String password = "password";
String host = "192.168.100.103";
int port=22;
try
{
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "yes");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
}catch(Exception e) {
e.printStackTrace();
}
}
Maybe it's no more relevant but in my case, the similar problem was happened with docker-compose.yml in container, that was build from spring-boot application that 100 % worked locally
Solution was to apply hack on ~/.ssh folder.
After that I suppose volumes was correctly mapped between docker container and local machine, and described exception was gone.
After a few minutes of testing i found a solution for this. If you don't want to use the default
knownHost
File, just create your ownThis how the file could look:
And all those entries are separated by new lines. You get the RSA key that you want by asking your session:
So
session.getHostKey().getKey()
is what you want to call to get the key.You also need to call
session.connect();
before you ask for the key and handle it in the catch.As most the of the answers suggest you have to provide know host file but fail to address how to get it. You simply need to SSH to the host.
Eg
ssh user@192.168.100.103
when prompted provide password. For first time connection it will you to save the hosts ssh key fingerprint. Once you are connected you can find your known_host file at
For me on windows path is
C:\Users\athakur\.ssh\known_hosts
. You can directly use this file. Or edit the file and pick up entry from it corresponding to your IP address which would look something likeNote : Host machines SSH fingerprint (based on hosts public key that you can find at
/etc/ssh/ssh_host_rsa_key.pub
) may change in SSH is reinstalled in that machine. Or you may encounter MIM attack (even it is for testing sake). In such cases you will have to pick new entry in same way mentioned above.You have to supply a KnownHostKeys file by calling following function
jsch.setKnownHosts(new FileInputStream(knownHostsFile));
this file should have all the the known hosts' fingerprints separated by new lines.
for example
you can obtain this key from server by using any sftp client however following command may help if you are using linux or unix