Tag Archives: com.jcraft.jsch.JSchException: Auth fail

com.jcraft.jsch.JSchException: Auth fail [How to Solve]

Background

Server information: server a: 10.102.110.1 server B: 10.102.110.2 need to transfer files from server a to server B through SFTP

There is a function in the application project to transfer log files through SFTP. During the deployment, authentication free (secret free) login has been configured between servers, that is, SFTP password free login. However, after the deployment of the project, when the service is started, the following error is still reported when the transfer is needed: com.jcraft.jsch.jschexception: auth fail

14:26:12.704 [pool-1-thread-1] ERROR fileTransfer - connect to server failed:
com.jcraft.jsch.JSchException: Auth fail
	at com.jcraft.jsch.Session.connect(Session.java:519) ~[jsch-0.1.54.jar:na]
	at com.jcraft.jsch.Session.connect(Session.java:183) ~[jsch-0.1.54.jar:na]
	at com.shop.core.log.transfer.SftpFileTransfer.connect(SftpFileTransfer.java:64) ~[livechat-core-0.0.1-SNAPSHOT.jar:na]
	at com.shop.core.log.transfer.FileTransferExecutor.processFile(FileTransferExecutor.java:96) [livechat-core-0.0.1-SNAPSHOT.jar:na]
	at com.shop.core.log.transfer.FileTransferExecutor.access$000(FileTransferExecutor.java:26) [livechat-core-0.0.1-SNAPSHOT.jar:na]
	at com.shop.core.log.transfer.FileTransferExecutor$1.run(FileTransferExecutor.java:48) [livechat-core-0.0.1-SNAPSHOT.jar:na]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [na:1.7.0_67]
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304) [na:1.7.0_67]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) [na:1.7.0_67]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.7.0_67]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_67]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_67]
	at java.lang.Thread.run(Thread.java:745) [na:1.7.0_67]
14:26:12.705 [pool-1-thread-1] ERROR fileTransfer - Connect to 10.102.110.7 failed, cannot transfer data files!
14:26:22.768 [pool-1-thread-1] ERROR fileTransfer - connect to server failed:
com.jcraft.jsch.JSchException: Auth fail
	at com.jcraft.jsch.Session.connect(Session.java:519) ~[jsch-0.1.54.jar:na]
	at com.jcraft.jsch.Session.connect(Session.java:183) ~[jsch-0.1.54.jar:na]
	at com.shop.core.log.transfer.SftpFileTransfer.connect(SftpFileTransfer.java:64) ~[livechat-core-0.0.1-SNAPSHOT.jar:na]
	at com.shop.core.log.transfer.FileTransferExecutor.processFile(FileTransferExecutor.java:96) [livechat-core-0.0.1-SNAPSHOT.jar:na]
	at com.shop.core.log.transfer.FileTransferExecutor.access$000(FileTransferExecutor.java:26) [livechat-core-0.0.1-SNAPSHOT.jar:na]
	at com.shop.core.log.transfer.FileTransferExecutor$1.run(FileTransferExecutor.java:48) [livechat-core-0.0.1-SNAPSHOT.jar:na]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [na:1.7.0_67]
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304) [na:1.7.0_67]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) [na:1.7.0_67]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.7.0_67]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_67]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_67]
	at java.lang.Thread.run(Thread.java:745) [na:1.7.0_67]
14:26:22.768 [pool-1-thread-1] ERROR fileTransfer - Connect to 10.102.110.7 failed, cannot transfer data files!

Solution process

###1. Check the configuration parameters, such as user name and password, for configuration errors. I won’t explain too much here. I’ll check the configuration of SFTP connection

2. Check whether the SFTP password free login configuration between servers is OK

In case of auth fail authentication failure, the first thing to check is whether the SSH password free login configuration of the server is OK. In 10.102.110.1 server a, use the following command:

ssh [email protected]

Shop: user name

10.102.110.2: server IP

After entering the command, you can successfully connect to server B without entering the password. That means that the secret free configuration is OK

###3. Check the configuration item passwordauthentication in/etc/SSH/sshd_ Passwordauthentication, a configuration item in the config folder, is no by default“ Set whether password authentication is allowed. Change it to yes and restart the service

I checked the 10.102.110.2 server B configuration and found no problem. Passwordauthentication is configured with yes

4. See if session. Connect sets the connection timeout

Check the code and find that in the code, connect is the default construction

//Omit other codes
this.sshSession.connect();
//Omit other codes

After a look at the source code, I think the connection timeout should be set, the default is 0! So change to the following code

// Set the login timeout time, not set may report an error
this.sshSession.connect(1500);

After restarting the service, it is found that it is normal, the connection is successful, and an error is reported

##To sum up the emergence of a problem, there are many possible reasons. We should not mess up because of the problem. We should have the idea of investigating the problem. Step by step, the problem will be solved eventually