登录后立即关闭 WSL 申请的 ssh 连接

登录后立即关闭 WSL 申请的 ssh 连接

有一个类似的问题:登录成功后立即关闭ssh连接

但是,我的情况是,仅当我使用 ssh 通过 WSL 终端连接某个服务器时,连接才会立即关闭,如下所示:

➜  ~ ssh [email protected]
[email protected]'s password:
Last login: Fri Jan 12 10:44:41 2024 from 202.127.205.222
Connection to 202.127.204.19 closed.

即使我连接到另一台服务器并跳转到该服务器,它仍然关闭。但我可以通过 VS code 或其他 ssh 客户端正常连接,我猜问题一定是出在 WSL 上。

我的WSL信息如下:

PS C:\Users\Admin> wsl --list --verbose
  NAME      STATE           VERSION
* Ubuntu    Running         2
➜  ~ uname -a
Linux KEQ44MJ 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
➜  ~ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.2 LTS"

部分细节如下ssh -vvv

➜  ~ ssh -vvv [email protected]
...
Last login: Fri Jan 12 10:58:51 2024 from 202.127.204.192
debug3: receive packet: type 98
debug1: client_input_channel_req: channel 0 rtype exit-signal reply 0
debug3: receive packet: type 98
debug1: client_input_channel_req: channel 0 rtype [email protected] reply 0
debug2: channel 0: rcvd eow
debug2: chan_shutdown_read: channel 0: (i0 o0 sock -1 wfd 4 efd 6 [write])
debug2: channel 0: input open -> closed
debug3: receive packet: type 96
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: chan_shutdown_write: channel 0: (i3 o1 sock -1 wfd 5 efd 6 [write])
debug2: channel 0: output drain -> closed
debug3: receive packet: type 97
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug3: send packet: type 97
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
  #0 client-session (t4 r0 i3/0 o3/0 e[write]/0 fd -1/-1/6 sock -1 cc -1 io 0x00/0x00)

debug3: send packet: type 1
Connection to 202.127.204.19 closed.
Transferred: sent 2272, received 2988 bytes, in 0.3 seconds
Bytes per second: sent 8822.0, received 11602.2
debug1: Exit status -1

服务器上的 sshd 日志:似乎被客户端关闭了。

[root@pcfrt log]# tail /var/log/secure
Jan 12 10:58:51 pcfrt sshd[277098]: pam_unix(sshd:session): session closed for user asipp
Jan 12 11:03:07 pcfrt su: pam_unix(su:session): session closed for user root
Jan 12 11:05:35 pcfrt sshd[270847]: pam_unix(sshd:session): session closed for user root
Jan 12 11:13:01 pcfrt sshd[5895]: Accepted password for asipp from 202.127.204.192 port 57240 ssh2
Jan 12 11:13:01 pcfrt sshd[5895]: pam_unix(sshd:session): session opened for user asipp by (uid=0)
Jan 12 11:13:01 pcfrt sshd[5948]: Received disconnect from 202.127.204.192 port 57240:11: disconnected by user
Jan 12 11:13:01 pcfrt sshd[5948]: Disconnected from 202.127.204.192 port 57240
Jan 12 11:13:01 pcfrt sshd[5895]: pam_unix(sshd:session): session closed for user asipp
Jan 12 11:15:52 pcfrt sshd[7493]: Accepted password for root from 202.127.204.3 port 47868 ssh2
Jan 12 11:15:52 pcfrt sshd[7493]: pam_unix(sshd:session): session opened for user root by (uid=0)

我试过了ssh-keygen -R 202.127.204.19。没用。另外,由于服务器使用 RHEL7,我无法UsePAM no在 sshd_config 中进行设置。

答案1

您需要安装并配置 openssh windows 功能,然后配置它并将默认 shell 配置为 wsl。这里有一篇很棒的博客文章 https://www.hanselman.com/blog/the-easy-way-how-to-ssh-into-bash-and-wsl2-on-windows-10-from-an-external-machine

相关内容