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

SSH RemoteForward fails with shared Control Socket

· 52CE 我爱测

My ~/.ssh/config contains: ControlMaster auto ControlPath ~/.ssh/socket-%r@%h:%p Host hostname.example # TextMate rmate port RemoteForward :52698 localhost:52698 When I make an initial connection to the host, it successfully crea…

问题原文

原帖链接 · Lachlan Hunt · CC BY-SA 3.0

My ~/.ssh/config contains:

ControlMaster auto
ControlPath ~/.ssh/socket-%r@%h:%p

Host hostname.example
    # TextMate rmate port
    RemoteForward :52698 localhost:52698

When I make an initial connection to the host, it successfully creates the master socket file at ~/.ssh-lachlanhunt@hostname.example:22.

But when I open a new terminal and attempt to make a second connection, I get this error:

$ ssh hostname.example
mux_client_forward: forwarding request failed: remote port forwarding failed for listen port 52698
muxclient: master forward request failed
ControlSocket /Users/lachlanhunt/.ssh/[email omitted]:22 already exists, disabling multiplexing
Warning: remote port forwarding failed for listen port 52698

But if I comment out the RemoteForward line in the config, it's able to successfully reuse the connection, making it connect faster. Is there any way I can configure ssh to support both multiplexed connections and enable the remote port forwarding, such that it only attempts to forward the port if it's the master connection?

My system:

  • macOS Sierra 10.12.6
  • OpenSSH_7.4p1, LibreSSL 2.5.0

社区高分解答

原帖链接 · Lachlan Hunt · CC BY-SA 3.0

I figured out a solution. I use the Match section with the host and negated exec keywords to test for the hostname and the existence of the control socket file. If the file doesn't exist, then this is the master connection, so setup the port forwarding. Otherwise, this is a slave connection and the forwarding is skipped.

ControlMaster auto
ControlPath ~/.ssh/socket-%r@%h:%p

Match host hostname1.example.com,hostname2.example.com !exec "[ -e ~/.ssh/socket-%r@%h:%p ]"
    # TextMate rmate port
    RemoteForward :52698 localhost:52698

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

资料来源与适用范围

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

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

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