0

I was trying to scp using key-pair between 2 linux server. I was trying to follow http://sthen.blogspot.in/2008/03/sftp-i-java-with-jsch-using-private-key.html

I am able to scp file between linux server but when I execute code I am getting error com.jcraft.jsch.JSchException: UnknownHostKey. any idea what I am doing wrong.

Process : I have genrated passpharase free RSA key on client and copy public key on destination. now trying to scp file from client to destination using JCraft Librabry

I am using jar latest jar X.51

user3493965
  • 5
  • 1
  • 4

1 Answers1

0

Your client simply have no know host key from the server side. There are two things that can be done: Either you tell JSch not to check host key at all or supply a valid key.

Not checking HostKeys

In this case you have to supply a configuration with option StrictHostKeyChecking set to no:

...
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
Hashtable<String, String> config = new Hashtable<String, String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
...

Supply HostKeys

In this case you supply a file with your host keys. The file is a standard OpenSSH known hosts key file and can be created and updated with the standard ssh command on Linux or cygwin.

...
JSch jsch = new JSch();
jsch.setKnownHosts(knownHostsFilename);
Session session = jsch.getSession(user, host, port);
Hashtable<String, String> config = new Hashtable<String, String>();
config.put("StrictHostKeyChecking", "yes");
session.setConfig(config);
session.connect();
...

My personal advice is to use the second way, as known host keys are an additional security feature of ssh you shouldn't abandon if not necessary.

PS: You shouldn't use only docs that are over 6 years old.

blafasel
  • 1,091
  • 14
  • 25
  • Thanks blafasel for your quick response. So If I am trying to scp from server A to B then A must contain detail for B in its know_host file and this file of server A need to configure in JCraft.setKnownHosts(filename); – user3493965 Jul 01 '14 at 10:43