SSH远程端口转发无响应

6
我正在尝试使用Windows电脑上的SSH从我的本地网络中的Raspberry Pi进行远程端口转发。
ssh -R 5941:localhost:8000 pi@192.168.1.191

从上面的命令中,我正在尝试将来自树莓派服务器(192.168.1.191)端口5941的连接转发到我的个人电脑(localhost)上的端口8000,该端口托管了一个端口为8000的Web服务器。

以上ssh命令执行时没有错误,但是当我尝试在树莓派(192.168.1.191)上访问http://localhost:5941/时,我得到了以下错误:

ERR_EMPTY_RESPONSE

我已经尝试过:

  1. 关闭了我的Windows PC防火墙
  2. 在树莓派上运行netstat,结果如下:
$ sudo netstat -a | grep 5941
tcp        0      0 0.0.0.0:5941        0.0.0.0:*               LISTEN
tcp6       0      0 [::]:5941               [::]:*                  LISTEN
  1. 当我以详细模式运行ssh命令时 ssh -R 5941:localhost:8000 pi@192.168.1.191 -v 我会得到以下输出:
debug1: Remote connections from LOCALHOST:5941 forwarded to local address localhost:8000
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: console supports the ansi parsing
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Remote: Forwarding listen address "localhost" overridden by server GatewayPorts
debug1: remote forward success for: listen 5941, connect localhost:8000
debug1: All remote forwarding requests processed

当我尝试在树莓派上(192.168.1.191)访问http://localhost:5941/时,ssh控制台上显示了以下内容:

debug1: client_input_channel_open: ctype forwarded-tcpip rchan 3 win 2097152 max 32768
debug1: client_request_forwarded_tcpip: listen localhost port 5941, originator ::1 port 58122
debug1: getsockopt TCP_NODELAY: Invalid argument
debug1: connect_next: host localhost ([::1]:8000) in progress, fd=7
debug1: channel 1: new [::1]
debug1: confirm forwarded-tcpip
debug1: channel 1: connected to localhost port 8000
debug1: channel 1: free: ::1, nchannels 2

我该如何进一步诊断我的问题?

本地系统上,localhost:8000 上运行着哪个服务?提供此服务的进程是否记录任何消息? - Kenster
1个回答

13
解决方法是使用127.0.0.1代替localhost
ssh -R 5941:127.0.0.1:8000 pi@192.168.1.191

由于某种原因,Windows 上的 ssh 端口转发无法使用 ipv6,因此此命令强制使用 ipv4,在 Linux 上可以正常使用 localhost

我从这篇帖子中找到了解决方法。


1
对于其他可能会来到这里的人,我进行了上述更改(即将“localhost”改为127.0.0.1),并将其转换为“gcloud compute ssh…”端口转发命令,用于连接到GCP Windows服务器VM,我的ZMQ REQ/REP Python连接突然活跃起来。 - ryan
不要忘记在更改后,在/etc/ssh/sshd_config中启用:AllowTcpForwarding yesGatewayPorts yes,并重新启动SSH服务:sudo systemctl restart sshd - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接