How to SSH into WSL from Windows on the same machine

I followed the instructions in this answer:

How can I SSH into “Bash on Ubuntu on Windows 10”?

I'm attempting to SSH from PuTTY in Windows on the same machine. Using Port 22, which matches my config file. Tried both 127.0.0.1 and 127.0.1.1. However, I'm still getting "access denied":

enter image description here

Ultimately I'm hoping to set up a Visual Studio remote build, but I've been thus far unable to get in remotely at all. What next steps should I take?

Process Hacker shows this for port 22 (not sure what this means):

enter image description here

Here are the contents of /etc/ssh/sshd_config:

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation no

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin no
AllowUsers basel
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

Solution 1:

Change the 22 port to a other one,such as 2222,in the file /etc/ssh/sshd_config,then restart the ssh service by the commond sudo service ssh --full-restart,you will successfully login.But I don't know the reason.

I also try use it as a remote gdb server for visual studio by VisualGDB,it not works well. VisualGDB will support it in the next version as the offical website shows.The link is https://sysprogs.com/w/forums/topic/visualgdb-with-windows-10-anniversary-update-linux-support/#post-9274

Solution 2:

Port 22 does not work because Windows comes with a built in SSH server.

Windows 10 comes with an SSH server, and it will either need to be disabled or the port that will be used for SSH on WSL will need to be changed. I chose to do the latter and use port 2200 for WSL SSH.

Source: https://virtualizationreview.com/articles/2017/02/08/graphical-programs-on-windows-subsystem-on-linux.aspx

Solution 3:

When you login, use your Windows Microsoft Account password, not your WSL Linux user password. You will be logged into your Windows home directory running cmd.exe. Enter command /windows/system32/bash.exe ~ --login to log into your WSL home directory and execute your .profile.

Solution 4:

Contrary to the accepted and other answers here. The port does not have to be changed. I was able to successfully connect on port 22 to my OpenSSH server runnin inside Ubuntu 18.04 WSL on port 22. It seems that in addition to the user explicitly running the Windows OpenSSH server explcitly, Windows can also run the OpenSSH server in the background without the user explicitly enabling it, this happens when you enable "Developer Mode" in the new settings.

If you are trying to login to your WSL OpenSSH server, but you are ending up at a batch/cmd prompt in your Windows home folder, then Windows is running an OpenSSH server on port 22 in the background and is not letting WSL use it for outbound traffic.

If you want to connect on port 22 to your OpenSSH sever in WSL then try the following steps:

  1. Start not logged into SSH.

  2. If you don't have one already, open Windows Firewall, select Add Rule and configure it to allow incoming traffic on port 22.

  3. Open the new Windows 10 "Settings" app, navigate to "Updates & Security" and to the section "For developers"

  4. Turn off the option for "Device Discovery -- Make your device visible to USB connections and your local network."

After doing this, I was able to connect to the openssh-server inside of my WSL instance on port 22.

Reference Article:

https://www.ctrl.blog/entry/how-to-win10-ssh-service.html

Solution 5:

What I did was sudo ssh-keygen instead of ssh-keygen.

I ran into some earlier problems with files not writing correctly and this fixed it for me.