Skip to content
Advertisement

SSH password authentication in Java using jcraft JSch fails with “Auth fail” but command-line “ssh” works

My system’s OS is Ubuntu 18. I am trying to connect SFTP through Java jcraft JSch but facing this exception:

com.jcraft.jsch.JSchException: Auth fail

Below are working:

  1. From terminal using sftp works fine with username and password, no issues.
  2. From FileZilla also it is working fine.

Maven Dependency:

JavaScript
JavaScript

Output:

JavaScript

Terminal command sftp -vvv output:

JavaScript

sftp -o "PreferredAuthentications password" -vvv username@host Output:

JavaScript

Advertisement

Answer

Your ssh uses keyboard interactive authentication. The password authentication does not seem to work on your server. So you need to do the same with JSch.

See the official UserAuthKI example.

Basically you need to implement the UIKeyboardInteractive interface (together with the UserInfo interface) and associate the implementation with the session using the Session.setUserInfo.

If the authentication is prompting for a single “password” only, implement the UIKeyboardInteractive.promptKeyboardInteractive method to return a single element array with the password.


Obligatory warning: Do not use StrictHostKeyChecking=no to blindly accept all host keys. That is a security flaw. You lose a protection against MITM attacks. For the correct (and secure) approach, see: How to resolve Java UnknownHostKey, while using JSch SFTP library?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement