大网数据广告活动
服务器运维问答

Jsch Exception: Auth Fail with private key

· 52CE 我爱测

I'm using below code to connect remote server and I followed below steps to connect private/public key generation & concatenate of public key with authorized key. Code: private Session createSession() throws JSchException { JSch …

问题原文

原帖链接 · Shailu · CC BY-SA 3.0

I'm using below code to connect remote server and I followed below steps to connect private/public key generation & concatenate of public key with authorized key.

Code:

private Session createSession() throws JSchException {
        JSch jsch = new JSch();
            jsch.addIdentity(privateKey);                   //add private key path as ~/.ssh/id_rsa
            Session session;
            session = jsch.getSession(user, host, port);
            java.util.Properties config = new java.util.Properties();

        Properties cfg = new Properties();
            cfg.put("trust", "true");
            cfg.put("StrictHostKeyChecking", "no");
            cfg.put("HashKnownHosts", "yes");

            session.setConfig(cfg);
            session.connect();
            return session;
    }

Steps:

1. I can able to generate private/public key in the path ~/.ssh/id_rsa(private key) and ~/.ssh/id_rsa.pub(public key)
    >> ssh-keygen (or) ssh-keygen -t rsa -b 4096
   Note: Generated key with no passphrase
2. I have added public key with authorized_keys with below command
    >> ssh user@host "echo \"`cat ~/.ssh/id_rsa.pub`\" >> .ssh/authorized_keys"

Still I'm facing the exception "com.jcraft.jsch.JSchException: Auth fail". Please guide me to proceed.

社区高分解答

原帖链接 · Murph · CC BY-SA 3.0

I believe this can happen for several reasons:

  • The private key is password protected, which you have already verified is not the case.

  • The directory containing the keys is encrypted.

  • The file permissions on the .ssh directory and or public/private keys are too open. They should be:

    • ssh directory: 700
    • public key: 644
    • private key: 600 (possibly even 400)
  • What also might be going on is the format of the public key within the authorized keys file is incompatible. You might need to convert it to a different format. Assuming OpenSSH:

    • Convert to SSH2: ssh-keygen -i -f /path/to/private/key ssh-keygen -i -f /path/to/public/key
    • Convert from SSH2: ssh-keygen -e -f /path/to/private/key ssh-keygen -e -f /path/to/public/key

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。