简体   繁体   English

Java ssh 使用 JSch 连接密钥 - 身份验证失败

[英]Java ssh connect with key using JSch - Auth Failed

I'm trying to connect to localhost using ssh with key but i still get the "Auth Failed" error.我正在尝试使用带密钥的 ssh 连接到本地主机,但我仍然收到“身份验证失败”错误。

Here is the method implementation:下面是方法实现:

public void downloadUsingPublicKey(String username, String host)
    {

         String privateKey = "~/.ssh/id_rsa";

         JSch jsch = new JSch(); 
         Session session = null; 
         Channel channel = null; 
         ChannelSftp channelSftp = null; 
         try 
         { 

             jsch.addIdentity(privateKey); 
             System.out.println("Private Key Added."); 


             session = jsch.getSession(username, host); 
             System.out.println("session created."); 

             java.util.Properties config = new java.util.Properties(); 
             config.put("StrictHostKeyChecking", "no"); 

             session.setConfig(config); 
             session.connect(); 

             channel = session.openChannel("sftp"); 
             channel.connect(); System.out.println("shell channel connected...."); 

             channelSftp = (ChannelSftp)channel; 
             channelSftp.cd(Config.dir); 

             System.out.println("Changed the directory..."); 
         } catch (JSchException e) 
         { 
             // TODO Auto-generated catch block 
             e.printStackTrace(); 
         } catch (SftpException e) 
         { 
             // TODO Auto-generated catch block 
             e.printStackTrace(); 
         }finally
         { 
             if(channelSftp!=null)
             { 
                 channelSftp.disconnect(); 
                 channelSftp.exit(); 
             } 
             if(channel!=null) channel.disconnect(); 
             if(session!=null) session.disconnect(); 
         } 
    }

I've created my public/private key pair using linux terminal, as follows:我已经使用 linux 终端创建了我的公钥/私钥对,如下所示:

ssh-keygen -t rsa -b 4096 -C "myemail@email.com"

I didn't put any phrase.我没有放任何词组。 Next step:下一步:

ssh-add ~/.ssh/id_rsa

And finally最后

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Then when i run my program i get error:然后当我运行我的程序时出现错误:

com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:512)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at pl.eroj.filedownloader.Downloader.downloadUsingPublicKey(Downloader.java:73)
    at pl.eroj.filedownloader.Downloader.main(Downloader.java:107)

Any ideas?有任何想法吗? My key is of OpenSSH type starting with line "-----BEGIN RSA PRIVATE KEY-----"我的密钥是 OpenSSH 类型的,以“-----BEGIN RSA PRIVATE KEY-----”行开头

This how I connected localy using with Path of private Key when这是我如何使用私钥路径连接本地的
privateKeyPath ="C:\\Keys\\private_key.ppk" privateKeyPath ="C:\\Keys\\private_key.ppk"

    public static OutputStream ConnectionUsingKey(String user, String hostName, String privateKeyPath)
        throws JSchException, IOException {
    JSch jsch = new JSch();
    Session session = null;
    try {

        jsch.addIdentity(privateKeyPath);
        session = jsch.getSession(user, hostName, 22);
        session.setConfig("PreferredAuthentications", "publickey");
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        if (session.isConnected() == true) {
            System.out.println("Connection to Session server is successfully");
        }
        channel = session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect(30 * 1000);
        return channel.getOutputStream();

    } catch (JSchException e) {
        throw new RuntimeException("Failed to create Jsch Session object.", e);
    }
}

Thank you @Vladi.谢谢@Vladi。 In my case I needed to connect to a server that used both private key and a password.就我而言,我需要连接到同时使用私钥和密码的服务器。 Hence I had to set the Config like this因此我不得不像这样设置配置

        session.setConfig("PreferredAuthentications", "publickey,password");
        session.setPassword("some_password");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM