Skip to content

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

@DukeCharles

Description

@DukeCharles

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

  1. 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

<dependency>
      <groupId>org.apache.sshd</groupId>
      <artifactId>sshd-sftp</artifactId>
      <version>2.14.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.sshd</groupId>
      <artifactId>sshd-core</artifactId>
      <version>2.14.0</version>
    </dependency>

Now run this code :

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

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions