apache / apache/mina-sshd

Unable to connect to sftp server running on WSL using hostname localhost

Open
#661 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.1k
Forks
400
Avg merge
5d 1h
Merged PRs (30d)
1

Description

### Version

2.14.0

### Bug description

The apache mina sshClient is unable to find the sftp server running on WSL on the same machine using the hostname "localhost" and "127.0.0.1".

I was able to connect to the server using :
OpenSSH_for_Windows_9.5p1 a.ka the sftp command in cmd/powershell
winscp version 6.3.6
Paramiko library for python all of the them were using the following parameters:

host = "localhost" port = "22" with correct wsl username and password

**how to reproduce**

1. Open powershell and run the command :

`wsl --install Ubuntu-24.04`

Create an account for the linux distro

2. Run the following command in order to install and start your SFTP server

`sudo apt update`
`sudo apt install openssh-server`
`sudo service ssh restart`

Now validate that the service is running

` sudo service ssh status`

Now you should have your sftp server running

Now create a maven project with the following dependency in Pom

```xml

org.apache.sshd
sshd-sftp
2.14.0

```

```xml

org.apache.sshd
sshd-core
2.14.0

```

Now run this code :

```java
public static void main( String[] args ) {
String host = "localhost";
int port = 22;
String username = "your-username";
String password = "your-password";

SshClient client = SshClient.setUpDefaultClient();
client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
client.start();

try (ClientSession session = client.connect(username, host, port).verify(3000).getSession()) {
session.addPasswordIdentity(password);
session.auth().verify(3000);

SftpClient sftpClient = SftpClientFactory.instance().createSftpClient(session);

//Sftp operation here

} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
} finally {
client.stop();
}

}
```
Normally you should be able to connect to the server and run some sftp commands

PS. If I use the virtual ethernet IP of my WSL distro it worked.

### Actual behavior

```
[main] INFO org.apache.sshd.common.io.DefaultIoServiceFactoryFactory - No detected/configured IoServiceFactoryFactory; using Nio2ServiceFactoryFactory
[main] INFO org.apache.sshd.client.config.hosts.DefaultConfigFileHostEntryResolver - resolveEffectiveResolver(sftpuser@localhost:22/null) no configuration file at C:\Users\sftpuser\.ssh\config
org.apache.sshd.common.SshException: DefaultConnectFuture[sftpuser@localhost/127.0.0.1:22]: Failed (IOException) to execute: The remote computer refused the network connection
at org.apache.sshd.common.future.AbstractSshFuture.lambda$verifyResult$2(AbstractSshFuture.java:146)
at org.apache.sshd.common.future.AbstractSshFuture.formatExceptionMessage(AbstractSshFuture.java:206)
at org.apache.sshd.common.future.AbstractSshFuture.verifyResult(AbstractSshFuture.java:145)
at org.apache.sshd.client.future.DefaultConnectFuture.verify(DefaultConnectFuture.java:55)
at org.apache.sshd.client.future.DefaultConnectFuture.verify(DefaultConnectFuture.java:36)
at org.apache.sshd.common.future.VerifiableFuture.verify(VerifiableFuture.java:121)
at org.chuck.App.main(App.java:29)
Caused by: java.io.IOException: The remote computer refused the network connection
at java.base/sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:299)
at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389)
at java.base/java.lang.Thread.run(Thread.java:840)
Exception in thread "main" java.lang.RuntimeException: org.apache.sshd.common.SshException: DefaultConnectFuture[sftpuser@localhost/127.0.0.1:22]: Failed (IOException) to execute: The remote computer refused the network connection
at org.chuck.App.main(App.java:39)
Caused by: org.apache.sshd.common.SshException: DefaultConnectFuture[charles@localhost/127.0.0.1:2222]: Failed (IOException) to execute: The remote computer refused the network connection
at org.apache.sshd.common.future.AbstractSshFuture.lambda$verifyResult$2(AbstractSshFuture.java:146)
at org.apache.sshd.common.future.AbstractSshFuture.formatExceptionMessage(AbstractSshFuture.java:206)
at org.apache.sshd.common.future.AbstractSshFuture.verifyResult(AbstractSshFuture.java:145)
at org.apache.sshd.client.future.DefaultConnectFuture.verify(DefaultConnectFuture.java:55)
at org.apache.sshd.client.future.DefaultConnectFuture.verify(DefaultConnectFuture.java:36)
at org.apache.sshd.common.future.VerifiableFuture.verify(VerifiableFuture.java:121)
at org.chuck.App.main(App.java:29)
Caused by: java.io.IOException: The remote computer refused the network connection
at java.base/sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:299)
at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389)
at java.base/java.lang.Thread.run(Thread.java:840)

Process finished with exit code 1
```

Meaning that I cant connect, but if

### Expected behavior

Normally it should find my sftp service

### Relevant log output

```Shell

```

### Other information

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the connection call in App.java and reproduce the reported setup using WSL Ubuntu, localhost or 127.0.0.1, and port 22; compare it with the working virtual Ethernet IP. Check the Apache MINA SSHD client connection path and its handling of the resolved address. Done means the supplied localhost setup connects successfully, or the limitation is clearly established and documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, linux, ubuntu
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.