Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'USERAUTH fail' while conneting to github using JSch

Tags:

git

macos

ssh

jsch

I have two GitHub accounts, and both have different SSH keys for them.

if i use terminal to check the connection i can connect using both and the response message has corresponding user name.

Key1:

ssh -i ~/.ssh/id_rsa.pub [email protected]
PTY allocation request failed on channel 0
Hi m-aamir-ashfaq! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

Key2:

ssh -i ~/.ssh/id2_rsa.pub [email protected]
PTY allocation request failed on channel 0
Hi mutong2! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

It seems OK here,

But when i use java (JSch) i can connect using key 1 but not with key 2.

Following is the code:

    JSch jsch = new JSch();
    String host ="[email protected]";  // 
    String privateKey = "~/.ssh/id2_rsa" ;

    try {

        jsch.addIdentity(privateKey);
        String user=host.substring(0, host.indexOf('@'));
        host=host.substring(host.indexOf('@')+1);
        Session session=jsch.getSession(user, host, 22);
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
    } catch (JSchException e) {
        e.printStackTrace();
    }

if variable privateKey is '~/.ssh/id_rsa' it connects, but '~/.ssh/id2_rsa' throws exception.

its added to ssh-agent as well:

ssh-add -l
2048 64:b2:68:e0:95:51:e2:db:15:ba:e8:f1:3d:31:bc:ca /Users/amirashfaq/.ssh/id_rsa (RSA)
2048 58:26:cd:cf:dd:fe:e1:0c:68:5b:22:23:86:a4:da:9a /Users/amirashfaq/.ssh/github_rsa (RSA)
4096 bd:bd:e8:f6:78:80:3c:6d:0a:96:47:2f:f8:ae:ca:0f /Users/amirashfaq/.ssh/id2_rsa (RSA)

Please suggest me solution for this issue.

like image 930
Amir Ashfaq Avatar asked Mar 15 '23 09:03

Amir Ashfaq


1 Answers

OK after some tries, i figure out that my second key has passphrase. i provided one while making the kay and solution was simple then.

Just need to a small change and it worked.

jsch.addIdentity(privateKey,"passphrase");
like image 193
Amir Ashfaq Avatar answered Mar 17 '23 07:03

Amir Ashfaq