'"com.jcraft.jsch.JSchException: Auth fail" with working passwords
While trying to upload the file to our server, i am getting the following exception
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:464)
at com.jcraft.jsch.Session.connect(Session.java:158)
at FtpService.transferFileToReciever(FtpService.java:80)
at FtpService.transferFileToReciever(FtpService.java:54)
at FtpService.transferFileToRecievers(FtpService.java:44)
at FtpService.transferSingeFile(FtpService.java:241)
at FtpService.main(FtpService.java:26)
Auth fail
The part of function transferFileToReciever from source file is
JSch jsch = new JSch();
jsch.addIdentity("/root/.ssh/id_dsa");
Session session = jsch.getSession(username, host, 22);
session.setUserInfo(serverinfo);
session.connect(); //geting exception here
boolean ptimestamp = true;
The passwords are working, since i can do login using ssh, but using JSCh it doesnt work even provided with key, username and password. Using id_dsa key with java version "1.6.0_25". What could be the error?
Found other similar question, but not the answer. Thanks in advance.
Solution 1:[1]
Tracing the root cause, i finally found that the public key of type dsa is not added to the authorized keys on remote server. Appending the same worked for me.
The ssh was working with rsa key, causing me to look back in my code.
thanks everyone.
Solution 2:[2]
Try to add auth method explicitly as below, because sometimes it is required:
session.setConfig("PreferredAuthentications", "password");
Solution 3:[3]
I have also face the Auth Fail issue, the problem with my code is that I have
channelSftp.cd("");
It changed it to
channelSftp.cd(".");
Then it works.
Solution 4:[4]
Example case, when I get file from remote server and save it in local machine
package connector;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class Main {
public static void main(String[] args) throws JSchException, SftpException, IOException {
// TODO Auto-generated method stub
String username = "XXXXXX";
String host = "XXXXXX";
String passwd = "XXXXXX";
JSch conn = new JSch();
Session session = null;
session = conn.getSession(username, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channel = null;
channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
channel.cd("/tmp/qtmp");
InputStream in = channel.get("testScp");
String lf = "OBJECT_FILE";
FileOutputStream tergetFile = new FileOutputStream(lf);
int c;
while ( (c= in.read()) != -1 ) {
tergetFile.write(c);
}
in.close();
tergetFile.close();
channel.disconnect();
session.disconnect();
}
}
Solution 5:[5]
If username/password contains any special characters then inside the camel configuration use RAW for Configuring the values like
RAW(se+re?t&23)wherese+re?t&23is actual passwordRAW({abc.ftp.password})where{abc.ftp.password}values comes from a spring property file.
By using RAW, solved my issue.
Solution 6:[6]
Found other similar question, but not the answer.
It would have been interesting to know, where you have found this question.
As far as I can remember and according com.jcraft.jsch.JSchException: Auth cancel
try to add to method .addIdentity() a passphrase. You can use "" in case you generated a keyfile without one.
Another source of error is the fingerprint string. If it doesn't match you will get an authentication failure either (depends from on the target server).
And at last here my working source code - after I could solve the ugly administration tasks:
public void connect(String host, int port,
String user, String pwd,
String privateKey, String fingerPrint,
String passPhrase
) throws JSchException{
JSch jsch = new JSch();
String absoluteFilePathPrivatekey = "./";
File tmpFileObject = new File(privateKey);
if (tmpFileObject.exists() && tmpFileObject.isFile())
{
absoluteFilePathPrivatekey = tmpFileObject.getAbsolutePath();
}
jsch.addIdentity(absoluteFilePathPrivatekey, passPhrase);
session = jsch.getSession(user, host, port);
//Password and fingerprint will be given via UserInfo interface.
UserInfo ui = new UserInfoImpl(pwd, fingerPrint);
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
}
Solution 7:[7]
in my case I was using below dependency
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
and getting the same exception of Auth fail, but updated dependency to below version and problem get resolved.
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
Solution 8:[8]
I had this problem while using the maven plugin git-commit-id-plugin. I was able to push to the remote of my local git project, but
the plugin was giving this error when running mvn compile. It seems the plugin needs the remote of the current git project specified with the https protocol. Instead, what I had was:
$ git remote -v
origin [email protected]:johnbet/myprojectname.git (fetch)
origin [email protected]:johnbet/myprojectname.git (push)
I solved the issue setting the remote url with
$ git remote set-url origin https://github.com/johnbet/myprojectname.git
such that the previous command returns:
$ git remote -v
origin https://github.com/johnbet/myprojectname.git (fetch)
origin https://github.com/johnbet/myprojectname.git (push)
I discovered this by noticing that the remote my project had originally (I cloned it from another GitHub repo) was using the https protocol, while the one I set after was not.
See the official documentation about changing a remote repository's url here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AjayLohani |
| Solution 2 | Ivan Aracki |
| Solution 3 | ivan.sim |
| Solution 4 | Mikro Koder |
| Solution 5 | pirho |
| Solution 6 | Community |
| Solution 7 | Pankaj |
| Solution 8 |
