我正在使用下面的代码来连接远程服务器,我按照下面的步骤连接私钥/公钥生成和连接的公钥与授权密钥。
码:
private Session createSession() throws JSchException { JSch jsch = new JSch(); jsch.addIdentity(privateKey); //add private key path as ~/.ssh/id_rsa Session session; session = jsch.getSession(user, host, port); java.util.Properties config = new java.util.Properties(); Properties cfg = new Properties(); cfg.put("trust", "true"); cfg.put("StrictHostKeyChecking", "no"); cfg.put("HashKnownHosts", "yes"); session.setConfig(cfg); session.connect(); return session; }
脚步:
1. I can able to generate private/public key in the path ~/.ssh/id_rsa(private key) and ~/.ssh/id_rsa.pub(public key) >> ssh-keygen (or) ssh-keygen -t rsa -b 4096 Note: Generated key with no passphrase 2. I have added public key with authorized_keys with below command >> ssh user@host "echo \"`cat ~/.ssh/id_rsa.pub`\" >> .ssh/authorized_keys"
仍然我面临exception“com.jcraft.jsch.JSchException:身份validation失败”。 请指导我继续。