Make an SSH connection with a remote server using Java

We can build an ssh connection to any remote server by following some simple steps mentioned below.

Adding dependencies

We will use the JSch library to connect with the remote server. So, we will add its dependency here –

		<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
		<dependency>
			<groupId>com.jcraft</groupId>
			<artifactId>jsch</artifactId>
			<version>0.1.55</version>
		</dependency>

Now, we can connect with the remote server with Jsch using two methods –

Let’s see each of them one by one.

Making an SSH connection with Jsch using a password

So, let us see what we need to connect to a remote server. Well, there are only a few things-

  • Remote server IP
  • Remote server port ( The port on which you want to connect on, say, 33000 )
  • Username
  • Password

Below is the sample code to connect with your server using the password –

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

public class Codekru {

	public static void main(String[] args) {
		
		try {
			JSch jsch = new JSch();
			String username = "codekru"; // your username
			String host = "127.0.0.1"; // your remote server address
			int port = 33000; // your remote server port
			String password = "root"; // your username's password

			Session session = jsch.getSession(username, host, port);
			session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.setTimeout(15000);
			session.connect();
		} catch (JSchException e) {
			e.printStackTrace();
		}
	}

}

This is it if you want to make an ssh connection using the password. Now, let’s move on to making a connection using a key.

Making an SSH connection using key

So, We will have to store our key someplace in our machine. We kept it at “/Users/codekru/key”. Now, let’s use this key and make an ssh connection.

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

public class Codekru {
	public static void main(String[] args) {
		try {

			JSch jsch = new JSch();
	        String user = "codekru"; // your username
	        String host = "127.0.0.1"; // your remote server address
	        int port = 33000; // your remote server port
	        String yourKeyName = "/Users/codekru/key";
	         
	        jsch.addIdentity(yourKeyName);
	        Session session = jsch.getSession(user, host, port);
	        session.setConfig("StrictHostKeyChecking", "no");
	        session.setTimeout(15000);
	        session.connect();

		} catch (JSchException e) {
			e.printStackTrace();
		}

	}
}

Using this, you can establish an ssh connection with Jsch using a key.

What if we used an invalid key? What exception we will get?

We will get an invalid privatekey JSchException.

com.jcraft.jsch.JSchException: invalid privatekey: [[email protected]
	at com.jcraft.jsch.KeyPair.load(KeyPair.java:948)
	at com.jcraft.jsch.KeyPair.load(KeyPair.java:561)
	at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
	at com.jcraft.jsch.JSch.addIdentity(JSch.java:406)
	at com.jcraft.jsch.JSch.addIdentity(JSch.java:366)
What if we used the wrong password?

Here, we will get an Auth fail JSchException.

com.jcraft.jsch.JSchException: Auth fail
	at com.jcraft.jsch.Session.connect(Session.java:519)
	at com.jcraft.jsch.Session.connect(Session.java:183)
Related Articles –

I hope you liked this article. If you found anything wrong or have any doubts, please feel free to write us in the comments or mail us at [email protected].

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *