Ami*_*faq 3 git macos ssh jsch
我有两个 GitHub 帐户,它们都有不同的 SSH 密钥。
如果我使用终端检查连接,我可以使用两者进行连接,并且响应消息具有相应的用户名。
关键1:
ssh -i ~/.ssh/id_rsa.pub git@github.com
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.
Run Code Online (Sandbox Code Playgroud)
关键2:
ssh -i ~/.ssh/id2_rsa.pub git@github.com
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.
Run Code Online (Sandbox Code Playgroud)
这里好像还行
但是当我使用 java (JSch) 时,我可以使用密钥 1 进行连接,但不能使用密钥 2 进行连接。
以下是代码:
JSch jsch = new JSch();
String host ="git@github.com"; //
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();
}
Run Code Online (Sandbox Code Playgroud)
如果变量 privateKey 是 '~/.ssh/id_rsa' 它连接,但 '~/.ssh/id2_rsa' 抛出异常。
它也添加到 ssh-agent 中:
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)
Run Code Online (Sandbox Code Playgroud)
请建议我解决这个问题。
经过一些尝试,我发现我的第二个密钥有密码。我在制作 kay 时提供了一个,当时解决方案很简单。
只需要一个小的改变,它就奏效了。
jsch.addIdentity(privateKey,"passphrase");
Run Code Online (Sandbox Code Playgroud)