SFTP with java client when keys are shared [duplicate]












0
















This question already has an answer here:




  • Can we use JSch for SSH key-based communication?

    1 answer




I have two servers A and B.
I want to SFTP a file from server A to B.



Public key of server A (~/.ssh/id_rsa.pub) has been added to the ~/.ssh/authorized_keys of server B.



From command line, I can SFTP from server A to B without entering password.
However, from a Java client using library Jsch I am unable to make SFTP connection to server B and I am getting authentication error:



Error occurred during SFTP. Auth fail
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:519)
at com.jcraft.jsch.Session.connect(Session.java:183)
at Main.main(Main.java:15)


Is there a way I can connect to server B for SFTP purposes using Java client without specifying password?



Below is my Java code for reference:



import com.jcraft.jsch.*;

public class Main {

public static void main(String args) {

JSch jsch = new JSch();
Session session = null;

try {
session = jsch.getSession("processor", "remoteserver.myorg.com", 22);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Trying to connect...");
session.connect();
System.out.println("Connected successfully.");

Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println("Doing SFTP...");
sftpChannel.put("/tmp/test.txt", "/some/remote/folder");
System.out.println("Success");
sftpChannel.exit();
session.disconnect();

} catch (JSchException | SftpException e) {
System.err.println("Error occurred during SFTP. " + e.getMessage());
e.printStackTrace();
}

}
}









share|improve this question













