Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSHJ Example of Public Key Auth from File

Can someone give me an example of using SSHJ for Public Key Authentication? I realise this question is essentially identical to ssh example of private/public key authentication, however the answer by the author https://stackoverflow.com/users/126346/shikhar refers to a google user group that no longer exists, and I am having trouble getting it to work.

Thanks! Phil

like image 788
Obiphil Avatar asked Sep 28 '11 08:09

Obiphil


People also ask

How do I SSH from a key file?

To generate an SSH key pair, run the command ssh-keygen. It will look like this when you run it: laptop1:~ yourname$ ssh-keygen Generating public/private rsa key pair. You'll be prompted to choose the location to store the keys.

How SSH public key looks like?

An SSH2 public key in OpenSSH format will start with "ssh-rsa". The idea behind all of this is that once you have keys on the remote server and your local host, access will be simpler since the server will only grant access to someone who has the matching private key.

How do I find my SSH public key?

To generate an SSH private/public key pair for your use, you can use the ssh-keygen command-line utility. You can run the ssh-keygen command from the command line to generate an SSH private/public key pair. If you are using Windows, by default you may not have access to the ssh-keygen command.


2 Answers

We built the overthere framework on top of SSHJ. Which can connect also connect using key files. The following piece of code should work, but make sure you add the bouncycastle library to your classpath.

SSHClient client = new SSHClient();
String username = "johndoe";
File privateKey = new File("~/.ssh/id_rsa");
KeyProvider keys = client.loadKeys(privateKey.getPath());
client.authPublickey(username, keys);

Hope that helps.

like image 104
Hiery Nomus Avatar answered Oct 07 '22 07:10

Hiery Nomus


I just had this issue as well. I ended up changing

client.authPublickey(user, "id_rsa.pub")

to

client.authPublickey(user, client.loadKeys("id_rsa"))
like image 20
sirolf2009 Avatar answered Oct 07 '22 06:10

sirolf2009