使用私钥取消 JSch Auth

Posted

技术标签:

【中文标题】使用私钥取消 JSch Auth【英文标题】:JSch Auth Cancel with private key 【发布时间】:2018-10-12 18:29:07 【问题描述】:

当尝试从 JSch 执行无密码 SSH 时,我遇到了以下错误。 使用私钥和密码建立连接但得到 Auth Cancel 异常。

尝试不使用密码,仍然得到相同的异常

 com.jcraft.jsch.JSchException: Auth cancel
 at com.jcraft.jsch.Session.connect(Session.java:518)
 at com.jcraft.jsch.Session.connect(Session.java:183)

连接的代码是:

JSch jschConnector = new JSch();
jschConnector.addIdentity("path_to_private_key","12345");
jschConnector.setKnownHosts("path_TO_KNOWN_HOST");              

Session session = jschConnector.getSession(user, host, port);
session.setTimeout(timeout);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
log.info(" Session created successfully");

JSch 日志:

INFO: Connecting to dedwfprsapp01.de.neustar.com port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_5.3
INFO: Local version string: SSH-2.0-JSCH-0.1.54
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: ecdh-sha2-nistp256 is not available.
INFO: ecdh-sha2-nistp384 is not available.
INFO: ecdh-sha2-nistp521 is not available.
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: ecdsa-sha2-nistp256 is not available.
INFO: ecdsa-sha2-nistp384 is not available.
INFO: ecdsa-sha2-nistp521 is not available.
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ssh-dss
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server:
INFO: kex: server:
INFO: kex: client: diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client:
INFO: kex: client:
INFO: kex: server->client aes128-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
INFO: Host 'dedwfprsapp01.de.neustar.com' is known and matches the RSA host key
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Authentications that can continue: keyboard-interactive,password
INFO: Next authentication method: keyboard-interactive
INFO: Disconnecting from dedwfprsapp01.de.neustar.com port 22
JSCH Exceptioncom.jcraft.jsch.JSchException: Auth cancel

SSH xyz.com -v 的日志:

Warning: Identity file .ssh/jenkins not accessible: No such file or directory.
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to dedwdprsrmc001 [10.57.19.176] port 22.
debug1: Connection established.
debug1: identity file /home/dnasssd/.ssh/identity type -1
debug1: identity file /home/dnasssd/.ssh/identity-cert type -1
debug1: identity file /home/dnasssd/.ssh/id_rsa type 1
debug1: identity file /home/dnasssd/.ssh/id_rsa-cert type -1
debug1: identity file /home/dnasssd/.ssh/id_dsa type -1
debug1: identity file /home/dnasssd/.ssh/id_dsa-cert type -1
debug1: identity file /home/dnasssd/.ssh/id_ecdsa type -1
debug1: identity file /home/dnasssd/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<2048<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'dedwdprsrmc001' is known and matches the RSA host key.
debug1: Found key in /home/dnasssd/.ssh/known_hosts:7
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
Server krbtgt/NEUSTAR@KRBPROD.NEUSTAR.COM not found in Kerberos database

debug1: Trying to start again
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive
debug1: Unspecified GSS failure.  Minor code may provide more information


debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /home/dnasssd/.ssh/identity
debug1: Offering public key: /home/dnasssd/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key '/home/dnasssd/.ssh/id_rsa':
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Last login: Wed May  2 14:56:44 2018 from dedwfprsapp01.de.neustar.com

【问题讨论】:

【参考方案1】:

INFO:可以继续的身份验证:keyboard-interactive,password

您的服务器根本不支持公钥身份验证(或不允许您的帐户使用)。


根本原因是您连接到错误的主机。在 JSch 中,您连接到 dedwfprsapp01[.de.neustar.com]。使用ssh,您将连接到dedwdprsrmc001

【讨论】:

以上是关于使用私钥取消 JSch Auth的主要内容,如果未能解决你的问题,请参考以下文章

使用 JSch 接受的 ssh-keygen 创建 SSH 私钥 [重复]

JSch 从字符串中添加私钥

JSch 从字符串中添加私钥

JSch为Java生成的密钥对提供无效的私钥错误

JSch 多个隧道/跳转主机

JSch 多个隧道/跳转主机