大网数据广告活动
服务器运维问答

SSH tunnel throws an error: bind [ip]:801: Cannot assign requested address channel_setup_fwd_listener_tcpip: cannot listen to port: 801

· 52CE 我爱测

If I forward the port from the server to my localhost (I used my web server on apache on my other virtual machine as the server for forwarding), then everything turns out without problems. ssh -L 801:192.168.0.115:80 [email omitt…

问题原文

原帖链接 · Rag Zan · CC BY-SA 4.0

If I forward the port from the server to my localhost (I used my web server on apache on my other virtual machine as the server for forwarding), then everything turns out without problems.

ssh -L 801:192.168.0.115:80 [email omitted]

But when trying to grant access to other network members, an error occurs

ssh -L 192.168.0.115:801:192.168.0.115:80 [email omitted]

bind [192.168.0.115]:801: Cannot assign requested address 
channel_setup_fwd_listener_tcpip: cannot listen to port: 801 Could not request local forwarding.

And of course, when trying to access port 801, nothing worked. I tried to use different ports greater than 4000, when looking at netstat no ports are used, I killed the port but nothing happened.

When trying to use the reverse SSH tunnel, it did not give any errors, but it also failed to open localhost: 801 on the virtual machine

ssh -R 192.168.0.115:801:127.0.0.1:80 [email omitted]

(only on the ssh machine itself through which the tunnel was laid)

when trying to open the SSH debugger (ssh -R 192.168.0.115:801:127.0.0.1:80 [email omitted] -v), it outputs this:

debug1: Remote: Forwarding listen address "192.168.0.115" overridden by server GatewayPorts 
debug1: remote forward success for: listen 192.168.0.115:801, connect 127.0.0.1:80 
debug1: Sending environment.

Settings: in sshd_config changed:

Gateway yes
AllowTcpForwarding yes

(on all computers)

When trying to connect IPv4, the result did not change (ssh -4 -L 0.0.0.0:801:192.168.0.115:80 [email omitted])

There could have been an error in the initially incorrect setting, but when resetting all machines and installing from scratch, nothing has changed.

已采纳解答

原帖链接 · Nikita Kipriyanov · CC BY-SA 4.0

For local forwarding, you need to use -g on the command line, or enable GatewayPorts to yes (default no, disabled) in .ssh/config or /etc/ssh_config to allow listening on non-localhost address:

ssh -g -L 192.168.0.115:801:192.168.0.115:80 [email omitted]

See man ssh_config and man ssh.

To be able to listen on non-localhost IP when doing remote forwarding, you need to enable it in the remote server's sshd_config: set GatewayPorts to clientspecified or yes (default no, disabled). See man sshd_config.

Notice, not just "Gateway" but "GatewayPorts". By the way, it was mentioned in the extended error message that you've got, that the reverse forwarding was overridden because of this restrictive setting. Read your error messages carefully!

Also, notice that port 801 is privileged. You can't listen to it as ordinary user. When doing SSH local port forwarding (-L) it is being bound by the client; you need to bear the CAP_NET_BIND_SERVICE capability, e.g. run ssh client as root or under authbind or something like this. When doing SSH remote port forwarding (-R), however, the port is bound by the sshd process after dropping privileges to the user who is authenticated, so that user needs to be root (i.e. connecting to SSH directly as root, ouch), or sshd needs to bear the same capability on the remote (that hurts again). Here's more about Linux capabilities (and there are links further).

Much easier and safer to just pick a port number above 1024.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-03-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。