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
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 -Ddddeworks properly and I can connect to my openssh server from WSL, but not from windows CMD. Gotconnection refusedevery timedpkg-reconfigure openssh-serverbut got errorinvoke-rc.d: could not determine current runlevelUPDATE!
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.confAdd the following:Exit Ubuntu and again:
wsl --shutdownThen restart Ubuntu.sudo systemctl status… should show your Systemd services.Source: AskUbuntu
sudo dpkg-reconfigure openssh-serverfixes the problem I am facing. Thanks a lot!