How to SSH into WSL from Windows on the same machine

How to SSH into WSL from Windows on the same machine

我按照这个答案中的说明进行操作:

如何通过 SSH 进入“Windows 10 上的 Ubuntu 上的 Bash”?

我尝试在同一台机器上的 Windows 上从 PuTTY 进行 SSH。使用与我的配置文件匹配的端口 22。尝试了 127.0.0.1 和 127.0.1.1。但是,我仍然收到“访问被拒绝”的消息:

在此处输入图片描述

最终,我希望设置 Visual Studio 远程构建,但到目前为止我根本无法远程进入。我接下来应该采取什么步骤?

Process Hacker 显示端口 22 如下(不确定这是什么意思):

在此处输入图片描述

以下是 /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

答案1

在文件中将22端口改为其他端口,比如2222,/etc/ssh/sshd_config然后通过命令重新启动ssh服务sudo service ssh --full-restart,就可以成功登录了,但我不知道原因。

我也尝试通过 VisualGDB 将其用作 Visual Studio 的远程 gdb 服务器,但效果不佳。VisualGDB 将在下一个版本中支持它,如官方网站所示。链接是https://sysprogs.com/w/forums/topic/visualgdb-with-windows-10-anniversary-update-linux-support/#post-9274

答案2

端口 22 不起作用,因为 Windows 自带了内置 SSH 服务器。

Windows 10 附带一个 SSH 服务器,要么需要禁用它,要么需要更改 WSL 上用于 SSH 的端口。我选择后者,并使用端口 2200 作为 WSL SSH。

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

答案3

登录时,请使用 Windows Microsoft 帐户密码,而不是 WSL Linux 用户密码。您将登录到运行 cmd.exe 的 Windows 主目录。输入命令/windows/system32/bash.exe ~ --login以登录到 WSL 主目录并执行您的.profile

答案4

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.

相关内容