无法访问在 WSL 中运行的 SSH 服务器

无法访问在 WSL 中运行的 SSH 服务器

基本上我已经在 Kali WSL Linux 中启用了 SSH 服务器。

问题是,当我从 Windows 进行操作并提供密码时,它工作正常并且连接成功。ssh [email protected]

当我从互联网上执行此操作时ssh root@PublicIP,它会要求输入密码,然后失败。

我已在路由器中启用端口转发,以在端口 22 上启用 SSH 访问,并且还启用了PermitRootLogin: Yes.

从 Internet 执行 ssh 时,命令的输出如下: ssh -vvv [email protected]

debug1: Trying private key: C:\\Users\\username/.ssh/id_dsa
debug3: GetFileAttributesExW with last error 2
debug3: no such identity: C:\\Users\\username/.ssh/id_dsa: No such file or directory
debug1: Trying private key: C:\\Users\\username/.ssh/id_ecdsa
debug3: GetFileAttributesExW with last error 2
debug3: no such identity: C:\\Users\\username/.ssh/id_ecdsa: No such file or     directory
debug1: Trying private key: C:\\Users\\username/.ssh/id_ed25519
debug3: GetFileAttributesExW with last error 2
debug3: no such identity: C:\\Users\\username/.ssh/id_ed25519: No such file or     directory
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
debug3: failed to open file:/dev/tty error:3
debug1: read_passphrase: can't open /dev/tty: No such file or directory

服务器/var/log/auth.log输出是:

Aug 19 17:17:01 kali CRON[1427]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 19 17:17:01 kali CRON[1427]: pam_unix(cron:session): session closed for user root
Aug 19 17:21:13 kali systemd-logind[553]: New seat seat0.
Aug 19 17:21:13 kali systemd-logind[553]: Watching system buttons on /dev/input/event5 (Power Button)
Aug 19 17:21:13 kali systemd-logind[553]: Watching system buttons on /dev/input/event0 (AT Translated Set 2 keyboard)
Aug 19 17:21:15 kali sshd[586]: Server listening on 0.0.0.0 port 22.
Aug 19 17:21:15 kali sshd[586]: Server listening on :: port 22.
Aug 19 17:21:16 kali gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session opened for user Debian-gdm by (uid=0)
Aug 19 17:21:16 kali systemd-logind[553]: New session c1 of user Debian-gdm.
Aug 19 17:21:16 kali systemd: pam_unix(systemd-user:session): session opened for user Debian-gdm by (uid=0)
Aug 19 17:21:25 kali polkitd(authority=local): Registered Authentication Agent for unix-session:c1 (system bus name :1.16 [/usr/bin/gnome-shell], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8)
Aug 19 17:21:27 kali realmd[785]: Loaded settings from: /usr/lib/realmd/realmd-defaults.conf /usr/lib/realmd/realmd-distro.conf
Aug 19 17:21:27 kali realmd[785]: holding daemon: startup
Aug 19 17:21:27 kali realmd[785]: starting service
Aug 19 17:21:27 kali realmd[785]: connected to bus
Aug 19 17:21:27 kali realmd[785]: released daemon: startup
Aug 19 17:21:27 kali realmd[785]: claimed name on bus: org.freedesktop.realmd
Aug 19 17:21:44 kali gdm-password]: pam_unix(gdm-password:session): session opened for user root by (uid=0)
Aug 19 17:21:44 kali systemd: pam_unix(systemd-user:session): session opened for user root by (uid=0)
Aug 19 17:21:44 kali systemd-logind[553]: New session 2 of user root.
Aug 19 17:21:47 kali polkitd(authority=local): Registered Authentication Agent     for unix-session:2 (system bus name :1.55 [/usr/bin/gnome-shell], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8)
Aug 19 17:22:28 kali realmd[785]: quitting realmd service after timeout
Aug 19 17:22:28 kali realmd[785]: stopping service
Aug 19 17:23:03 kali sshd[1269]: Connection closed by 192.168.0.13 port 57122     [preauth]
Aug 19 17:24:35 kali sudo:     root : TTY=pts/0 ; PWD=/root ; USER=root ;     COMMAND=/usr/bin/passwd
Aug 19 17:24:35 kali sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Aug 19 17:24:37 kali passwd[1284]: pam_unix(passwd:chauthtok): password changed for root
Aug 19 17:24:37 kali passwd[1284]: gkr-pam: couldn't update the login keyring password: no old password was entered
Aug 19 17:24:37 kali sudo: pam_unix(sudo:session): session closed for user     root
Aug 19 17:25:01 kali CRON[1287]: pam_unix(cron:session): session opened for user     root by (uid=0)
Aug 19 17:25:01 kali CRON[1287]: pam_unix(cron:session): session closed for user root
Aug 19 17:26:11 kali sshd[1290]: Accepted password for root from 192.168.0.13 port 57190 ssh2
Aug 19 17:26:11 kali sshd[1290]: pam_unix(sshd:session): session opened for user root by (uid=0)
Aug 19 17:26:11 kali systemd-logind[553]: New session 5 of user root.
Aug 19 17:28:04 kali passwd[1307]: pam_unix(passwd:chauthtok): password changed for sshd
Aug 19 17:28:04 kali passwd[1307]: gkr-pam: couldn't update the login keyring password: no old password was entered
Aug 19 17:28:14 kali sshd[1290]: pam_unix(sshd:session): session closed for user root
Aug 19 17:28:14 kali systemd-logind[553]: Removed session 5.
Aug 19 17:35:01 kali CRON[1328]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 19 17:35:01 kali CRON[1328]: pam_unix(cron:session): session closed for user root

如何才能成功连接 SSH?

答案1

Windows 中适用于 Linux 的任何 Windows 子系统都不完全是您常规运行的常规 Linux。 (更多数据请参阅末尾的相关问题链接)

远程 SSH 需要伪终端支持才能正常工作。根据这两篇文章,它最近才在 6 月份的 Windows 14361 Insider 预览更新中添加。目前还没有向公众开放,所以目前,远程 SSH 无法在任何 WSL Linux 版本中工作

Ubuntu 似乎正在为此做准备,而 Kali 往往需要一段时间才能赶上。

此外,WSL 没有特殊权限,只是任何其他常规 Windows 用户/进程。关闭 WSL 命令行框后,WSL 中的服务/守护程序不会继续运行,所有进程都会被终止。

如果必须运行(永久)Linux 服务,最好在虚拟机中运行 Linux。

关于伪终端/pty 支持,请参阅:

Tmux 支持 Windows 上 Ubuntu 上的 Bash

适用于 PC 的 Windows 10 build 14361:您需要了解的一切

在 Windows 10 build 14361 中,我们向 Windows Subsystem for Linux (WSL) 添加了伪终端支持

查看相关问题:尝试在 Debian 版本 9.5 Linux 中运行常规隧道

也可以看看:什么是伪终端(pty/tty)?

附言。我不会忽视尝试在 WLS 上运行永久服务需要复杂的设置/技巧,这些服务依赖于与 Windows 密切相关的 WSL。它们很丑,我怀疑它们的可靠性。

目前还不清楚新的 WSL pty 实现是否允许 OpenSSH 工作。

相关内容