问题
I'm trying to use JSch with a private key configuration. I've generated a public and private key using PuTTYgen but am unsure what to do with both of the files.
Which key (public/private) needs transferring to the server?
回答1:
First, you need to register your PuTTYgen-generated public key on the server. See Getting ready for public key authentication or (my) Set up SSH public key authentication.
And finally see Can we use JSch for SSH key-based communication? for details on using the private key in JSch.
Make sure you use the latest version of JSch, as older versions do not support the .ppk format natively.
回答2:
Code Snippet for connection using PuTTy private Key (.ppk)
JSch jsch=new JSch();
jsch.setKnownHosts("~\.ssh\know_hosts");
jsch.addIdentity("~\sshkey.ppk");
Session session=jsch.getSession("ec2-user", "54.12.11.90", 22);
session.setConfig("PreferredAuthentications", "publickey");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel=session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(3*1000);
Have used 0.1.54 version of Jsch
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
来源:https://stackoverflow.com/questions/28442505/jsch-how-to-use-with-putty-private-key