问题原文
原帖链接 · jstm88 · CC BY-SA 3.0
Follow-Up: It looks like the rapid series of disconnects coinciding with a few months of running each server is probably coincidental and just served to reveal the actual problem. The reason it failed to reconnect is almost certainly due to the AliveInterval values (kasperd's answer). Using the ExitOnForwardFailure option should allow the timeout to occur properly before reconnecting, which should solve the problem in most cases. MadHatter's suggestion (the kill script) is probably the best way to make sure that the tunnel can reconnect even if everything else fails.
I have a server (A) behind a firewall that initiates a reverse tunnel on several ports to a small DigitalOcean VPS (B) so I can connect to A via B's IP address. The tunnel has been working consistently for about 3 months, but has suddenly failed four times in the last 24 hours. The same thing happened a while back on another VPS provider - months of perfect operation, then suddenly multiple rapid failures.
I have a script on machine A that automatically executes the tunnel command (ssh -R *:X:localhost:X address_of_B for each port X) but when it executes, it says Warning: remote port forwarding failed for listen port X.
Going into the sshd /var/log/secure on the server shows these errors:
bind: Address already in use
error: bind: Address already in use
error: channel_setup_fwd_listener: cannot listen to port: X
Solving requires rebooting the VPS. Until then, all attempts to reconnect give the "remote port forwarding failed" message and will not work. It's now to the point where the tunnel only lasts about 4 hours before stopping.
Nothing has changed on the VPS, and it is a single-use, single user machine that only serves as the reverse tunnel endpoint. It's running OpenSSH_5.3p1 on CentOS 6.5. It seems that sshd is not closing the ports on its end when the connection is lost. I'm at a loss to explain why, or why it would suddenly happen now after months of nearly perfect operation.
To clarify, I first need to figure out why sshd refuses to listen on the ports after the tunnel fails, which seems to be caused by sshd leaving the ports open and never closing them. That seems to be the main problem. I'm just not sure what would cause it to behave this way after months of behaving as I expect (i.e. closing the ports right away and allowing the script to reconnect).
社区高分解答
原帖链接 · Stephen Quan · CC BY-SA 3.0
For me when a ssh tunnel disconnects it takes awhile for the connection to reset so the ssh process continues to block leaving me with no active tunnels and I don't know why. A workaround solution is to put ssh into the background with -f and to spawn new connections without waiting for old connections to reset. The -o ExitOnForwardFailure=yes can be used to limt the number of new processes. The -o ServerAliveInterval=60 improves the reliability of your current connection.
You can repeat the ssh command frequently, say, in a cron, or, in a loop in your script, for example, in the following, we run the ssh command every 3 minutes:
while (1)
do
ssh -f user@hostname -Rport:host:hostport -N -o ExitOnForwardFailure=yes -o ServerAliveInterval=60
sleep 180
done
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
