WSL: Cannot connect to WSL due to ssh connection refused

I cannot connect to WSL by ssh on Windows due to connection refused.

>ssh zjw@localhost -p2222
ssh: connect to host localhost port 2222: Connection refused

But Actually, the ssh-server is running well on my WSL. And ssh on WSL can connect to WSL itself normally.

$ sudo service ssh status
 * sshd is running

$ ssh zjw@localhost -p2222
...

WSL’s sshd_config is

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server
# configured by CLion
ListenAddress 127.0.0.1
Port 2222
UsePrivilegeSeparation no
PasswordAuthentication yes

(Actually, this configuration is got from this script)

What’s wrong about this? Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 7
  • Comments: 16

Most upvoted comments

I solved this problem by set port number to 22222. I guess this may be a firewall issue? e.g: I tried to launch a nodejs app on port 2000 and access it in windows via localhost, I got “connection refused” Is anyone knows the root cause and how to solve it? @therealkenc (Right before the issue closed, a link is posted but it’s not a solution, so could this issue be reopened?)

No advice from this discussion helps: sudo /usr/sbin/sshd -Dddde works properly and I can connect to my openssh server from WSL, but not from windows CMD. Got connection refused every time

  1. I have tried to change port to 22222, 2223, 8002, but situation did not change
  2. I tried to alter 127.0.0.1 with 0.0.0.0 - same
  3. Tried to shutdown windows firewall - same
  4. Tried to restart openssh and reboot wsl - same
  5. Finally, I tried to dpkg-reconfigure openssh-server but got error invoke-rc.d: could not determine current runlevel

UPDATE!

I was working on WSL 2! So, I changed listen ip on openssh to 0.0.0.0, find out what is actual WSL ip (hostname -I) and sshed to it from windows. It works!!

I started having this problem after the latest windows update. These instructions worked for me. They’re quite old, but seem to still be valid.

This worked for me:

To enable, start your Ubuntu (or other Systemd) distribution under WSL (typically just wsl ~ will work).

sudo -e /etc/wsl.conf Add the following:

[boot]
systemd=true

Exit Ubuntu and again:

wsl --shutdown Then restart Ubuntu.

sudo systemctl status … should show your Systemd services.

Source: AskUbuntu

I started having this problem after the latest windows update. These instructions worked for me. They’re quite old, but seem to still be valid.

sudo dpkg-reconfigure openssh-server fixes the problem I am facing. Thanks a lot!