Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

JSch cannot log in with a key that the ssh command accepts

Errorcom.jcraft.jsch.JSchException: Auth fail

Short answer

The same key can work in OpenSSH and fail in JSch because the two negotiate different signature algorithms. Servers on OpenSSH 8.8 or newer refuse RSA/SHA-1, and Ed25519 needs Java 15 or Bouncy Castle in the JSch fork. Move to the maintained fork and pick the matching fix.

  • Applies to JSch fork com.github.mwiede:jsch 0.2.x, Java 17, OpenSSH 9.x server
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the OpenSSH client verbosely with the same key and look at which key type and signature algorithm the server accepts. This tells you which cause below applies.

    ssh -vvv -i ~/.ssh/id_rsa deploy@203.0.113.10 true
  2. 2Match what you see, then jump to its fix

    1. 1Auth fail with an ssh-rsa key against a server on OpenSSH 8.8 or laterThe server turned off RSA/SHA-1 and the JSch version can only sign with itFix
    2. 2Auth fail or algorithm errors with an ssh-ed25519 keyThe key is Ed25519 and the runtime cannot use itFix
    3. 3The server is old, and the OpenSSH client needs +ssh-rsa to connect to it as wellA legacy server only accepts RSA/SHA-1 (stopgap)Fix

What does the error look like?

  • com.jcraft.jsch.JSchException: Auth fail
  • Algorithm negotiation fail

Cause 1 of 3

The server turned off RSA/SHA-1 and the JSch version can only sign with it

1Check

OpenSSH 8.8 disabled RSA signatures using SHA-1 by default. OpenSSH itself switches to the stronger RSA/SHA-256/512 signatures for existing ssh-rsa keys. A JSch version that only offers SHA-1 signatures is therefore refused.

mvn dependency:tree | grep -i jsch

2Fix

Replace the old com.jcraft jsch artifact with the maintained fork. Its docs say ssh-rsa keys keep working by default using the RSA/SHA-256 and RSA/SHA-512 signature algorithms.

<dependency>
  <groupId>com.github.mwiede</groupId>
  <artifactId>jsch</artifactId>
  <version>0.2.x</version> <!-- use the current release -->
</dependency>

3Verify

Run the connect code again; the session connects and the command runs.

# session.connect() returns without JSchException

Confirmed by openssh.org

Did this fix it?

Cause 2 of 3

The key is Ed25519 and the runtime cannot use it

1Check

The fork's README says ssh-ed25519 and ssh-ed448 need at least Java 15 or Bouncy Castle on the classpath. Check the Java version.

java -version

2Fix

Run on Java 15 or newer, or add Bouncy Castle (bcprov-jdk18on) to the classpath.

<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk18on</artifactId>
  <version>1.x</version> <!-- use the current release -->
</dependency>

3Verify

Connect again with the Ed25519 key.

# session.connect() succeeds

Confirmed by github.com

Did this fix it?

Cause 3 of 3

A legacy server only accepts RSA/SHA-1 (stopgap)

1Check

OpenSSH's notes say RSA/SHA-1 can be re-enabled selectively through HostkeyAlgorithms and PubkeyAcceptedAlgorithms, as a stopgap until the server is upgraded.

ssh -o PubkeyAcceptedAlgorithms=+ssh-rsa -o HostkeyAlgorithms=+ssh-rsa deploy@legacy.example.com true

2Fix

Enable ssh-rsa for that one JSch session only, then plan the server upgrade.

session.setConfig("server_host_key", session.getConfig("server_host_key") + ",ssh-rsa");
session.setConfig("PubkeyAcceptedAlgorithms", session.getConfig("PubkeyAcceptedAlgorithms") + ",ssh-rsa");

3Verify

The session connects to the legacy host.

# session.connect() succeeds

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Use the maintained JSch fork and keep it updated.
  • Prefer Ed25519 keys on a current Java, or RSA keys with SHA-2 signatures.
  • Re-enable ssh-rsa per session only for a legacy host, never for the whole application.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Back end

Gradle runs no Cucumber scenarios from my feature files

Feature files exist under src/test but nothing runs

Cucumber runs on the JUnit Platform, so Gradle must call useJUnitPlatform() and have the cucumber-junit-platform-engine on the test classpath.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.