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

Stable remote port fowarding

· 52CE 我爱测

I need some port on a publicly accessible host constantly forwarded to a server that is behind a firewall. I'm currently using autossh to connect from the server to an openssh container running on GKE and forward the port as requ…

问题原文

原帖链接 · Johannes Bauer · CC BY-SA 4.0

I need some port on a publicly accessible host constantly forwarded to a server that is behind a firewall.

I'm currently using autossh to connect from the server to an openssh container running on GKE and forward the port as required:

/usr/bin/autossh \
   -y \
   -v \
   -i <ssh key> \
   -M 0 \
   -q \
   -o 'ServerAliveInterval 60' \
   -o 'ServerAliveCountMax 3' \
   -p <alternative ssh port> \
   -l <user name> \
   <name resolving (locally) to GKE public IP address> \
   -R 30000:localhost:6000 
   'while true; do echo were up; sleep 10; done'  # this is done to generate a minimal amount of traffic.

Problem is, sometimes when the connection drops, autossh reconnects but can't set up forwarding again. It won't fail though, and therefore it won't retry. When I stop autossh and try to connect in such a situation, just using ssh (-vvv), I get a warning message that says the port is already in use.

Questions:

  1. Is there a better way to keep forwarding a port to a server behind a firewall (securely)

  2. Why isn't the port freed immediately once the connection is dropped, can I force it to be freed somehow, or at least disconnect and try again until it is freed?

已采纳解答

原帖链接 · Srividya · CC BY-SA 4.0

  1. This is because the host you’re connecting to may not realize that the previous connection has died, and is still utilizing the port. While the new autossh connection will succeed, it won’t open a tunnel and autossh won't restart since it thinks the connection is okay.

  2. There are two particular OpenSSH options that are useful when using autossh:

    1. ExitOnForwardFailure=yes on the client side to make sure forwardings have succeeded when autossh assumes the connection is set up properly.

    2. ClientAliveInterval on the server side to make sure the listening socket is closed on the server side if the connection closes on the client side.

    Replace the autossh command for:

    -o “ServerAliveCountMax 3” with -o “ServerAliveCountMax=3”

    -o “ServerAliveInterval 60” with -o “ServerAliveInterval =60”

    the “=” sign was critical

Example:

    autossh -M 0 -q -f -gNC -o “ServerAliveCountMax=3” -o “ServerAliveInterval=60” -o “ExitOnForwardFailure=yes” -R xxxx:localhost:22  auto ssh@remotehost

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

资料来源与适用范围

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

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

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