SSH 端口隧道断开并显示“选择:无效参数”

SSH 端口隧道断开并显示“选择:无效参数”

我正在通过隧道连接到远程服务器以访问私有 API。我正在使用以下命令(通过将其输入到终端)来转发单个端口:ssh -L $local_port:$host:$port -v。尝试了许多不同的本地端口(9000、9001 等)。

我可以直接从浏览器或通过 nginx 代理使用 api 几个小时而不会出现问题。如果从 playframework 应用程序访问它,ssh 会话会打印“select: 无效参数”并关闭连接。

发生这种情况时日志如下所示:

debug1: channel 1016: free: direct-tcpip: listening port 9001 for ec2-xxx.compute-1.amazonaws.com port 9000, connect from 127.0.0.1 port 65349, nchannels 1
select: Invalid argument
Connection to ec2-xxx.compute-1.amazonaws.com closed.
Transferred: sent 243904, received 64728 bytes, in 87.8 seconds
Bytes per second: sent 2778.0, received 737.2
debug1: Exit status -1

Play 应用程序正在使用 AsyncHttpClient 发出 GET 请求 localhost:$local_port。

连接方面有办法修复这个问题吗?例如让 ssh 忽略错误并继续连接?

答案1

我刚刚注意到你的调试输出显示通道 1016。我怀疑你的文件描述符用完了。我刚刚在我的 Linux 笔记本电脑上检查了一下,ulimit -a 显示最大值为 1024。所以,我想你也达到了这个上限。理想的解决方案是找出你如何使用这么多同时使用的文件描述符,并以某种方式减少它。

另一种方法是简单地提高打开文件描述符的最大数量:

 (useful debug commands here)
 lsof -p <pid>    should show you the open filedescriptors in use by a process, btw.
 ulimit -a     should show you your soft limits in this specific shell
 ulimit -aH    should show you the hard limits in this specific shell
 cat /proc/<pid>/limits     should show you the limits in effect on a specific process.

要提高 fd 限制,您需要在两台机器上编辑 /etc/security/limits.conf 以包含以下行:

*       hard    nofile    4096
*       soft    nofile    4096
root    hard    nofile    4096
root    soft    nofile    4096

这些新配置的限制仅在您再次登录时才会生效。我经常通过 ssh 连接到 localhost 来测试这类东西,但如果我是你,我会尽可能重启。

如果您无法重新启动远程计算机,那么至少我会重新启动 sshd。在再次使用 ssh 之前,请使用 ulimit -a 确认最大打开文件或文件描述符为 4096。

当您通过 ssh 进入远程机器时,运行 ulimit -n 来验证它是否为 4096。

祝你好运。

答案2

前面的答案更有可能是正确的,但如果增加文件句柄的数量没有帮助,您可能需要检查您的登录文件(.bashrc,.bash_profile,.login,/etc/login等)是否不正确使用内置bash函数“select” - 有关更多信息,请参阅man bash。

相关内容