通过 SSH 远程访问 Windows 上的 Ubuntu Bash

通过 SSH 远程访问 Windows 上的 Ubuntu Bash

这个答案我能够从本地主机通过 ssh 进入 Linux 子系统,但这还不足以让我进行远程登录。

我有:

  1. 以管理员身份运行 Bash 开始菜单项;
  2. /etc/ssh/sshd_config按照上面链接的答案中建议的所有修改进行;
  3. 从端口 22 更改为端口 2222,以尝试避免使用系统端口带来的任何安全问题;
  4. 已验证其是否ssh trey@localhost -p 2222按预期工作。

但是,这不允许我从 LAN 上的其他主机登录。如果我停止服务 ( sudo service ssh stop) 并以调试模式运行 ( sudo /usr/sbin/sshd -d),我会得到以下输出:

sudo: unable to resolve host SYCORAX
debug1: sshd version OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type DSA
debug1: private host key: #1 type 2 DSA
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type ECDSA
debug1: private host key: #2 type 3 ECDSA
debug1: private host key: #3 type 4 ED25519
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-d'
Set /proc/self/oom_adj from 0 to -17
debug1: Bind to port 2222 on 0.0.0.0.
Server listening on 0.0.0.0 port 2222.
debug1: Bind to port 2222 on ::.
Bind to port 2222 on :: failed: Address already in use.

如果我尝试使用以太网的 IP 地址(可 ping 通)从另一台主机 ssh,它会保持在那里,不会改变。但是,如果我尝试从本地主机 ssh,它会继续:

debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 4 out 4 newsock 4 pipe -1 sock 7
debug1: inetd sockets after dupping: 3, 3
Connection from 127.0.0.1 port 60453 on 127.0.0.1 port 2222
debug1: Client protocol version 2.0; client software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8 pat OpenSSH_6.6.1* compat 0x04000000
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: list_hostkey_types: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: client->server aes128-ctr [email protected] none
debug1: kex: server->client aes128-ctr [email protected] none
debug1: expecting SSH2_MSG_KEX_ECDH_INIT
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: KEX done
debug1: userauth-request for user trey service ssh-connection method none
debug1: attempt 0 failures 0
debug1: PAM: initializing for "trey"
debug1: PAM: setting PAM_RHOST to "localhost"
debug1: PAM: setting PAM_TTY to "ssh"
Failed none for trey from 127.0.0.1 port 60453 ssh2
debug1: userauth-request for user trey service ssh-connection method password
debug1: attempt 1 failures 0
debug1: PAM: password authentication accepted for trey
Accepted password for trey from 127.0.0.1 port 60453 ssh2
debug1: do_pam_account: called
debug1: PAM: establishing credentials
debug1: Entering interactive session for SSH2.
debug1: server_init_dispatch_20
debug1: server_input_channel_open: ctype session rchan 0 win 1048576 max 16384
debug1: input_session_request
debug1: channel 0: new [server-session]
debug1: session_new: session 0
debug1: session_open: channel 0
debug1: session_open: session 0: link with channel 0
debug1: server_input_channel_open: confirm session
debug1: server_input_global_request: rtype [email protected] want_reply 0
debug1: server_input_channel_req: channel 0 request pty-req reply 1
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req pty-req
debug1: Allocating pty.
debug1: session_pty_req: session 0 alloc /dev/pts/0
debug1: SELinux support disabled
debug1: server_input_channel_req: channel 0 request env reply 0
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req env
debug1: server_input_channel_req: channel 0 request shell reply 1
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req shell
Starting session: shell on pts/0 for trey from 127.0.0.1 port 60453
debug1: Setting controlling tty using TIOCSCTTY.

我从本地 SSH 客户端成功登录,并打印以下调试消息:

trey@localhost's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.4.0+ x86_64)

 * Documentation:  https://help.ubuntu.com/

Last login: Sat Aug 27 14:51:29 2016 from localhost
debug1: PAM: reinitializing credentials
debug1: permanently_set_uid: 1000/1000
debug1: Unable to open session: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
Environment:
  LANG=en_US.UTF-8
  USER=trey
  LOGNAME=trey
  HOME=/home/trey
  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
  MAIL=/var/mail/trey
  SHELL=/bin/bash
  SSH_CLIENT=127.0.0.1 60453 2222
  SSH_CONNECTION=127.0.0.1 60453 127.0.0.1 2222
  SSH_TTY=/dev/pts/0
  TERM=xterm
trey@SYCORAX:~$

一旦我从该本地注销远程控制,我从中得到以下信息sshd退出前的过程:

debug1: Received SIGCHLD.
debug1: session_by_pid: pid 677
debug1: session_exit_message: session 0 channel 0 pid 677
debug1: session_exit_message: release channel 0
debug1: session_pty_cleanup: session 0 release /dev/pts/0
syslogin_perform_logout: logout() returned an error
debug1: session_by_channel: session 0 channel 0
debug1: session_close_by_channel: channel 0 child 0
debug1: session_close: session 0 pid 0
debug1: channel 0: free: server-session, nchannels 1
Received disconnect from 127.0.0.1: 11: disconnected by user
debug1: do_cleanup
debug1: PAM: cleanup
debug1: PAM: closing session
debug1: PAM: deleting credentials
debug1: audit_event: unhandled event 12

有趣的是,我完全没有遇到任何问题,无论是本地还是远程,都可以在同一台主机上运行 Ubuntu 16 的 Hyper-V 虚拟机中sshd在端口 22 上运行。 (当然,Hyper-V VM 客户机的 IP 地址与 Windows 主机不同。)

Telnet 到端口 2222 也失败了。

我怀疑sshd绑定到 IPv6 端口是了解发生了什么事情的关键,但我所知道的工具(包括 Windows 和 Linux 方面的)都没有显示任何绑定到 TCP v4 或 v6 中的端口 2222 的内容。

答案1

您可能需要在 Windows 防火墙上打个洞。我遇到了这个问题,并通过短暂关闭 WF 进行了验证,并确认我可以从外部机器访问。

然后我添加了一条规则来允许我设置的端口,对你来说这个端口是 2222。

我重新启用了 WF,一切正常。

答案2

debug1: Bind to port 2222 on 0.0.0.0.
Server listening on 0.0.0.0 port 2222.
debug1: Bind to port 2222 on ::.
Bind to port 2222 on :: failed: Address already in use.

2222您以某种方式在 中两次绑定到端口sshd_config。该文件是什么样子的?

不确定这些行中是否隐藏着其他问题,如果是,请澄清。

答案3

从 Win 10 上提升的 PowerShell 控制台发出以下命令:

New-NetFirewallRule -DisplayName "Allow Inbound Port 2222" -Direction Inbound -LocalPort 2222 -Protocol TCP -Action Allow

使用上述 PS 命令,您将在 Windows 防火墙上打开入站端口 2222。这将使您的其他 LAN 主机能够登录到当前 Win 10 主机上的 WSL 实例。

高血压

相关内容