marked as duplicate by Martin Prikryl sftp
Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Can we use JSch for SSH key-based communication?

      1 answer




    I have two servers A and B.
    I want to SFTP a file from server A to B.



    Public key of server A (~/.ssh/id_rsa.pub) has been added to the ~/.ssh/authorized_keys of server B.



    From command line, I can SFTP from server A to B without entering password.
    However, from a Java client using library Jsch I am unable to make SFTP connection to server B and I am getting authentication error:



    Error occurred during SFTP. Auth fail
    com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:519)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at Main.main(Main.java:15)


    Is there a way I can connect to server B for SFTP purposes using Java client without specifying password?



    Below is my Java code for reference:



    import com.jcraft.jsch.*;

    public class Main {

    public static void main(String args) {

    JSch jsch = new JSch();
    Session session = null;

    try {
    session = jsch.getSession("processor", "remoteserver.myorg.com", 22);
    session.setConfig("StrictHostKeyChecking", "no");
    System.out.println("Trying to connect...");
    session.connect();
    System.out.println("Connected successfully.");

    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    System.out.println("Doing SFTP...");
    sftpChannel.put("/tmp/test.txt", "/some/remote/folder");
    System.out.println("Success");
    sftpChannel.exit();
    session.disconnect();

    } catch (JSchException | SftpException e) {
    System.err.println("Error occurred during SFTP. " + e.getMessage());
    e.printStackTrace();
    }

    }
    }









    share|improve this question













    marked as duplicate by Martin Prikryl sftp
    Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 21 '18 at 11:54


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0


      1







      This question already has an answer here:




      • Can we use JSch for SSH key-based communication?

        1 answer




      I have two servers A and B.
      I want to SFTP a file from server A to B.



      Public key of server A (~/.ssh/id_rsa.pub) has been added to the ~/.ssh/authorized_keys of server B.



      From command line, I can SFTP from server A to B without entering password.
      However, from a Java client using library Jsch I am unable to make SFTP connection to server B and I am getting authentication error:



      Error occurred during SFTP. Auth fail
      com.jcraft.jsch.JSchException: Auth fail
      at com.jcraft.jsch.Session.connect(Session.java:519)
      at com.jcraft.jsch.Session.connect(Session.java:183)
      at Main.main(Main.java:15)


      Is there a way I can connect to server B for SFTP purposes using Java client without specifying password?



      Below is my Java code for reference:



      import com.jcraft.jsch.*;

      public class Main {

      public static void main(String args) {

      JSch jsch = new JSch();
      Session session = null;

      try {
      session = jsch.getSession("processor", "remoteserver.myorg.com", 22);
      session.setConfig("StrictHostKeyChecking", "no");
      System.out.println("Trying to connect...");
      session.connect();
      System.out.println("Connected successfully.");

      Channel channel = session.openChannel("sftp");
      channel.connect();
      ChannelSftp sftpChannel = (ChannelSftp) channel;
      System.out.println("Doing SFTP...");
      sftpChannel.put("/tmp/test.txt", "/some/remote/folder");
      System.out.println("Success");
      sftpChannel.exit();
      session.disconnect();

      } catch (JSchException | SftpException e) {
      System.err.println("Error occurred during SFTP. " + e.getMessage());
      e.printStackTrace();
      }

      }
      }









      share|improve this question















      This question already has an answer here:




      • Can we use JSch for SSH key-based communication?

        1 answer




      I have two servers A and B.
      I want to SFTP a file from server A to B.



      Public key of server A (~/.ssh/id_rsa.pub) has been added to the ~/.ssh/authorized_keys of server B.



      From command line, I can SFTP from server A to B without entering password.
      However, from a Java client using library Jsch I am unable to make SFTP connection to server B and I am getting authentication error:



      Error occurred during SFTP. Auth fail
      com.jcraft.jsch.JSchException: Auth fail
      at com.jcraft.jsch.Session.connect(Session.java:519)
      at com.jcraft.jsch.Session.connect(Session.java:183)
      at Main.main(Main.java:15)


      Is there a way I can connect to server B for SFTP purposes using Java client without specifying password?



      Below is my Java code for reference:



      import com.jcraft.jsch.*;

      public class Main {

      public static void main(String args) {

      JSch jsch = new JSch();
      Session session = null;

      try {
      session = jsch.getSession("processor", "remoteserver.myorg.com", 22);
      session.setConfig("StrictHostKeyChecking", "no");
      System.out.println("Trying to connect...");
      session.connect();
      System.out.println("Connected successfully.");

      Channel channel = session.openChannel("sftp");
      channel.connect();
      ChannelSftp sftpChannel = (ChannelSftp) channel;
      System.out.println("Doing SFTP...");
      sftpChannel.put("/tmp/test.txt", "/some/remote/folder");
      System.out.println("Success");
      sftpChannel.exit();
      session.disconnect();

      } catch (JSchException | SftpException e) {
      System.err.println("Error occurred during SFTP. " + e.getMessage());
      e.printStackTrace();
      }

      }
      }




      This question already has an answer here:




      • Can we use JSch for SSH key-based communication?

        1 answer








      java sftp jsch ssh-keys






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 11:02









      Akshay LokurAkshay Lokur

      1,47562537




      1,47562537




      marked as duplicate by Martin Prikryl sftp
      Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 '18 at 11:54


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Martin Prikryl sftp
      Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 '18 at 11:54


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Use addIdentity() api in jsync and point to your private key file location.



          Ref:
          Can we use JSch for SSH key-based communication?



             String privateKey = "~/.ssh/id_rsa";    
          jsch.addIdentity(privateKey);
          System.out.println("identity added ");
          Session session = jsch.getSession(user, host, port);
          System.out.println("session created.");





          share|improve this answer


























          • Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

            – Martin Prikryl
            Nov 21 '18 at 11:55


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Use addIdentity() api in jsync and point to your private key file location.



          Ref:
          Can we use JSch for SSH key-based communication?



             String privateKey = "~/.ssh/id_rsa";    
          jsch.addIdentity(privateKey);
          System.out.println("identity added ");
          Session session = jsch.getSession(user, host, port);
          System.out.println("session created.");





          share|improve this answer


























          • Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

            – Martin Prikryl
            Nov 21 '18 at 11:55
















          0














          Use addIdentity() api in jsync and point to your private key file location.



          Ref:
          Can we use JSch for SSH key-based communication?



             String privateKey = "~/.ssh/id_rsa";    
          jsch.addIdentity(privateKey);
          System.out.println("identity added ");
          Session session = jsch.getSession(user, host, port);
          System.out.println("session created.");





          share|improve this answer


























          • Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

            – Martin Prikryl
            Nov 21 '18 at 11:55














          0












          0








          0







          Use addIdentity() api in jsync and point to your private key file location.



          Ref:
          Can we use JSch for SSH key-based communication?



             String privateKey = "~/.ssh/id_rsa";    
          jsch.addIdentity(privateKey);
          System.out.println("identity added ");
          Session session = jsch.getSession(user, host, port);
          System.out.println("session created.");





          share|improve this answer















          Use addIdentity() api in jsync and point to your private key file location.



          Ref:
          Can we use JSch for SSH key-based communication?



             String privateKey = "~/.ssh/id_rsa";    
          jsch.addIdentity(privateKey);
          System.out.println("identity added ");
          Session session = jsch.getSession(user, host, port);
          System.out.println("session created.");






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 11:35









          Akshay Lokur

          1,47562537




          1,47562537










          answered Nov 21 '18 at 11:08









          SenthilSenthil

          7921515




          7921515













          • Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

            – Martin Prikryl
            Nov 21 '18 at 11:55



















          • Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

            – Martin Prikryl
            Nov 21 '18 at 11:55

















          Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

          – Martin Prikryl
          Nov 21 '18 at 11:55





          Please do not answer duplicate questions. Vote them to be closed as duplicate instead.

          – Martin Prikryl
          Nov 21 '18 at 11:55



          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]