Public Key Authentication

In Working with the Client API we briefly covered how to authenticate the client. To authenticate we place a set of Authenticator implementations on the SshContext. The client iterates over these attempting each authentication in turn until the server notifies it that the connection has been authenticated.

The first task is to tell the client the remote username for this connection.


The following Authenticator implementations are available for Public Key authentication

Private Key File

This Authenticator takes a and a passphrase.

            new PrivateKeyFileAuthenticator(new File(".ssh/id_rsa"), "xxxxx"));


Key Pairs

This Authenticator takes a variable number of pre-loaded SshKeyPair objects. This requires that you load the key pair in a separate operation and can be used where the key pair is not necessarily stored in a file.

    sshContext.addAuthenticator(new PublicKeyAuthenticator(
	      new FileInputStream(".ssh/id_rsa")).toKeyPair("xxxxx")));

 You may add as many key pairs as you need. The Authenticator will verify which key is suitable for authentication before attempting the actual authentication.

Have more questions? Submit a request


Please sign in to leave a comment.