Nikhil Udayakumar
Nikhil Udayakumar

Reputation: 21

How to connect to unix via java without passing user name and password

How to connect to a remote unix host from java without passing username and password.

The below code is what I am using

   package testing;     

    import java.io.InputStream;

    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;

    public class Test {

        public static void main(String[] args) {

            execute();
        }

            public static void execute() {
                    try {
                        JSch jsch = new JSch();

                        String user = "xxxx";
                        String host = "xxxxxx";
                        int port = 22;
                        String privateKey = "xxxxxxxxx";

                        jsch.addIdentity(privateKey);
                        System.out.println("identity added ");

                        Session session = jsch.getSession("nudayaku","cloudgw-dev-01" );
                        System.out.println("session created.");
                        session.setConfig("PreferredAuthentications", "publickey");
                        java.util.Properties config = new java.util.Properties();    
                        config.put("StrictHostKeyChecking", "no");      
                        session.setConfig(config);  
                        session.connect();
                        System.out.println("session connected.....");

                    } catch (Exception e) {
                        System.err.println(e);
                    }
                }
    }

Please find the below error.

stack trace error

com.jcraft.jsch.Session.connect(Session.java:519)
     com.jcraft.jsch.Session.connect(Session.java:183)
     testing.Test.execute(Test.java:39)
     testing.Test.main(Test.java:17)

Upvotes: 1

Views: 328

Answers (1)

Stephen C
Stephen C

Reputation: 718758

Based on the evidence so far, the most likely causes are (IMO):

  1. You are using the wrong host, and the account doesn't exist on that host.
  2. You are using the wrong user name for the remote account
  3. You are using a private key that doesn't match one of the public keys recorded for the remote account
  4. The remote server is not configured to accept PublicKey auth.
  5. The remote account's ~/.ssh directory is not configured correctly. A common problem is for either the ~/.ssh directory or the ~/.ssh/authorized_keys file to be too open.

I think that we can eliminate problems with the private key file. I think that would give you a different exception.


If none of the above is the cause, then you need to turn on JSch logging at debug level to get more insights as to what is going wroing. Unfortunately, JSch doesn't use a logging framework by default, but there is an example in the JSch examples page that gives code that you can borrow:

Finally, if client-side logging doesn't help, see if you can (temporarily) turn on debugging for sshd on the remote server side. (Be aware that sshd is a security / privacy concern. Sensitive information about all SSH connections will be logged.)


This is not the problem, but the following code looks a bit strange:

session.setConfig("PreferredAuthentications", "publickey");
java.util.Properties config = new java.util.Properties();    
config.put("StrictHostKeyChecking", "no");      
session.setConfig(config);  

Why don't you write the last 3 statements as follows?

session.setConfig("StrictHostKeyChecking", "no");

Upvotes: 1

Related Questions